Thread: How to loadlibrary from a new thread

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    hellertown, pennsylvania
    Posts
    24

    How to loadlibrary from a new thread

    Hello everyone,

    I've searched the forum for an answer to my newbie question, but found nothing directly related to it...although lots of good stuff about DLL's.

    In my little program in the main() I successfully use LoadLibrary and GetProcAddress and call a couple of functions in a DLL. In short, the DLL works as such. Then, I CreateThread(...) and create a new thread. The function called by CreateThread attempts to do a LoadLibrary and GetProcAddress to the SAME DLL. Nothing happens. No error messages, no nothing. The function invoked by CreateThread gets to the line in which LoadLibrary is called and does nothing. Program exits without errors.

    The DLL itself has a BOOL WINAPI DllMain entry point with the "usual" DLL_PROCESS_ATTACH, etc. values in a switch block.

    It seems like what I'm trying to do should work, but it doesn't. I have zero tech support, and I'm out of ideas.

    Thanks for considering this problem.

    Regards to all,
    Mark Allyn

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Have you tried passing the address of the DLL that you got from the first LoadLibrary into the function that is being run by the new thread?

    DLLs are loaded into the address space of the entire process so you don't really need to call LoadLibrary again unless the main thread might unload the library before the second thread is done with it.

    Are you sure that the functions you're calling aren't working properly? Is there a reason that the program should stay open for longer than that?

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Isn't it a platform specific thread?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Moved to Windows Programming.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    What does GetLastError() return?

    Generally DDLs loaded at runtime (dynamically) should either;

    have global scope (loaded once and passed around as required)

    or

    loaded, used and released as required.

    This depends on the requirements of the app and the DLL.

    Also IIRC once a process has called LoadLibrary() on a particular DLL (and so 'mapped' it) the DLLMain() will not get called again by the same process (until the DLL is unmapped with a call to FreeLibrary() ).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  2. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  3. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 10:55 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM