afeijao
asked on
Redefinition of VirtualAllocEx
Hi,
I'm writing a program using Visual C++ 6.0 that needs to run in all Windows versions (from Win95 to XP).
Because some of the Kernel32 functions don't exist across all Windows versions I'm dinamically loading the functions using GetProcAddress().
I'm getting redefinition compile errors in functions that are already imported by the VC6.0 libraries.
As an example, if I want to use VirtualAllocEx() dinamically I use the following code:
typedef LPVOID (WINAPI *VIRTUALALLOCEX)(HANDLE, LPVOID, DWORD, DWORD, DWORD);
VIRTUALALLOCEX VirtualAllocEx;
VC compiler complains that VirtualAllocEx is redefined (error C2372).
Of course I could name the function differently but for consistency I would like to use the same name.
Is there a way to redefine imported functions (or undefine it from the import list) ?
I'm writing a program using Visual C++ 6.0 that needs to run in all Windows versions (from Win95 to XP).
Because some of the Kernel32 functions don't exist across all Windows versions I'm dinamically loading the functions using GetProcAddress().
I'm getting redefinition compile errors in functions that are already imported by the VC6.0 libraries.
As an example, if I want to use VirtualAllocEx() dinamically I use the following code:
typedef LPVOID (WINAPI *VIRTUALALLOCEX)(HANDLE, LPVOID, DWORD, DWORD, DWORD);
VIRTUALALLOCEX VirtualAllocEx;
VC compiler complains that VirtualAllocEx is redefined (error C2372).
Of course I could name the function differently but for consistency I would like to use the same name.
Is there a way to redefine imported functions (or undefine it from the import list) ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The namespace solution seems a good compromise.
The only problem is that it only works for C++.
Is there a way of using namespaces in C/SDK (which I'm using) without converting the whole project to C++ ?
The only problem is that it only works for C++.
Is there a way of using namespaces in C/SDK (which I'm using) without converting the whole project to C++ ?
Well, if you are compiling the C code AS C code, your only solution would be to mangle the names of your function pointers. C is for the most part, though, a subset of C++, and if you are compiling it as C++ code, which you should be able to get away with, you can add in namespaces, or any other C++ features as you see fit.
ASKER
I would like to keep my code strait C.
Do you know if there's a way to #undef a function ?
Do you know if there's a way to #undef a function ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(more information on winver at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/using_the_windows_headers.asp )
namespace afeijao {
typedef LPVOID (WINAPI *VIRTUALALLOCEX)(HANDLE, LPVOID, DWORD, DWORD, DWORD);
VIRTUALALLOCEX VirtualAllocEx;
// ... other declarations
}
At this point, it can all be referenced as "afeijao::VirtualAllocEx()
It might also be possible to confine all of microsoft's header files to their own namespace, as in
namespace MS {
#include <windows.h>
}
which should cause microsoft's VirtualAllocEx to be reached by MS::VirtualAllocEx();.
If you have your objects in one namespace, and microsoft's in another, you get to pick and choose with the using directive as follows:
namespace MS {
#include <windows.h>
}
namespace afeijao {
typedef LPVOID (WINAPI *VIRTUALALLOCEX)(HANDLE, LPVOID, DWORD, DWORD, DWORD);
VIRTUALALLOCEX VirtualAllocEx;
// ... other declarations
}
using MS::CreateThread;
using afeijao::VirtualAllocEx;
would result in CreateThread() using microsoft's header, and VirtualAllocEx() using your function pointer.
I hope I've been clear enough on this topic.
cplusplus.com has an easy to understand namespace tutorial at
http://www.cplusplus.com/doc/tutorial/tut5-2.html
MSDN's namespace documentation is more complete, albeit harder to navigate at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/tions_37.asp