CLI and RegisterClassEx?

I need to create a window with a message pump, accessible from a managed class.
I can create the window, but thanks to WNDCLASSEX.lpfnWndProc requiring an unmanaged static function pointer, I'm having trouble figuring out how to set it up to where the callback function can access members in the managed class.

The way I did this before in unmanaged code was via passing a pointer to the class in the lParam parameter of the CreateWindowEx function, then casting it into the class from within the WndProc callback, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
	{
		static MyClass* app = NULL;
		switch( msg )
		{
			case WM_CREATE || WM_NCCREATE:
			{
				// Get the 'this' pointer we passed to CreateWindowEx via the lpParam parameter.
				CREATESTRUCT* cs = (CREATESTRUCT*)lParam;
				app = (MyClass*)cs->lpCreateParams;
				return 0;
			}
		}

		// Don't start processing messages until after WM_CREATE.
		if( app )
			return app->WindowProc(msg, wParam, lParam);
		else
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}


... However, this doesn't seem to work in managed code, since AFAIK you can't cast an instance of the managed class into an LPVOID to pass to the callback (and even if you could, you can't cast it into a managed object from an unmanaged static function).

So what can I do to get around this barrier? The entire purpose of my project is to provide a simple .NET interface to C# and VB users which simplify many Direct3D concepts. I'm trying pretty damn hard to not have to force them to use P/Invoke statements on my library.

Many thanks to any and all advice!
Very few of the regulars know CLI here. You'll get a faster answer if you ask @ the MSDN forums.
Thanks! To anyone stumbling across this post in search of the same answer, do a little research on 'gcroot' and use it in a native class, which your managed class can access.
Topic archived. No new replies allowed.