本文介绍了LoadLibrary() 一个 EXE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可执行文件(我使用 Visual C++ 10 创建),我需要从我编写的另一个程序(相同环境)中使用它的功能.由于复杂的部署要求,我不会涉及到,从所需的功能构建一个 DLL 并将其加载到两个程序中不是我能做的.

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.

所以我想我可以在EXE中__declspec(dllexport)一些函数,然后LoadLibrary()会让我GetProcAddress() 他们.

So I thought that I can __declspec(dllexport) some functions in the EXE, and then LoadLibrary() will let me GetProcAddress() them.

显然这是不可能的,但当我开始看它时 - 它看起来可行.

Obviously this can't be done, though when I started looking at it - it looked feasible.

具体来说,当您在 EXE 项目中使用 __declspec(dllexport) 函数时,Visual C++ 还会生成一个用于动态链接的 lib 文件 - 因此您甚至不需要使用 LoadLibrary() - 只需链接到生成的库并调用函数.

Specifically, when you __declspec(dllexport) functions in an EXE project, Visual C++ also generates a lib file for dynamic linking - so you don't even need to use LoadLibrary() - just link against the resulting lib and call the functions.

不幸的是,主要问题是当您将生成的文件声明为 EXE 时,Visual C++ 将CRTmain"入口点添加到生成的文件中,而不是 DLL 获得的CRTDLLmain".当 Windows(自动)LoadLibrary() 主程序的 EXE 时,它不会调用CRTDLLmain"入口点(因为它不存在),模块的 C 运行时不会'没有被初始化,结果所有有趣的工作(例如内存分配)都失败了,并出现了有趣的 (*) 运行时异常.

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) LoadLibrary() 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.

如下,我的问题是:有没有办法让 Visual C++ 将CRTmain"入口点CRTDLLmain"入口点都构建到结果文件中?

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 and the "CRTDLLmain" entry point?

(*) "有趣" 就像一个古老的中国诅咒.

(*) "Interesting" as in an old Chinese curse.

推荐答案

简短的回答是不".仔细观察后,没有办法让 VC++ 做我想做的事,很可能没有任何其他编译器.

The short answer, is "no". After looking far and wide, there is no way to get VC++ to do what I want, and quite likely not any other compiler.

主要问题是大多数人都知道和喜爱的 main() 入口点并不是 C++ 可执行文件中真正的入口点:编译器需要做大量的初始化工作才能获得"C++ 运行时库"到可用状态,以及初始化全局变量、静态变量等.此初始化在共享库中使用与在可执行文件中不同的代码,并且无法像另一个一样运行.

The main issue is that the main() entry point most people know and love is not the real entry point in C++ executables: the compiler needs to do a lot of initialization work to get the "C++ Run Time library" to a usable state, as well as initialize globals, statics and the likes. This initialization uses different code in shared libraries than in executables and there is no way to one to behave like another.

可能可以做的一件事是将共享功能构建到一个 DLL 中,并且主要可执行文件将 DLL 作为资源嵌入,并从可执行文件的内存映射区域加载它(有几个代码示例如何在 stackoverflow 和网络上的其他地方使用 VC++ 执行此操作).现在另一个程序可以通过从捆绑可执行文件加载 DLL 来做同样的事情.

One thing that possibly can be done, is to build the shared functionality into a DLL, and for the primary executable to embed the DLL as a resource, and load it from a memory mapped region of the executable file (there are several code samples how to do this with VC++ on stackoverflow and elsewhere on the web). Now another program can do the same thing by loading the DLL from the bundling executable.

这篇关于LoadLibrary() 一个 EXE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:10