0

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!

  • possible duplicate of [DLL_PROCESS_ATTACH failing to execute on Windows 7 C++](http://stackoverflow.com/questions/8556782/dll-process-attach-failing-to-execute-on-windows-7-c) – GSerg Aug 17 '12 at 22:54

2 Answers2

3

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.

tinman
  • 6,348
  • 1
  • 30
  • 43
  • 5
    As a general rule, if you're casting a pointer to a function for any purpose other than to cast it back to the exact same type it originally was, you're probably doing something fundamentally broken. – David Schwartz Aug 17 '12 at 23:22
0

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. :)

  • Or maybe it is but it was just a coincidence that your 32-bit piece of program did not exit too early? You should add that `Wait..` to your 32-bit piece of program, too. – xQuare Aug 19 '12 at 07:08
  • I can't understand your answer, unless I assume that you mean the main program exited early and killed the thread before it could show the message box. albert – Albert van der Horst Jul 12 '18 at 12:17