LoadLibrary () EXE?

I have an executable file (which I created using Visual C ++ 10), and I need to use its capabilities from another program that I wrote (in the same environment). Due to the complex deployment requirements that I will not deal with, creating a DLL from the required functionality and loading it into both programs is not something I can do.

So, I thought I could __declspec(dllexport) some functions in an EXE, and then LoadLibrary() provide me with GetProcAddress() them.

Obviously, this cannot be done, although when I started looking at it, it seemed practically feasible.

In particular, when you are __declspec(dllexport) in an EXE project, Visual C ++ also creates a lib file for dynamic linking, so you donโ€™t even need to use LoadLibrary() - just a link to the resulting lib and function call.

Unfortunately, the main problem is that when you declare the resulting file as EXE, Visual C ++ adds the "CRTmain" access point to the resulting file instead of the "CRTDLLmain" that the DLL receives. When Windows (automatically) LoadLibrary() EXE from your main program, it does not call the entry point "CRTDLLmain" (since it does not exist), the C runtime for the module is not initialized, and as a result all the interesting work (for example, memory allocation) fails with interesting (*) runtime exceptions.

So my question is: is there a way to get Visual C ++ to embed the CRTmain entry point and the CRTDLLmain entry point in the resulting file?

(*) "Interesting," as in an ancient Chinese curse.

+9
c windows visual-c ++ visual-studio-2010
source share
3 answers

The short answer is no. After you looked differently, there is no way to get VC ++ to do what I want, and most likely not any other compiler.

The main problem is that the main() entry point, which most people know and love, is not a real entry point to C ++ executables: the compiler needs to perform many initialization operations to get the "C ++ runtime library" before useful state, as well as initialize global variables, statics and the like. This initialization uses different codes in shared libraries than in executable files, and there is no way to behave like another.

One thing that can possibly be done is to create common functions in the DLL and for the main executable file to embed the DLL as a resource and load it from the area with memory display of the executable file (there are several code examples on how to do this with V ++ on stackoverflow and elsewhere on the Internet). Now another program can do the same by loading the DLL from the bundling executable.

-one
source share

Yes it is possible.

http://www.codeproject.com/Articles/1045674/Load-EXE-as-DLL-Mission-Possible

The idea: a) fix the IAT and b) call the CRT before calling your export.

+8
source share

Simply no! The problem is that CRT and the EXE you want to load use some globule variables. You core exe does the same. So how should memory allocation be used?

If you want to use such a structure, you must use a DLL to be aware of multithreading, CRT initialization, and all these other materials. You need it!

But what about COM Automation? Wouldn't it be a simple decision to use your code in one EXE from another?

0
source share

All Articles