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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WIN32 API wrapper classes

Status
Not open for further replies.

sbayeta

Programmer
Apr 4, 2003
13
0
0
AR
Hi,

I wrote a little set of WIN32 API wrapper C++ classes, just as a way to learn the API and to maintain my code organized.

My Window class is supposed to be used like this:

Code:
#include <windows.h>
#include "wrappers.h"

Window *wn;
ProcedureBag *pb;
Button *bt;

void on_create(WPARAM, LPARAM);
void on_destroy(WPARAM, LPARAM);
void on_command(WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
	pb = new ProcedureBag();
	pb->addProcedure(WM_CREATE, on_create);
	pb->addProcedure(WM_DESTROY, on_destroy);
	pb->addProcedure(WM_COMMAND, on_command);
	wn = new Window(hInst, "My window", pb);
	return wn->processEvents();	//this function enters the messageloop
}


void on_create(WPARAM wParam, LPARAM lParam) {
	//do something
}

void on_destroy(WPARAM wParam, LPARAM lParam) {
	//do something
}

void on_command(WPARAM wParam, LPARAM lParam) {
	//do something
}

The ProcedureBag class is basically a wrapper for an STL map, holding (message-procedure) pairs. It only has two methods:

Code:
/*
 * Add a procedure to the bag, associating it to a particular message
 */
void ProcedureBag::addProcedure(UINT msg, PROCPTR procedure) {
	(*bag)[msg] = procedure;
}

/*
 * Executes the procedure associated to msg, passing to it wParam and lParam
 */
bool ProcedureBag::executeProcedure(UINT msg, WPARAM wParam, LPARAM lParam) {
	if(bag->find(msg) != bag->end()) {
		(*bag)[msg](wParam, lParam);
		return true;
	}
	return false;
}

The latter is called by the Window class in its window procedure, shown below:

Code:
LRESULT CALLBACK Window::WndProc(HWND hWnd, UINT msg, WPARAM wParam,LPARAM lParam) {
/*
	if(pBag->executeProcedure(msg, wParam, lParam)) return 0;
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

The problem is that I have to declare Window::WndProc as static in order for the file to compile (I don't know why), and hence I also need to declare pBag (a private ProcedureBag in Window class) also as static. And when I do this, the program compiles but doesn't link because of an unresolved reference to this pBag.

I'd like to make Window::WndProc an instance method and pBag an instance variable. How can I do this ?

If you need more code tell me and I'll post it.

Thanks in advance.

Any other suggestion about the approach I'm taking will be appreciated.
 
What was the error that you got when you compiled with win main as non-static? Was it a compiler or a linker error?

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
You forget C++ rule: must define (not only declare) all static data members. Add Window::pBag = 0; definition in this class implementation file.

It seems it's not the best idea to wrap STL containers (designed for direct using). MFC and WTL present Win API wrappers (at least for GUI area)...
 
The window procedure needs to be static because the Win API is C-based, not C++. How about defining a global STL map which keeps track of every window instance and ties the HWND handles to their C++ objects? Then have one global window procedure, which uses the HWND argument to look up the corresponding C++ object, and forward the other arguments to a method of that object to deliver its message.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top