1

我试图将 WinMain 函数隐藏在 DLL 中,以避免一遍又一遍地再次键入大部分代码。

我从 DLL 中导出 wWinMain,将其声明为

extern "C" int WINAPI wWinMain( ... ) { // repetitive code here }

并使用了链接器选项/EXPORT:wWinMain,但是当我尝试在另一个项目中使用导入库时出现错误

LIBCMTD.lib(wincrt0.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function __tmainCRTStartup

备注我确实想使用 GUI 界面,我知道这是定义 main 而不是 WinMain 函数时的常见错误。此外,我在两个项目中都启用了 UNICODE 支持。我应该怎么办?

4

6 回答 6

2

这是不可能的,链接器只能将 EXE 的入口点设置为 EXE 内部的函数。将 DLL 中的 wWinMain() 重命名为其他名称。在链接到 EXE 的源代码文件中编写 wWinMain(),只需调用 DLL 的导出函数即可。

于 2010-12-29T06:22:19.600 回答
1
//  ...somewhere in a .cpp file within my .dll's sources...
#define WinMain WinMainOld // ...to suppress Win32 declaration of WinMain
#include <windows.h>
#undef WinMain // ...so that WinMain below is not replaced
.   .   . 
#pragma comment(linker, "/export:_WinMain@16") // ...to export it from .dll
extern "C" // ...to suppress C++ name decoration
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     PSTR pCmdLine, int nCmdShow)
{
    .   .   .
}
于 2016-02-08T00:03:25.660 回答
0
EXPORT int WINAPI _WinMain_(int (*_main_)(int argc, char **argv), HINSTANCE hInst, HINSTANCE    hPrevInstance, LPSTR commandLine, int nCmdShow);
int _XMain( int argc, char **argv );

#define XMain   WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR commandLine, int nCmdShow)\
{return _WinMain_( _XMain, hInst, hPrevInstance, commandLine, nCmdShow );}  \
int _XMain

然后_WinMain_()打电话或安排_XMain()

在您的应用程序源中:

int XMain( int argc, char **argv )
{
}
于 2014-10-01T19:27:34.980 回答
0

如果要调用 dll 的 WinMain,则需要替换 CRTWinMainStartup 函数(您喜欢的 CRT 库中的_tmainCRTStartup),并使其调用您导出的 WinMain,这会阻止链接器寻找本地 WinMain 并仍然保持正确的流程程序(CRT 启动的源代码应该在任何编译器的 crt 源代码中)

于 2010-12-29T07:23:20.657 回答
0

您应该在 DLL 中使用 WinMain 吗?它不应该是 DllMain 吗?

于 2010-12-29T04:31:35.737 回答
0

我找到了一种将 WinMain 放入 DLL 的方法。

  • 您需要使用 WinMain 而不是 wWinMain (我不知道为什么)
  • 将 def 文件附加到您的项目中,
    并将EXPORTS WinMain附加到 def 文件中。像这样

    出口

    WinMain

    从观察来看,都需要生成导出函数,不仅仅是WinMain。

    经测试,__declspec(dllexport)的方式对WinMain无效。

  • 使用#pragma comment(lib, "testDll.lib")将您的程序链接到DLL 库 或修改项目中的设置。
于 2012-12-19T13:41:20.593 回答