简体   繁体   中英

CreateThread in 64bit DLL won't work

I have a 32 bit and a 64 bit executable. Both load a DLL that is of the same bit, as in the 64 bit executable loads a 64bit dll. Anyway, the 32 bit DLL works perfectly, it creates a thread and pops a hello world messagebox. The 64bit DLL however, that piece of code never executes. It's like the createthread fails.

    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. 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? 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. 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. By doing anything complex you can inadvertently load another DLL causing a deadlock.

Instead you would usually have a separate initialization function in your DLL that your calling code can call after it has loaded the DLL.

Ok the solve was simple, the thread exited too early. Adding WaitForSingleObject(hThread, INFINITE); solved the issue. Wasn't necessary in 32bit for some reason. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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