Expose Private Window Handle From Class

I am finishing up work on a RenderWindow system for a project I am working on, but sadly have hit some problems.

I have an abstracted window creation system that has private variables, more specifically the handle to the window, which is needed for initializing Direct3D9. My problem is that I need the window handle to initialize systems, however the expose function I wrote does not want to work.

1
2
3
4
HWND RenderWindow::ExposeHandle()
{
    return hWndMain;
}



When I call the fucntion (something like HWND hWnd = rw.ExposeHandle()) the debugger tells me the address of the variable is 0xcccccc{unused}. Does anyone know how I can expose the private window handle, so that I may use it to initialize stuff like DirectX?

Thanks,
Strych.
How are you initializing hWndMain?

Memory full of cccccc-s suggests that you've created an instance of the RenderWindow class but that its hWndMain member has not been set to any particular value.
Last edited on
Hmm, that is weird, but I initialize like:
1
2
RenderWindow rw;
HWND hWnd = rw.ExposeHandle();


I then try and pass the handle into my initialize function for DirectX, but it fails. What is weird is the window shows so I know for a fact there is something stored in that handle. What I do not get though is that "in theory" I should be able to expose the handle this way and use it. What is even weirder is no matter what method I try (making it public, etc...) it always comes out to that same value in the debugger.
Last edited on
How are you defining your constructor?
I have the defined, but as of right now they do nothing. I have one method that sets the parameters and one that creates the window and shows it.

HEADER
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef _RENDERWINDOW_H_
#define _RENDERWINDOW_H_

#include <Windows.h>
#include <string>
#include "SexyDefs.h"

//@TODO: Impliment messageproc override system.



class SEXYAPI RenderWindow
{
        private:	
                LPCTSTR lpClassName; //this is our classname
                WNDCLASSEX wcx; //our windows class
				HWND hWndMain;//handle to our main window
				HINSTANCE hInstMain;//main application handle
                HINSTANCE hPrevInst;//previous application handle

                
        public:
                bool KeyPressed; //true if a key was pressed
                std::string LastKeyPress;

                //mouse
                bool RightClick; //mouse was right clicked
                bool LeftClick;  //mouse was left clicked


				/**
				* Initialize a window.  Set all settings.
				* ----------------
				* @PARAM:
				*	HINSTANCE: Handle to current instance of the application.
				* @PARAM:
				*	HINSTANCE: Handle to previous instance of the application.
				* @PARAM:
				*	LPSTR: Command line.
				* @PARAM:
				*	int: Show the command line.
				* @PARAM:
				*	UINT: Window style.
				* @PARAM:
				*	LPCTSTR: Class name.
				*/
                bool SetupRenderWindow(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd, 
					WNDPROC msgproc,UINT Style,LPCTSTR lpszClassName); 

				/**
				* Take the set parameters and create/show the window.
				* ----------------
				* @PARAM:
				*	DWORD: Window style.
				* @PARAM:
				*	LPCTSTR: Name of window.
				* @PARAM:
				*	int: Width of window.
				* @PARAM:
				*	int: Height of window.
				*/
                bool RenderWindow::CreateRenderWindow(DWORD dwStyle,LPCTSTR lpWindowName,int iWidth,int iHeight); 
                
				/**
				* Expose the private window.
				*/
				HWND ExposeHandle();
			

				/**
				* Default message proc for the render window.
				* ----------------
				* @NOTE: 
				*	This must be declared as STATIC. THIS IS VERY IMPORTANT>
				* @Param:
				*	HWND: Handle to window.
				* @PARAM:
				*	UINT: Message to be switched.
				* @PARAM:
				*	WPARAM: Extra info.
				* @PARAM:
				* LPARAM: Extra info.
				*/
                static LRESULT CALLBACK MyWindowProc(HWND hWndMain,UINT uMsg,WPARAM wParam,LPARAM lParam); 

               /**
			   * Constructor.
			   */
				RenderWindow(){}

				/**
				* Destructor.
				*/
				~RenderWindow(){}                
};

#endif //_RENDERWINDOW_H_ 
SOURCE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "RenderWindow.h"
#include "BuildConfig.h"

/*=================================================
	Global Variables.
=================================================*/
RenderWindow *MyRenderWindow = NULL;


