6

I am trying to create a dll which will create a thread when you load him for some reason the thread function is not doing anything.. :\

this is my code:

dllthread != null.. why its not working?

#include "stdafx.h"
DWORD WINAPI ThreadProc(
  __in  LPVOID lpParameter
)
{

    std::ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.\n";
    myfile.close();

    return 0;
}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{

    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:    
        DWORD DllThreadID;
        HANDLE DllThread; //thread's handle

        DllThread=CreateThread(NULL,0,&ThreadProc,0,0,&DllThreadID);
// 
        if (DllThread == NULL)
            MessageBox(NULL, L"Error", L"Error", MB_OK);

        CloseHandle(DllThread);
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:


        break;
    }
    return TRUE;
}
D_R
  • 4,894
  • 4
  • 45
  • 62
  • 3
    Don't do this. Creating a thread from DllMain is a [bad idea](http://blogs.msdn.com/b/oldnewthing/archive/2007/09/04/4731478.aspx). – Joe White Nov 25 '11 at 20:53
  • what are you suggesting me to do then? i need to create a thread from a dll file.. – D_R Nov 25 '11 at 20:59
  • Dan, do you own the source of the application into which this DLL is loaded? – hmjd Nov 25 '11 at 21:06
  • Yes i am loading the dll using LoadLibary function.. LoadLibrary(L"alpha.dll") don't get me wrong. it does load the dll when i remove the "if (DllThread == NULL)" a messagebox is poped up the problem is that the thread is not working – D_R Nov 25 '11 at 21:07
  • You need to export a method that starts your thread, so the host EXE can decide when (and whether) to call it. Keep in mind that just because your DLL is being *loaded* doesn't mean the host app actually intends to *run* your code -- it might be Explorer trying to load an icon resource, and your thread could crash the whole desktop. (Or lock it up, if you're trying to create your thread from DllMain.) – Joe White Nov 25 '11 at 21:14
  • It is not allowed to call LoadLibrary() or FreeLibrary() from the DLL entry point/ startup code. Windows may get confused and Load or Free DLLs in the wrong order. Creating a thread in DllMain() is tricky because the system may have to call LoadLibrary() to do that. In general: don't do things in DllMain() that posibly can fail. Write an exported function to do the startup work instead, and call that function from the host application. – bert-jan Nov 25 '11 at 21:44

1 Answers1

11

Instead of starting the thread from DllMain() export a function that would launch the thread instead:

extern "C" __declspec(dllexport) void start_thread()
{
    DWORD DllThreadID;
    HANDLE DllThread; //thread's handle

    DllThread=CreateThread(NULL,0,ThreadProc,0,0,&DllThreadID);
    if (DllThread == NULL)
        MessageBox(NULL, L"Error", L"Error", MB_OK);
    else
        CloseHandle(DllThread);

}

After calling LoadLibrary() use GetProcAddress() to get access to the start_thread() function.

Hope this helps.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • I know this is a very old thread, but it may shed some light on a problem I'm having...https://stackoverflow.com/questions/49653102/threads-not-running-why?noredirect=1#comment86319707_49653102 – SPlatten Apr 04 '18 at 16:31