CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2005
    Posts
    114

    Window Creation Failure (Can't seem to find the problem)

    I've just recently returned to WINAPI programming in C++, but now in Vista. I'm having a problem creating a window. According to GetLastError() windows cannot find the class. Clearly the class has been registered (in the same module of course) and I'm using the same exact name for both lpszClassName and the class parameter in the CreateWindow function. I just don't see the problem.

    Code:
    #include <Windows.h>
    
    UINT msg_is_server = NULL;
    UINT msg_transmit = NULL;
    
    LRESULT CALLBACK ServerWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	if (msg == msg_is_server)
    	{
    		BOOL *bRet = (BOOL*) wParam;
    		*bRet = TRUE;
    	}
    	else
    	{
    		BOOL *bRet = (BOOL*) wParam;
    		*bRet = FALSE;
    	}
    
    	return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance,
    					HINSTANCE hPrevInstance,
    					LPSTR lpCmdLine,
    					int nCmdShow)
    {
    	LPCWSTR IPC_SERVER = L"IPCServer";
    	WNDCLASSEX wcx;
    	MSG msg;
    	HWND hWnd;
    
    	msg_is_server = RegisterWindowMessage((LPCWSTR) L"IPC_IsServer");
    	msg_transmit = RegisterWindowMessage((LPCWSTR) L"IPC_Transmit");
    
    	wcx.cbSize = sizeof(WNDCLASSEX);
    	wcx.style = 0;
    	wcx.cbWndExtra = 0;
    	wcx.cbClsExtra = 0;
    
    	wcx.hCursor = NULL;
    	wcx.hIcon = NULL;
    	wcx.hIconSm = NULL;
    
    	wcx.lpszClassName = IPC_SERVER;
    	wcx.lpfnWndProc = ServerWndProc;
    	wcx.lpszMenuName = NULL;
    	
    	wcx.hbrBackground = NULL;
    
    	if (!RegisterClassEx(&wcx))
    	{
    		MessageBox(NULL, (LPCWSTR) L"Unable to register IPCServer window class.", (LPCWSTR) L"Error", MB_OK);
    		return 0;
    	}
    
    	hWnd = CreateWindow(IPC_SERVER, (LPCWSTR)L"IPCServer", WS_SYSMENU | WS_CAPTION, 20, 20, 20, 20,HWND_MESSAGE, NULL, hInstance, NULL);
    
    	if (!hWnd)
    	{
    		DWORD err = GetLastError();
    		MessageBox(NULL, (LPCWSTR) L"Unable to create IPCServer window.", (LPCWSTR) L"Error", MB_OK);
    		return err;
    	}
    
    	while (GetMessage(&msg, hWnd, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return 0;
    }

  2. #2
    Join Date
    Nov 2009
    Posts
    15

    Re: Window Creation Failure (Can't seem to find the problem)

    You use HWND_MESSAGE in create window but dont declare it.

    You have to link to user32 this maybe your problem as well.

    Hope this helps.

  3. #3
    Join Date
    Nov 2009
    Posts
    15

    Re: Window Creation Failure (Can't seem to find the problem)

    You use HWND_MESSAGE in create window but dont declare it.

    You have to link to user32 this maybe your problem as well.

    Hope this helps.

  4. #4
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Window Creation Failure (Can't seem to find the problem)

    Snoopy07:
    HWND_MESSAGE is a special constant defined as ((HWND)-3) used to create message only window.


    I have no idea why you are getting this error. I copied your code built and ran without a hitch (VS 2008 on Vista Ultimate). Did you post a code that you are actually compiling?

    The question I have: are you trying to create message only window?
    If yes (and I think this is obvious) you do not need caption, hence no need for a window’s name and system menu. You can set size to 0, 0, 0, 0.
    Why? Simply, message only window is invisible. It only serves a purpose of handling messages.
    I hardly used message only window; maybe twice or three times. Remember, such a window does not receive broadcast messages.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured