在 DLL 中启动应用程序

Start Application within a DLL

本文关键字:应用程序 启动 DLL      更新时间:2023-10-16

所以我想创建一个包含应用程序的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;
}

和启动应用程序功能:

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

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

所以我的问题是我能做什么来使用 dll?

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

是的,有一个问题是 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.

源。

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