/*=================================================
	bool RenderWindow::SetupRenderWindow(HINSTANCE hInstance,
		HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
		int nShowCmd, UINT Style, LPCTSTR lpszClassName)
	----------------
	Set up a render window.  Set all of the settings
	and get it ready to be created and shown.
=================================================*/
bool RenderWindow::SetupRenderWindow(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
	int nShowCmd, WNDPROC msgproc, UINT Style, LPCTSTR lpszClassName)
{
        hInstMain = NULL;
        hPrevInst = NULL;
        hWndMain = NULL;

        hInstMain = hInstance;
        hPrevInst = hPrevInstance;
        RightClick = false;
        LeftClick = false; 

		RenderWindow::lpClassName = lpszClassName;

		ZeroMemory(&wcx, sizeof(WNDCLASSEX));

        wcx.cbSize					= sizeof(WNDCLASSEX);
        wcx.style					= Style;
		//If this build option has been defind in the options use the msg
		//proc that is passed into the function.  Otherwise use the default one.
#if defined(OVERRIDE_DFLT_MSG_PROC)
        wcx.lpfnWndProc				= msgproc;
#else
		wcx.lpfnWndProc				= MyWindowProc;
#endif
        wcx.cbClsExtra				= 0;
        wcx.cbWndExtra				= 0;//window extra
        wcx.hInstance				= hInstMain;//application handle
        wcx.hIcon					= LoadIcon(NULL,IDI_APPLICATION);//icon
        wcx.hCursor					= LoadCursor(NULL,IDC_ARROW);//cursor
        wcx.hbrBackground			= (HBRUSH)GetStockObject(WHITE_BRUSH);//background color
        wcx.lpszMenuName			= NULL;//menu
        wcx.lpszClassName			= lpszClassName;//class name
        wcx.hIconSm					= NULL;//small icon

        //register the window class, return 0 if not successful
        if(!RegisterClassEx(&wcx)) return(false);

        MyRenderWindow = this;
        return true;
}

/*=================================================
	bool RenderWindow::CreateRenderWindow(DWORD dwStyle,
		LPCTSTR lpWindowName, int iWidth, int iHeight)
	----------------
	Take the window creation paramters and create
	a render window.
=================================================*/
bool RenderWindow::CreateRenderWindow(DWORD dwStyle, LPCTSTR lpWindowName, int iWidth, int iHeight) 
{
        RenderWindow::hWndMain = CreateWindowEx(NULL,
                RenderWindow::lpClassName,
                lpWindowName,
                dwStyle,
                0, 0, iWidth, iHeight,
                NULL,
                NULL,
                hInstMain,
                NULL);

        if(!RenderWindow::hWndMain) return false;

        return true;
}


/*=================================================
	HWND RenderWindow::ExposeHandle()
	----------------
	Expose the private window handle
=================================================*/
HWND RenderWindow::ExposeHandle()
{
	return(hWndMain);
}


/*=================================================
	LRESULT CALLBACK RenderWindow::MyWindowProc(
		HWND hWndMain, UINT uMsg, WPARAM wParam,
		LPARAM lParam)
	----------------
	The default message proc for the render window
	system.
=================================================*/
LRESULT CALLBACK RenderWindow::MyWindowProc(HWND hWndMain, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
        switch(uMsg)
        {
                case WM_LBUTTONUP: 
                {
                        MyRenderWindow->LeftClick = true;
                }break;

                //case WM_RBUTTONDOWN: 
                //{
                //      MyRenderWindow->RightClick = true;
                //}break;

                case WM_RBUTTONUP:
                {
                        MyRenderWindow->RightClick = true;
                }break;


                case WM_CHAR:
                {
                        
                        MyRenderWindow->LastKeyPress = wParam;
                        MyRenderWindow->KeyPressed = true;
                                                
                }break;
                
                
                case WM_KEYDOWN:
                {
                                                
                }break;
                
                case WM_DESTROY://the window is being destroyed
                {
                        PostQuitMessage(0);//tell the application we are quitting
                }break;
                
                case WM_PAINT://the window needs repainting
                {
                        
                        //PAINTSTRUCT ps;//a variable needed for painting information
                        //HDC hdc = BeginPaint(hWndMain, &ps);//start painting
                        //EndPaint(hWndMain, &ps);//end painting
                        
                }break;

        }

        return(DefWindowProc(hWndMain,uMsg,wParam,lParam));
}


I am sorry about the long post, I forgot this site does not have the scrolling code boxes ;D
I am not sure if the code will help you, but I am at a loss here. By the way, thanks for trying to help.
It's late here in the UK, so I've only just had a quick look, and am off to sleep soon. Work tomorrow! :-(

But I can see

1
2
RenderWindow rw;
HWND hWnd = rw.ExposeHandle();


will return cccccc as (a) you do not init the handle member in you constructor.

And (b) you're not creating the window before you call ExposeHandle: you need to have successfully called SetupRenderWindow and CreateRenderWindow before you'll get a valid handle that way.

If that's not enough help, maybe someone in a different time zone can hep you?

Andy
Last edited on
After a bit of reworking I finally got it to work. I am surprised that it was that hard to expose a window handle in a class, but that is Microsoft for you.

Again thanks for the help Andy; I greatly appreciated it!
What was your solution?
Last edited on
Topic archived. No new replies allowed.