• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

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) ?
0
afeijao
Asked:
afeijao
  • 4
  • 2
2 Solutions
 
nonubikCommented:
>Because some of the Kernel32 functions don't exist across all Windows versions
You can use '#ifdef' statements corresponding to windows platform versions in order not to get compile errors.
0
 
WKalataCommented:
I believe MSVC++ 6 supports namespaces, and I think you have the perfect situation to apply them.  Encase all of your declarations in their own namespace, as in

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()" after you set the function pointer to the correct address.

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
0
 
afeijaoAuthor Commented:
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++ ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
WKalataCommented:
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.
0
 
afeijaoAuthor Commented:
I would like to keep my code strait C.

Do you know if there's a way to #undef a function ?
0
 
WKalataCommented:
Not really, unless you'd like to modify header files.  If you're only adding function pointers that exist in versions of windows ABOVE NT 4 / WIN95, but not in NT 4 / WIN95, you can add the line
#define WINVER 0x0400
directly before any line including a windows header file.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now