[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);
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.
Re: LoadLibrary returns 0
And shouldn't the OP's wide character strings be like this:-
Code:
L"Button"
L"Message from webpage"
etc ??
Re: LoadLibrary returns 0
Quote:
Originally Posted by
2kaud
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?
Re: LoadLibrary returns 0
Quote:
Originally Posted by
John E
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.
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.
Re: LoadLibrary returns 0
Quote:
Originally Posted by
funkmonkey
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.
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")
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.
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
Re: LoadLibrary returns 0
Quote:
Originally Posted by
2kaud
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.
Re: LoadLibrary returns 0
Quote:
Originally Posted by
funkmonkey
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
Re: LoadLibrary returns 0
Quote:
Originally Posted by
2kaud
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.
Re: LoadLibrary returns 0
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.
Re: LoadLibrary returns 0
Quote:
Originally Posted by
funkmonkey
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.
Tripped across your other post in C++ & Win API, where the code looks more complete. Have to admit, I cannot see a timer thing in there. Instead, I can see you have absolutely no idea what your EPIQ_ExecuteTimer() really does. You'd better stop fumbling with DLL and pay some attention to your main logic of timed execution, for whatever reason you're up to..
Re: LoadLibrary returns 0
Re: LoadLibrary returns 0
Quote:
Originally Posted by
Igor Vartanov
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.
Thanks. What would the define look like? Using _stdcall? as the definition? In all the examples I have looked at this is how the code has been and I have not seen a defenition for the maco. Unless it's in another file or they just did not post it with the question/answer.
Re: LoadLibrary returns 0
Actually I do know. ExecuteTimer is the equivilent of set timer, but very crude. It is a circular call that is simple and calls the EPIQ_TimerProc every second from the Sleep Delay. I figured this was easier than generating a thread with a timer. It's a really simple/stupid form of a timer. It may be crude but it does work and the only reason I need it is to press a system dialog button when obj.click function is called as this function freezes until the system dialog box is dismissed then it picks back up and continues to the next routing.
Re: LoadLibrary returns 0
Quote:
Originally Posted by
Igor Vartanov
Tripped across your
other post in C++ & Win API, where the code looks more complete. Have to admit, I cannot see a timer thing in there. Instead, I can see you have absolutely no idea what your EPIQ_ExecuteTimer() really does. You'd better stop fumbling with DLL and pay some attention to your main logic of timed execution, for whatever reason you're up to..
DIdn't reply with quotes so here is what I said from below.
Actually I do know. ExecuteTimer is the equivilent of set timer, but very crude. It is a circular call that is simple and calls the EPIQ_TimerProc every second from the Sleep Delay. I figured this was easier than generating a thread with a timer. It's a really simple/stupid form of a timer. It may be crude but it does work and the only reason I need it is to press a system dialog button when obj.click function is called as this function freezes until the system dialog box is dismissed then it picks back up and continues to the next routing.
But hey, let's insult the person because you can't figure it out with a circular operation.
Re: LoadLibrary returns 0
Quote:
Originally Posted by
Igor Vartanov
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.
Thanks. Your are probably correct as I don't define that until it is defined in the ifdef statement. Should I be using __Stdcall or __cdecl with the macro?
The LoadLibrary issue is somewhat resolved and now working on a similar issue with getprocaddress. The one issue I had is that the DLL will only be loaded in the debug directory, which is fine for now but will need to resolve this later after the code is more functional. Can't place the DLL in the system directory as this is for a company team project for processing records using VBA but with the C developed DLL for pressing a system dialog button in order to move the program on as the program freezes until the dismissal of the system dialog box occurs.
Moderators...... I guess you can close this post as I am no longer fighting a LoadLibrary issue but a GetProcAddress issue which is a different topic and I do have another thread for this. Thanks.
Re: LoadLibrary returns 0
Quote:
Originally Posted by
funkmonkey
Thanks. What would the define look like? Using _stdcall? as the definition? In all the examples I have looked at this is how the code has been and I have not seen a defenition for the maco. Unless it's in another file or they just did not post it with the question/answer.
In the C++ code that generates the .dll (exports) you have at the top:
Code:
#define EPIQUTIL_EXPORTS
In the code that uses the .dll (imports) you don't have this definition.
Re: LoadLibrary returns 0
Quote:
Originally Posted by
Igor Vartanov
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.
For the LoadLibrary issue it was that I moved the DLL to another directory, but even with the path defined LoadLibrary would not load the DLL. For now LoadLibrary is not an issue and I will figure out this problem later, as I just leave the DLL where it was compiled and LoadLibrary works. Scratching my head over that one though since the DLL can't seem to be placed elsewhere. Moving on to my other issue now GetProcAddress, with the same results but the DLL seems to not be applying __declspec(dllexport) macro for the compilation.
Re: LoadLibrary returns 0
Moderators, you may close this post as the LoadLibrary issue is sort of resolved. Will visit the issue at a later date as in the DLL debug location the DLL works, but not in another location. Will resolve, attempt, at a later date.