Skip to main content
gamedev.net
🔒 Locked

Loading a DLL

Started by Dante77 Jan 2, 2003 at 4:36 PM 10 replies 750+ views
Original Post
Dante77
Dante77
I''ve got problem with loading a DLL library. Simply I use this: HINSTANCE hDll; hDll = LoadLibrary ("C:\\OpenGl.DLL"); // code GetAPI = (GetAPI)GetProcAddress (hDll, "GetAPI"); But LoadLibrary always returns 0x10000000 and if I query last error with GetLastError it''s error 6, defined as: "6 The handle is invalid. ERROR_INVALID_HANDLE" What is this supposed to be?? Path is correct, DLL file too. Thanx for replies Dante
Dante77
LessBread
LessBread
Most times the path isn''t needed, just the name of the dll.

Here is the sequence of search directories used by LoadLibrary to find the specified library:

quote:

1. The directory from which the application loaded.
2. The current directory.
3. Windows 95/98/Me: The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
Windows NT/2000 or later: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is SYSTEM32.

4. Windows NT/2000 or later: The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is SYSTEM.
5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
6. The directories that are listed in the PATH environment variable.



I would try using the string "OpenGl32.dll" instead. Afaik, opengl.dll hasn''t shipped with Windows in years.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Dante77
Dante77
Yeah, I know the searching process of the LoadLib. fnc, but I don''t use the Winows'' OpenGL.dll or OpenGL32.dll lib. I use my own lib (here called OpenGL.dll). Just to be sure that Windows aren''t getting confused (OpenGL.dll system lib), I used another name for my lib.
It still doesn''t work. I don''t know what this s**t it means... Theres some another problem when it says "invalid handle"...
Dante77
dalleboy
dalleboy
quote:
Original post by Dante77
But LoadLibrary always returns 0x10000000

So what is the problem?

MSDN on LoadLibrary says:
"If the function fails, the return value is NULL. To get extended error information, call GetLastError."

The function is not returning NULL, so it should be ok, should it not?



Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Metal Typhoon
Metal Typhoon
quote:
Original post by Dante77
I''ve got problem with loading a DLL library. Simply I use this:

HINSTANCE hDll;
hDll = LoadLibrary ("C:\\OpenGl.DLL");
// code
GetAPI = (GetAPI)GetProcAddress (hDll, "GetAPI");

But LoadLibrary always returns 0x10000000 and if I query last error with GetLastError it''s error 6, defined as:

"6 The handle is invalid. ERROR_INVALID_HANDLE"

What is this supposed to be??
Path is correct, DLL file too.

Thanx for replies
Dante


what are your checking against ? if it is the library :
if (!hDll)
MessageBox (NULL,"Error","Error",MB_OK);

NOW i need to see some more codes for the GetAPI .. what is te typedef and how is it defined on the dll ? or are you using a .DEF ?

Metal Typhoon
Dante77
Dante77
OK OK, I''ll post smething more, but I got idea... I''ll have to try...
Dante77
Dante77
Dante77
dalleboy :
Hmm... so maybe I forgot ''bout LoadLibrary... it returns 0x10000000 always, but what that GelLastError code?? I''m just curious ''bout that.

Metal Typhoon:
Hmm... no matter what is the GetAPI declared either in .EXE or .DLL, GetProcAddress ALWAYS returns NULL or 0.


IN .EXE GetAPI is:
funcexport_t (*GetAPI) (funcimport_t fimp)

...and in .DLL:
funcexport_t GetAPI (funcimport_t fimp)
Dante77
AndyGeers
AndyGeers
I had this problem myself a long while ago... For me, the problem was that although you called your function "GetAPI", that''s not what it''s referred to internally, Windows will have garbled it up and added some extra info about parameter types. If I recall, I had to use some kind of command line program that shipped with Visual Studio that would list all of the functions inside a DLL with their original name and the garbled Windows version, and then you use that in your call to GetProcAddress instead.

Hope that was helpful,
Andy
dalleboy
dalleboy

  
// In DLL:

extern "C" __declspec(dllexport) int __cdecl MyFunc(int parm1);

// In EXE:

typedef int (__cdecl *MyFunc_ptr)(int parm1);
MyFunc_ptr MyFunc = (MyFunc_ptr) GetProcAddress(hDll, "MyFunc");
std::cout << "MyFunc(47) = " << MyFunc(47);




Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Metal Typhoon
Metal Typhoon

try this ->

quote:
Original post by dalleboy


    
// In DLL:

extern "C" __declspec(dllexport) int MyFunc(int parm1);

// In EXE:

typedef int (*MyFunc_ptr)(int parm1);
MyFunc_ptr MyFunc = (MyFunc_ptr) GetProcAddress(hDll, "MyFunc");
std::cout << "MyFunc(47) = " << MyFunc(47);




Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''



Metal Typhoon
Metal Typhoon
LessBread
LessBread
I should have spotted this earlier, but oh well.

LoadLibrary returns a handle to the module - that is, a pointer to the memory address that the target file has been mapped to. Thus, 0x10000000 is a valid return value for LoadLibary. Try dumping the first few bytes from that pointer to stdout. I''ll bet the first two bytes are "MZ" - 0x4D 0x5A 0x90 0x00.

Some code like this ought to do the trick:

printf("%08X", *hDll); // dereference the pointer to get at what it points to

As others have pointed out, the trouble more likely has to do with GetProcAddress and name mangling etc.



"Beautiful maiden," answered Candide, "when a man is in love, is jealous, and has been flogged by the Inquisition, he becomes lost to all reflection."
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.