title: LoadLibrary() an EXE?
body: <p>I have an executable (that I created using Visual C++ 10), and I need to use its capabilities from another program I wrote (same environment). Due to complex deployment requirements which I won't go into, building a DLL from the required functionality and loading it in both programs is not something I can do.</p>
  
  <p>So I thought that I can <code>__declspec(dllexport)</code> some functions in the EXE, and then <code>LoadLibrary()</code> will let me <code>GetProcAddress()</code> them.</p>
  
  <p>Obviously this can't be done, though when I started looking at it - it looked feasible. </p>
  
  <p>Specifically, when you <code>__declspec(dllexport)</code> functions in an EXE project, Visual C++ also generates a <code>lib</code> file for dynamic linking - so you don't even need to use <code>LoadLibrary()</code> - just link against the resulting lib and call the functions.</p>
  
  <p>Unfortunately, the main problem is that when you declare the resulting file as an EXE, Visual C++ adds the "CRTmain" entry point into the resulting file, instead of the "CRTDLLmain" that a DLL gets. When Windows (automatically) <code>LoadLibrary()</code> the EXE from your main program, it doesn't call the the "CRTDLLmain" entry point (because it doesn't exist), the C runtime for the module doesn't get initialized, and as a result all interesting work (such as memory allocation) fails with interesting(*) runtime exceptions.</p>
  
  <p>So as follows, my question is: is there a way to cause Visual C++ to build into the resulting file both the "CRTmain" entry point <strong>and</strong> the "CRTDLLmain" entry point?</p>
  
  <p>(*) "Interesting" as in an old Chinese curse.</p>