## Recommended Posts

Hello all, I got a problem with LoadLibrary(...). I created a test DLL placed it in some map, use LoadLibrary with the absolute pathname as argument. The function return 0x10000000. Oke this means no good. GetLastError() return code '6' : The handle is invalid : ERROR_INVALID_HANDLE. I do not know what I should be looking for now, or if this is a common problem, but can anyone help me with this. Thank you, Xeile

##### Share on other sites
0x10000000 is a valid return value for LoadLibrary(). LoadLibrary() return 0 on error.

##### Share on other sites
Quote:
 Original post by SiCrane0x10000000 is a valid return value for LoadLibrary(). LoadLibrary() return 0 on error.

Maybe the handle is valid, but when I do this:
DWORD Error = 0;HMODULE hLib = NULL;Error = GetLastError();   // Error is still equal to 0hLib = LoadLibrary("C:\\Test.dll");Error = GetLastError();   // Error is now 6, hLib = 0x10000000

It seems that there is something wrong here, what could it be then?

##### Share on other sites
Quote:
 DWORD Error = 0;HMODULE hLib = NULL;Error = GetLastError(); // Error is still equal to 0hLib = LoadLibrary("C:\\Test.dll");Error = GetLastError(); // Error is now 6, hLib = 0x10000000

Not sure why that occurs. There are some Win32 API functions that call SetLastError even when they succeed but I don't think LoadLibrary is one of them (it's docs say it is not) but maybe it does... Either way the handle should be valid to use. Try it out.

Mike.

##### Share on other sites
The value of GetLastError is meaningless until an error actaully happens. From what you've said no error is happening, thus you should not be calling GetLastError at all.

To speculate on why you're seeing ERROR_INVALID_HANDLE, I would guess that internally LoadLibrary is doing a bunch of checks of registry values, dependent library loading, yadda, yadda, yadda, some of which fails for whatever reason but doesn't actually prevent the library from being loaded.

##### Share on other sites
Quote:
 Original post by Anon MikeThe value of GetLastError is meaningless until an error actaully happens. From what you've said no error is happening, thus you should not be calling GetLastError at all.To speculate on why you're seeing ERROR_INVALID_HANDLE, I would guess that internally LoadLibrary is doing a bunch of checks of registry values, dependent library loading, yadda, yadda, yadda, some of which fails for whatever reason but doesn't actually prevent the library from being loaded.

Oke, I'll assume 0x10000000 is a valid handle. The next step I took, was with GetProcAddress;

I made a simple dll seen here:
// Test.c - Source for Test.dll__declspec(dllexport) int nTest = 12345;// End Test.c - Source for Test.dll

Now here is my program source:

// RT Library Link example.c - Source for execute#include "Windows.h"int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR CmdLine, int CmdShow){        SetLastError(0);  // Making sure 	DWORD Error = GetLastError(); // Indeed 0	HMODULE hLib = LoadLibraryEx("C:\\Test.dll", NULL, DONT_RESOLVE_DLL_REFERENCES); // Value is 0x10000000	Error = GetLastError(); // With use of Extended function with DONT_RESOLVE_DLL_REFERENCES still 0	FARPROC Proc = GetProcAddress(hLib, "nTest"); // Proc = 0x00000000	Error = GetLastError(); // 127; The specified procedure could not be found; ERROR_PROC_NOT_FOUND 	int i = (int)*Proc; // 0		FreeLibrary(hLib); // Returned true	return 0;}

Assuming that LoadLibrary succeeded, GetProcAddress isn't able to find the variable "nTest". What could be wrong here, the dll has been found else LoadLibrary would return 0x00000000 instead of 0x10000000.
What I'm doing wrong here?

Thanks,

Xeile

##### Share on other sites
Try exporting a function instead of an int. Once you get that working, then fuss with data. At any rate, there's no point to checking the last error value when the function returns an acceptable value. LoadLibrary If the function succeeds, the return value is a handle to the module. If the function fails, the return value is NULL. To get extended error information, call GetLastError.

DWORD Error = 0;HMODULE hLib = NULL;hLib = LoadLibrary("C:\\Test.dll");if ( NULL == hLib ) {  Error = GetLastError();}

##### Share on other sites
I now know what I did wrong:

In the dll sample code, the line:
__declspec(dllexport) int nTest = 12345;

should have been:
extern "C" __declspec(dllexport) int nTest = 12345;

Anyway, thanks for all the help.

Xeile

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627694
• Total Posts
2978673

• 19
• 14
• 12
• 10
• 12