ThreadProc does not run when CreateThread() is called within a DLL.

Discussion in 'C Programming' started by phark52@yahoo.com, Apr 8, 2005.

  1. Guest

    My main app calls LoadLibrary() to load a DLL, which calls
    CreateThread(). This does NOT return NULL and I get a thread ID.
    However, ThreadProc never gets executed when this code is in the DLL.
    It works fine in the standalone EXE source.

    I put example code below.

    #define MB(msg) MessageBox(0, msg, "", MB_OK|MB_ICONINFORMATION);

    DWORD APIENTRY tproc() {
    for(;;)
    MB("hi from tproc");
    return 0;
    }

    BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
    {
    HANDLE h;
    DWORD id;
    MB("Creating thread...");
    h=CreateThread(0, 0, tproc, 0, 0, &id);
    if (!h) { MB("Thread creation failed."); exit(1); }
    // tproc should run now and display msg boxes over and over..
    for(;;);
    exit(0);
    }


    There is something in MSDN.. can someone translate this for me? I don't
    understand. I thought initializing happens in DllMain. How do I tell
    Windows that initialization is done?

    Quote:
    Do not create a thread while impersonating another user. The call will
    succeed, however the newly created thread will have reduced access
    rights to itself when calling GetCurrentThread. The access rights
    granted are derived from the access rights that the impersonated user
    has to the process. Some access rights including
    THREAD_SET_THREAD_TOKEN and THREAD_GET_CONTEXT may not be present,
    leading to unexpected failures.

    * During process startup and DLL initialization routines, new threads
    can be created, but they do not begin execution until DLL
    initialization is done for the process.
    * Only one thread in a process can be in a DLL initialization or detach
    routine at a time.
    * ExitProcess does not return until no threads are in their DLL
    initialization or detach routines.
     
    , Apr 8, 2005
    #1
    1. Advertising

  2. CBFalconer Guest

    wrote:
    >
    > My main app calls LoadLibrary() to load a DLL, which calls
    > CreateThread(). This does NOT return NULL and I get a thread ID.
    > However, ThreadProc never gets executed when this code is in the DLL.
    > It works fine in the standalone EXE source.


    I see no mention of LoadLibrary, DLL, CreateThread, etc. in the C
    standard. I.E. this is off-topic in c.l.c. Follow-ups set.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Apr 8, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lamilla
    Replies:
    3
    Views:
    408
    Victor Bazarov
    Apr 7, 2005
  2. raj
    Replies:
    3
    Views:
    566
    Csaba
    Dec 14, 2005
  3. Jim Johnson

    CreateThread

    Jim Johnson, Mar 14, 2008, in forum: C++
    Replies:
    6
    Views:
    4,506
    James Kanze
    Mar 14, 2008
  4. Torsten Mohr

    Windows, CreateThread

    Torsten Mohr, Sep 7, 2009, in forum: Python
    Replies:
    0
    Views:
    372
    Torsten Mohr
    Sep 7, 2009
  5. KevinSimonson
    Replies:
    7
    Views:
    354
    Screamin Lord Byron
    Oct 18, 2010
Loading...

Share This Page