在DLL中启动应用程序

时间:2012-06-02 16:32:56

标签: c++ winapi dll

所以我想创建一个包含应用程序的Dll。我的代码:

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
                     )
{
    switch(ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
            StartApp();
            break;
    }

    return TRUE;
}

StartApp功能:

void StartApp()
{   
    //some declartions
    iPtr->Start();
}

问题是函数Start()在连续循环中运行(就像while(true)),我认为这是问题导致dll永远不会中断并返回true。我试图在不同的线程中运行它,但这不起作用。

所以我的问题是如何使用dll?

如果DllMain没有完成并且没有返回TRUE会有问题吗?

1 个答案:

答案 0 :(得分:0)

是的,有一个问题是DllMain没有返回,正如文档所述:

When a DLL entry-point function is called because a process is loading, the function returns TRUE to indicate success. For processes using load-time linking, a return value of FALSE causes the process initialization to fail and the process terminates. For processes using run-time linking, a return value of FALSE causes the LoadLibrary or LoadLibraryEx function to return NULL, indicating failure. (The system immediately calls your entry-point function with DLL_PROCESS_DETACH and unloads the DLL.) The return value of the entry-point function is disregarded when the function is called for any other reason.

Source

您可以为StartApp函数创建一个包装函数,并通过您的dll公开它。之后,您可以从可执行文件中调用导出的StartApp函数(在加载dll之后)。确保你从一个不同的线程中调用它,因为它会阻止它。