Tuesday, July 14, 2009

How do you create a Window inside a theard in C/C++?

I am having problems creating a window inside a thread. The thread is launched with a funtion from the main application. the LaunchThread() is in a DLL, the DLL also has a function for registering the window using RegisterWindow(), the WndProc and the ThreadProc are also in the DLL.





The thread is created using CreateThread and the ThreadProc calls CreateWindow to create the window, this window will be a user interface for operations that do not interfere with the main application, the window will be launching other threads and such threads will using functions defined in the DLL.





The only connection in between the main application and the DLL is the LaunchThread() function.





//-- Sample code





//-------main application


HANDLE hThread = LaunchThread();








// DLL


HANDLE DLLEXPORT LaunchThread()


{


// Create the theard


}





VOID ThreadProc(LPVOID lParam)


{


// Create the window using CreateWindow





// Message loop in here


}

How do you create a Window inside a theard in C/C++?
Can you be more specific? What exactly are the problems you're running into? If you're getting null handles, what do you get when you call GetLastError?





-- EDIT --





%26gt;The HWND returned by CreateWindow() is NULL,


%26gt; but GetLastError() returns 0.





That is very odd. I'm afraid I can't help debug without seeing more of the code. Some things you might keep in mind:





- GetLastError refers to the last win32 call, so if you're doing anything between the CreateWindow call and GetLastError you could be clobbering your result.





- If your window procedure doesn't handle certain messages, such as WM_CREATE correctly, Bad Things can happen. You should check to see if it's being entered and that messages are being properly routed by your message loop.





- Make sure the hInstance you pass in is for the correct module. You want the library you're using, not the main executable... see updated references for a blog post that may be relevant to you.





- You may want to eliminate the thread as the cause of the problem... if you create this window in the main application thread (ignoring for a moment that this probably doesn't make sense in the context of your app), does the code work then? If so, does it work in a thread in the main application executable (rather than in your library)?





Good luck.
Reply:Good to hear you're making progress. Check MSDN for information on error codes:





http://msdn.microsoft.com/libr... Report It



No comments:

Post a Comment