Sign in to follow this  

Problems with LoadLibrary(...)

This topic is 4389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
0x10000000 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 0
hLib = 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 this post


Link to post
Share on other sites
Quote:

DWORD Error = 0;
HMODULE hLib = NULL;

Error = GetLastError(); // Error is still equal to 0
hLib = 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Anon Mike
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.


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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

This topic is 4389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this