简体   繁体   English

64位DLL中的CreateThread无法正常工作

[英]CreateThread in 64bit DLL won't work

I have a 32 bit and a 64 bit executable. 我有32位和64位可执行文件。 Both load a DLL that is of the same bit, as in the 64 bit executable loads a 64bit dll. 两者都加载相同位的DLL,就像在64位可执行文件中加载64位dll一样。 Anyway, the 32 bit DLL works perfectly, it creates a thread and pops a hello world messagebox. 无论如何,32位DLL可以完美地工作,它创建一个线程并弹出一个hello world消息框。 The 64bit DLL however, that piece of code never executes. 但是,64位DLL永远不会执行。 It's like the createthread fails. 就像createthread失败一样。

    case DLL_PROCESS_ATTACH:
        myFunc();
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

void myFunc()
{

    HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&MyThread, NULL, 0, NULL);
}

DWORD WINAPI MyThread(LPVOID param)
{
    MessageBoxA(0, "HELLO 64", 0,0);
    ExitThread(0);
}

Those are the some snippets from the DLL. 这些是DLL中的一些摘要。 I've googeled and all I can come up with is that it's the stack alignment failing? 我已经糊涂了,我所能想到的就是堆栈对齐失败了吗? If that is the reason, how do I properly call CreateThread to make it work? 如果是这个原因,如何正确调用CreateThread使其正常工作? If that isnt the reason, does anyone know what might be wrong? 如果那不是原因,有谁知道这可能是错的吗?

I'd be outmost grateful for any help, thanks in advance! 我将非常感谢您的任何帮助,在此先感谢您!

You have the wrong signature for MyThread. 您为MyThread签名错误。 You should not cast it you should make sure your function matches the signature. 您不应该强制转换它,而应确保您的函数与签名匹配。 The correct code would be: 正确的代码是:

CreateThread(NULL, 0, MyThread, NULL, 0, NULL);

DWORD WINAPI MyThread(LPVOID param)
{
    // etc
}

Apart from that you should not do anything in your DllMain as @GSerg comments because there is a lock that is held while you are in there. 除此之外,您不应在DllMain中以@GSerg注释的形式进行任何操作,因为在那里您会保持一个锁。 By doing anything complex you can inadvertently load another DLL causing a deadlock. 通过执行任何复杂的操作,您可能会无意间加载另一个导致死锁的DLL。

Instead you would usually have a separate initialization function in your DLL that your calling code can call after it has loaded the DLL. 取而代之的是,您通常会在DLL中拥有一个单独的初始化函数,您的调用代码在加载DLL之后便可以对其进行调用。

Ok the solve was simple, the thread exited too early. 好了,解决方法很简单,线程退出得太早了。 Adding WaitForSingleObject(hThread, INFINITE); 添加WaitForSingleObject(hThread,INFINITE); solved the issue. 解决了这个问题。 Wasn't necessary in 32bit for some reason. 由于某些原因,在32位中不是必需的。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM