Click to See Complete Forum and Search --> : Encapsulating Win32 API in a namespace


ErikM
July 22nd, 2003, 12:15 AM
Hi all,

I was wondering if it would be possible to encapsulate the Win32 API in its own namespace, and how one might go about doing this. I think it would be a lot cleaner, and somewhat closer to object oriented than the way it's currently set up. I'm not an expert at this stuff, and am not sure if it could be done with the current form of the Win32 API.

To make this a little clearer, instead of calling
hWnd = CreateWindowEx(...)
I'd call
hWnd = win32::CreateWindowEx(...)

or I could do something like


using namespace win32;
hWnd = CreateWindowEx(...);


Thoughts?

Erik

AvDav
July 22nd, 2003, 12:00 PM
Hello.

namespace win32
{
#include <windows.h>
//all possible includes
};

int WINAPI WinMain(win32::HINSTANCE, win32::HINSTANCE, int, win32::LPSTR)
{
return 0;
}

But is it comfortable?

galathaea
July 22nd, 2003, 05:58 PM
The namespace'd #include posted by AvDav is the standard way for getting the Win32 API encapsulated in a namespace. It can be useful if one has multiple defines of the same APIs, for example when one has MFC, platform SDK, and DDK definitions (which may differ in the types declared as the parameters) included into the same project. Then one can have using declarations, like

using win32::GetModuleFileName;

or using directives

using namespace ddk;

as needed in the cpps to differentiate the desired functionality. Gary Nebbett uses this technique quite nicely in his Windows NT/2000 Native API Reference to differentiate his homespun headers from the DDK and other headers one might include into projects using his code.

ErikM
July 23rd, 2003, 10:03 AM
Thanks AvDav, that is exactly what I had in mind.

galathaea - Yes, that is pretty much what I was thinking it would be useful for. For example, if I want to use a bunch of functions that sound like Win32 API calls (CreateWindow, etc), but only want to use one or two actually WinAPI function calls.

Thanks again,
Erik