CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Feb 2009
    Posts
    32

    [RESOLVED] LoadLibrary returns 0

    Hi, I'm trying to generate a DLL that is just a simple timer like routine for activating when a System DLG box pops up. I am unable to get anything but 0 from the library. I have checked the code, but I don't see the issue. It would be great if someone could point out where my issue lies. Here is the C++/H code.

    Code:
    // 
    #include "pch.h" // use stdafx.h in Visual Studio 2017 and earlier
    #include <utility>
    #include <Windows.h>
    #include <synchapi.h>
    #include "EPIQ Util.h"
    
    
    
    
    // DLL internal state variables:
    static BOOL bTimerOn;  // Turns on/off the timer
    static BOOL bRunning;  // Keeps us in the while loop
    static unsigned long ulWait; // How long to wait
    
    //void EPIQ_ExecuteTimer();
    //void EPIQ_SetTimer();
    //void EPIQ_init(const unsigned long);
    //void EPIQ_StopTimer();
    //void EPIQ_StartTimer();
    
    // Initialize the Timer variables
    void EPIQ_init(const unsigned long ullWait)
    {
        bTimerOn = FALSE;
        ulWait = ullWait;
    
        MessageBox(NULL, (LPCWSTR) "Made it here", (LPCWSTR) "We made it", NULL);
    
        return;
    }
    
    
    
    // Stop the timer from running
    void EPIQ_StopTimer()
    {
        bTimerOn = FALSE;
        Sleep(ulWait);
        return;
    }
    
    // Start the timer from running
    void EPIQ_StartTimer()
    {
        bTimerOn = TRUE;
        return;
    }
    
    //The actual process of the timer
    void EPIQ_TimerProc()
    {
    
        //create two structures to hold our Main Window handle
        //and the Button's handle
        HWND WindowHandle;
        HWND ButtonHandle;
    
    
        //this window's caption is "File Download", so we search for it's handle using the FindWindow API		
        WindowHandle = FindWindow(NULL, (LPCWSTR) "Message from webpage");
    
        SetForegroundWindow(WindowHandle);
    
        //the Button's Caption is "OK" and it is a "Button".  SPYXX.exe that comes with Microsoft Visual Studio will reveal this information to you
        ButtonHandle = FindWindowEx(WindowHandle, 0, (LPCWSTR) "Button", (LPCWSTR) "OK");
    
        //send a message to the button that you are "clicking" it.  Surprisingly C++ understands what BM_CLICK is without having to set it.  Different than VB
        SendMessage(ButtonHandle, BM_CLICK, 0, 0);
    
        return;
    }
    
    
    
    
    #pragma once
    // EPIQ_Util.h - Contains declarations of Utility Functions
    #pragma once
    
    #ifdef EPIQUTIL_EXPORTS
    #define EPIQUTIL_API __declspec(dllexport)
    #else
    #define EPIQUTIL_API __declspec(dllimport)
    #endif
    
    // The functions needed to interface with VBA EPIQ_Interface
    
    // This function must be called before any other function.
    extern "C" EPIQUTIL_API void EPIQ_init(const unsigned long);
    
    // Set how long to wait in ms.
    extern "C" EPIQUTIL_API void EPIQ_SetTimer();
    
    // Perform the TimerProc function.
    extern "C" EPIQUTIL_API void EPIQ_ExecuteTimer();
    
    // disable the timer.
    extern "C" EPIQUTIL_API void EPIQ_StopTimer();
    
    // Start the Timer
    extern "C" EPIQUTIL_API VOID EPIQ_StartTimer();
    
    extern "C" EPIQUTIL_API void EPIQ_TimerProc();
    
    //extern "C" EPIQUTIL_API HWND SetActiveWindow Lib "user32.dll" (HWND hwnd);
    
    int SetForegroundWindow (int hwnd);
    
    
    int SetActiveWindow(int hwnd);

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,902

    Re: LoadLibrary returns 0

    You don't say what is 0. However you're not checking the return value from FindWindow()/FindWindowEx() which are NULL if not found.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,855

    Re: LoadLibrary returns 0

    And shouldn't the OP's wide character strings be like this:-

    Code:
    L"Button"
    
    L"Message from webpage"
    etc ??
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Join Date
    Feb 2009
    Posts
    32

    Re: LoadLibrary returns 0

    Quote Originally Posted by 2kaud View Post
    You don't say what is 0. However you're not checking the return value from FindWindow()/FindWindowEx() which are NULL if not found.
    Sorry..... That is the return value from LoadLibrary. The library is not able to load the DLL, and I'm not seeing why. Any suggestions?

  5. #5
    Join Date
    Feb 2009
    Posts
    32

    Re: LoadLibrary returns 0

    Quote Originally Posted by John E View Post
    And shouldn't the OP's wide character strings be like this:-

    Code:
    L"Button"
    
    L"Message from webpage"
    etc ??
    Did not know that. Usually in the past, years and years ago, the test was cast to (LPCWSTR). I have not been in C++ since about 2000, so I'm a lot rusty. Been working mostly with VBA. Thanks for the information, will adapt as noted.

  6. #6
    Join Date
    Feb 2009
    Posts
    32

    Re: LoadLibrary returns 0

    Code:
         Dim ProcAdd As LongPtr
        Dim hinstLib As Long
        
     
        ' Get a handle to the DLL module.
            hinstLib = LoadLibrary("C:\Users\epperbx\source\repos\EPIQ Util\x64\Debug\EPIQ Util.dll")
    Any ideas why this would be returning 0 would be greatly appreciated. I'm at my witts end. I've tried hinstLib with LongPtr, VBA, as well and that did not make any difference.

    I just don't see the issue. I have generated a blank project and still get (0) from LoadLibrary.

  7. #7
    Join Date
    Feb 2009
    Posts
    32

    Re: LoadLibrary returns 0

    Quote Originally Posted by funkmonkey View Post
    Code:
         Dim ProcAdd As LongPtr
        Dim hinstLib As Long
        
     
        ' Get a handle to the DLL module.
            hinstLib = LoadLibrary("C:\Users\epperbx\source\repos\EPIQ Util\x64\Debug\EPIQ Util.dll")
    Any ideas why this would be returning 0 would be greatly appreciated. I'm at my witts end. I've tried hinstLib with LongPtr, VBA, as well and that did not make any difference.

    I just don't see the issue. I have generated a blank project and still get (0) from LoadLibrary.
    FINALLY got this to work. Now I'm having similar issues with GetProcAddress always returning a (0). Below is the function call. Anyone see anything I'm missing here in the call?

    Code:
        Dim ProcAdd As LongPtr
        Dim hinstLib As LongPtr
        Dim hFreeLib As LongPtr
        
     
        ' Get a handle to the DLL module.
            hinstLib = LoadLibrary("C:\Users\epperbx\source\repos\_EPIQ Util DLL_\Debug\EPIQ Util.dll")
     
            ' If the handle is valid, try to get the function address.
        
            
                ProcAdd = GetProcAddress(hinstLib, "EPIQ_Init")
    Any ideas will hopefully save me hours of hair pulling.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,902

    Re: LoadLibrary returns 0

    Shouldn't \ inside " be \\ ? It should be in c++ but vb??

    hinstLib = LoadLibrary("C:\\Users\\epperbx\\source\\repos\\_EPIQ Util DLL_\\Debug\\EPIQ Util.dll")
    Last edited by 2kaud; March 6th, 2025 at 08:24 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Nov 2018
    Posts
    152

    Re: LoadLibrary returns 0

    > Shouldn't \ inside " be \\ ?

    No idea.
    The OP seems to be wandering between C++ and VB on a per-post basis.

    > Now I'm having similar issues with GetProcAddress always returning a (0).
    Well call GetLastError.
    https://learn.microsoft.com/en-us/wi...i-getlasterror

    Then you might find something better to report than "it doesn't work".


    > ProcAdd = GetProcAddress(hinstLib, "EPIQ_Init")
    Yeah, Init is in lower case in your most recent C++ code.

    Unlike the rest of the functions which are EPIQ and then a capital.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,902

    Re: LoadLibrary returns 0

    You can also use DUMPBIN to see what actually is being exported in the .dll file:

    dumpbin /exports C:\Users\epperbx\source\repos\_EPIQ Util DLL_\Debug\EPIQ Util.dll
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Feb 2009
    Posts
    32

    Re: LoadLibrary returns 0

    Quote Originally Posted by 2kaud View Post
    Shouldn't \ inside " be \\ ? It should be in c++ but vb??

    hinstLib = LoadLibrary("C:\\Users\\epperbx\\source\\repos\\_EPIQ Util DLL_\\Debug\\EPIQ Util.dll")
    Thanks for the input. I did not have to do the double slashes to get the code to finally load the dll. Not sure what the fixe was, as I compiled multiple times after minor tweeks to try and get the library to load. I'm just glad the LoadLibrary function works. Now I get to fight with GetProcAddress. Since the loadlibrary is working I'm focusing in on the DLL side, only makes sense to me.

    Anything seen from the calling Code above that looks out of place? I'm off to fiddle with the DLL side to see what I can find as to why the function can't pull in the address of the function.

    Thanks for your input.

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,902

    Re: LoadLibrary returns 0

    Quote Originally Posted by funkmonkey View Post
    Thanks for the input. I did not have to do the double slashes to get the code to finally load the dll. Not sure what the fixe was, as I compiled multiple times after minor tweeks to try and get the library to load. I'm just glad the LoadLibrary function works. Now I get to fight with GetProcAddress. Since the loadlibrary is working I'm focusing in on the DLL side, only makes sense to me.

    Anything seen from the calling Code above that looks out of place? I'm off to fiddle with the DLL side to see what I can find as to why the function can't pull in the address of the function.

    Thanks for your input.
    As I mentioned above, first use DUMPBIN to see what is actually being exported from the dll
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    Feb 2009
    Posts
    32

    Re: LoadLibrary returns 0

    Quote Originally Posted by 2kaud View Post
    As I mentioned above, first use DUMPBIN to see what is actually being exported from the dll
    Thanks. I tried to run DUMPBIN in a Command Prompt but it is not recognized. Does it get run from Visual Studio? I have not come across it yet in my search. I'm using VS 2022.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,902

    Re: LoadLibrary returns 0

    See https://learn.microsoft.com/en-us/cp...?view=msvc-170 for info about dumpbin and how it's used.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,631

    Re: LoadLibrary returns 0

    Code:
    #ifdef EPIQUTIL_EXPORTS
    #define EPIQUTIL_API __declspec(dllexport)
    #else
    #define EPIQUTIL_API __declspec(dllimport)
    #endif
    My best guess is EPIQUTIL_EXPORTS is not defined when the DLL gets built, thus no exporting occurs.
    Best regards,
    Igor

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured