How to conditional call a library
-
Hi all, I have a program that calls magnification.lib in windows 7, windows 8. (I'm calling #include magnification.h) But we know this library doesn't exist in windows XP. How can I ignore this library when opening the application in windows XP?
-
Hi all, I have a program that calls magnification.lib in windows 7, windows 8. (I'm calling #include magnification.h) But we know this library doesn't exist in windows XP. How can I ignore this library when opening the application in windows XP?
Create function pointers in a header file for all the magnification APIs that you're going to use. Check the OS version and if Vista or above, call
LoadLibrary
onMagnification.dll
. After this callGetProcAddress
on the function to initialize the function pointer. So the header file could look like this -typedef BOOL (*PFN_MAG_INITIALIZE)(void);
From the main program, you could have a call to some function called
InitMagnification
. In theInitMagnification
function, do the dynamic loading after checking the OS version.PFN_MAG_INITIALIZE MagInitialize = NULL;
if (osVersion >= "VISTA") // Consider this as pseudo-code
{
HMODULE hmod = LoadLibrary(_T("Magnification.dll"));MagInitialize = GetProcAddress(hmod, "MagInitialize");
}
In the source where this function is called, do the checking as follows -
if (MagInitialize)
{
MagInitalize();
}I have omitted error checking, which you have to do.
«_Superman_» _I love work. It gives me something to do between weekends.
-
Hi all, I have a program that calls magnification.lib in windows 7, windows 8. (I'm calling #include magnification.h) But we know this library doesn't exist in windows XP. How can I ignore this library when opening the application in windows XP?
thanh_bkhn wrote:
But we know this library doesn't exist in windows XP.
How can I ignore this library when opening the application in windows XP?Sounds like you need to read up on implicit vs. explicit linking.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Create function pointers in a header file for all the magnification APIs that you're going to use. Check the OS version and if Vista or above, call
LoadLibrary
onMagnification.dll
. After this callGetProcAddress
on the function to initialize the function pointer. So the header file could look like this -typedef BOOL (*PFN_MAG_INITIALIZE)(void);
From the main program, you could have a call to some function called
InitMagnification
. In theInitMagnification
function, do the dynamic loading after checking the OS version.PFN_MAG_INITIALIZE MagInitialize = NULL;
if (osVersion >= "VISTA") // Consider this as pseudo-code
{
HMODULE hmod = LoadLibrary(_T("Magnification.dll"));MagInitialize = GetProcAddress(hmod, "MagInitialize");
}
In the source where this function is called, do the checking as follows -
if (MagInitialize)
{
MagInitalize();
}I have omitted error checking, which you have to do.
«_Superman_» _I love work. It gives me something to do between weekends.
Thank you very much. After posting this question, I did exactly what you told me to do. Thank you again
-
thanh_bkhn wrote:
But we know this library doesn't exist in windows XP.
How can I ignore this library when opening the application in windows XP?Sounds like you need to read up on implicit vs. explicit linking.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
Thank you. I performed an explicitly link to my DLL.
-
Hi all, I have a program that calls magnification.lib in windows 7, windows 8. (I'm calling #include magnification.h) But we know this library doesn't exist in windows XP. How can I ignore this library when opening the application in windows XP?
As an alternative, you could use delay load dll. In this case you do not need to change the code you have already written (related to statically linking to the library), all you need to do is just do not call chunk of codes related to magnification at all. Saves you a lot of annoying LoadLibrary/GetProcAddress calls. However, it looks like you have already implemented it. So, just learn about this, so that you could use in future
-
As an alternative, you could use delay load dll. In this case you do not need to change the code you have already written (related to statically linking to the library), all you need to do is just do not call chunk of codes related to magnification at all. Saves you a lot of annoying LoadLibrary/GetProcAddress calls. However, it looks like you have already implemented it. So, just learn about this, so that you could use in future
Thank you for introducing me this. I found this way is more interesting than using LoadLibrary, but I still prefer to the old method, because it helps me to compile the program even on Windows XP