Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run-time DLL linking

Status
Not open for further replies.

AmkG

Programmer
May 12, 2001
366
PH
Okay, I need a certain API call that exists in Windows98 or later. Unfortunately some of my users only have Windows95. This particular API is in user32.dll of Win98. I have an alternative procedure for Windows95 but it won't be quite as pretty. Since prettiness counts for my users, and compatibility counts for maybe twice as much, I want to have the prettier solution on Win98 and a compatible solution on Win95. Now, my question is, would it be possible to link to a Windows-provided API routine using LoadLibrary() and GetProcAddress()? That way I can determine if I have Windows98 or not, use the API on Win98 or later, and use my ugly alternative on Win95.

Code:
if(hLibrary=(LoadLibrary("user32.dll")!=NULL)
    {
    if((ProcAddress=
      GetProcAddress(hLibrary,"Win98orLaterProc"))
       !=NULL)
        (ProcAddress)();
    FreeLibrary(hLibrary);
    }

And please, please, please, I don't want to answer my own questions again.... "Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
This will work but it would be better to check the operating system version first using GetVersionEx. For Win9x, you need to use the OSVERSIONINFO structure, not the OSVERSIONINFOEX as it isn't supported for these OS.

For more information, see the Platform SDK.
 
Well I'm more interested in whether or not the procedure actually exists, not the version, as long as the procedure is there I couldn't care less about the version... "Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
The loadLibrary followed by GetProcAddress does work. But there are some things to keep in mind:
1. If you use the frequently used libraries like advapi32.lib, kernel32.lib, user32.lib, ..., they are almost certainly already loaded in the processes address space. So I prefer first using GetModuleHandle ( "user32" ), and use LoadLibrary only when GetModuleHandle fails.
2. Many functions do not exist as the names they are known best. They exist two times, one followed by an 'A' (ANSI-version) and one followed by a 'W' (Unicode version). Under normal circumstances the C-compiler (this is done by macro's in the include files) chooses which one to use. So it might be, "Win98OrLaterProc" is not in the library, but Win98OrLaterProcA" and "Win98OrLaterProcW" are. If you are going to use GetProcAddress, you should specify the real name, so Win98OrLaterProcA or Win98OrLaterProcW.

Marcel

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top