Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,..
I was tring to create a window in a Win32 Application in Visual Studio 2008 .NET... The code is here
#include<windows.h>
int WINAPI WinMain(HINSTANCE hi,HINSTANCE pi,LPSTR str,int i)
{
	HWND hwnd;
	WNDCLASSEX wcex;
	wcex.style=0;
	wcex.lpszClassName="Hello";
	wcex.lpszMenuName=NULL;
	wcex.cbSize=sizeof(&wcex);
	wcex.cbClsExtra=NULL;
	wcex.cbWndExtra=NULL;
	RegisterClassEx(&wcex);
	hwnd=CreateWindowEx(NULL,"Hello","Hai",WS_OVERLAPPEDWINDOW,0,0,500,400,NULL,NULL,hi,NULL);
	ShowWindow(hwnd,i);
	UpdateWindow(hwnd);
	return 0;
}

This shows no errors on execution... but no window appeared and the application exited says(Native has exited with code 0 (0x0)),...

I have created some applications successfully some months before in some older version ( Visual studio 6) and Direct String assignment is also not taken in this Visual studio 2008 .NET,.,. Is there can be any Problems with the compiler or Some project settings needed.

I have tried that for simple Message Box display..and that's working.. so the problem must be in code... I check with breakpoints and I think the application termination is due to error in creation of handle for window (HWND).. What's the actual problem ... help me..

Thanks You...
Dinesh Balu
Posted
Comments
Richard MacCutchan 16-Sep-10 12:53pm    
You DO need a message loop and a Window procedure to make this work. There are lots of samples on the internet, and a very basic template application available in Visual Studio or Visual C++ Express.
Richard MacCutchan 17-Sep-10 4:35am    
I would suggest you get a copy of Visual C++ 2010 Express (free download from Microsoft) and use the sample code generated by the code wizard to show you all that you need to create a working program.
Dinesh Balu 17-Sep-10 5:53am    
But,.. I have tried the sample code generated with the Visual studio 2008 .NET, and that's working..... what's the problem with my own code....

You really need to do some more reading about why a function parameter is defined as a pointer. This is what it should look like:
C++
    MSG msg;
// ...
while(GetMessage(&msg,hwnd,NULL,NULL))
{
    DispatchMessage(&msg);
    TranslateMessage(&msg);
}
 
Share this answer
 
Comments
Dinesh Balu 17-Sep-10 1:49am    
oh!.... I thought LPMSG is itself a datatype or structure,.. so it means a Long Pointer to the structure MSG.... right?... ok i got it.. now no errors in code ... but no window is displayed and the application looking for message in while loop....... I think the error is in window creation...... check it out.......
Thank you.......
Hi,
Check your code against this:
// Mini.cpp : minimal Win32 application.
#include<windows.h>
#include <tchar.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == WM_DESTROY)
		PostQuitMessage(0);
	return DefWindowProc(hWnd, message, wParam, lParam);
}

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int nCmdShow)
{
	LPCTSTR psText = _T("Hello");
	WNDCLASSEX wc = { sizeof WNDCLASSEX, 0, WndProc, 0, 0, hInstance, LoadIcon(NULL, IDI_APPLICATION), 
		LoadCursor(NULL, IDC_ARROW), GetSysColorBrush(COLOR_WINDOW), NULL, _T("Mini") };
	if (RegisterClassEx(&wc))
		if (HWND hWnd = CreateWindow(wc.lpszClassName, psText, WS_OVERLAPPEDWINDOW, 
			CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL))
		{
			ShowWindow(hWnd, nCmdShow);
			UpdateWindow(hWnd);
			MSG msg = {0};
			while (GetMessage(&msg, NULL, 0, 0))
				DispatchMessage(&msg);
			return (int)msg.wParam;
		}
		else 
			psText = _T("CreateWindow failed");
	else
		psText = _T("RegisterClass failed");

	return MessageBox(NULL, psText, wc.lpszClassName, MB_OK | MB_ICONHAND);
}

cheers,
AR
 
Share this answer
 
Comments
Dinesh Balu 18-Sep-10 1:47am    
I have set NULL for Icon , Cursor,and Background of WINDCLASSEX...... is that could be the problem......
Thank you......
Alain Rist 18-Sep-10 2:05am    
You can check with my code that it is not enough to fail RegisterClassEx().
Dinesh Balu 18-Sep-10 2:09am    
I have checked my code for WNDCLASSEX with breakpoint and checked the local values.... I got
of the values as ....

hbrBackground 0x01900010 {unused=??? } HBRUSH__ *
unused CXX0030: Error: expression cannot be evaluated

lpszMenuName 0x00000000 <bad ptr=""> const wchar_t *
CXX0030: Error: expression cannot be evaluated

hIconSm 0xcccccccc {unused=??? } HICON__ *
unused CXX0030: Error: expression cannot be evaluated

Is that could be the problem.......
Dinesh Balu 18-Sep-10 2:14am    
Hai AR...... your code is working....... but what's the problem with mine?.....
Alain Rist 18-Sep-10 2:42am    
Check ALL differences with mine :)
You should read some about window procedures[^]. You currently have no code handling events, a message loop[^], and the code exits immediately.
 
Share this answer
 
v2
Comments
Dinesh Balu 16-Sep-10 12:10pm    
I tried with a Unlimited while loop....no use...... I think Window procedure and message loop is not necessary for just showing a window...
Thanks.......
you need a call back function, and deal with the message queue
 
Share this answer
 
Comments
Dinesh Balu 17-Sep-10 11:15am    
actually the problem is Registering WNDCLASSEX object is not successfull... what shall i do for that....

Thank you
Carlos_never 18-Sep-10 0:40am    
write the code follow the Answer 4. "LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) " is your call back function,which was transfered by OS. Messages which you could use to control the window are sent to that function.
"while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);" is a message queue, you use it to get messages from OS,and then send the messages to your call back funtion.
in your WNDCLASS object,there is a parameter called lpfnWndProc,which you could appoint the call back funtion. wcex.lpfnWndProc = WndProc
Carlos_never 18-Sep-10 1:03am    
I think you are sleep now. you can do like blow, and when you create a window successfully,you should compare the difference.

#include<windows.h>
LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam); // call back function
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return(0);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd; //handle to the main window
MSG Msg;
WNDCLASSEX wcex;
wcex.style=0;
wcex.lpfnWndProc=WndProc; // appoint the call back function, you loss it
wcex.lpszClassName="Hello";
wcex.lpszMenuName=NULL; // no menu
wcex.hInstance=hInstance; // current instance handle,which was one parameter of main function
wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION); // the icon style
wcex.hCursor=LoadCursor(NULL,IDC_ARROW); // the cursor style
wcex.cbSize=sizeof(&wcex);
wcex.cbClsExtra=NULL; // no extra style
wcex.cbWndExtra=NULL; // no extra style
wcex.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); // the black ground color is whit

RegisterClassEx(&wcex);
hwnd=CreateWindowEx(NULL,"Hello","Hai",WS_OVERLAPPEDWINDOW,0,0,500,400,NULL,NULL,hi,NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);

while( GetMessage(&Msg,NULL,0,0)) // message loop
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Dinesh Balu 18-Sep-10 1:55am    
Hai.... I have used exact code like the above....... see ' answer 6 ' but no change..... no error,.. no window...... The WNDCLASSEX object is not registered.....
Dinesh Balu 18-Sep-10 2:08am    
I have checked my code for WNDCLASSEX with breakpoint and checked the local values.... I got
of the values as ....

hbrBackground 0x01900010 {unused=??? } HBRUSH__ *
unused CXX0030: Error: expression cannot be evaluated

lpszMenuName 0x00000000 <bad ptr=""> const wchar_t *
CXX0030: Error: expression cannot be evaluated

hIconSm 0xcccccccc {unused=??? } HICON__ *
unused CXX0030: Error: expression cannot be evaluated

Is that could be the problem.......
I have checked my code for WNDCLASSEX with breakpoint and checked the local values.... I got
of the values as ....
hbrBackground   0x01900010 {unused=??? }    HBRUSH__ *
        unused  CXX0030: Error: expression cannot be evaluated
lpszMenuName    0x00000000 <Bad Ptr>    const wchar_t *
            CXX0030: Error: expression cannot be evaluated
hIconSm 0xcccccccc {unused=??? }    HICON__ *
        unused  CXX0030: Error: expression cannot be evaluated 


Is that could be the problem.......
 
Share this answer
 
#include<windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hi,HINSTANCE pi,LPSTR str,int i)
{
	MSG msg;
	HWND hwnd;
	WNDCLASSEX wcex;

	wcex.cbSize=sizeof(WNDCLASSEX);
	wcex.style=0;
	wcex.lpfnWndProc=WndProc;
	wcex.hInstance=hi;
	wcex.lpszClassName=TEXT("Hello");
	wcex.lpszMenuName=0;
	wcex.hbrBackground=(HBRUSH)GetStockObject(1);
	wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
	wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wcex.cbClsExtra=0;
	wcex.cbWndExtra=0;

	if(!RegisterClassEx(&wcex))
		MessageBox(NULL,TEXT("ERROR"),TEXT("ERROR"),0);
	GetLastError();
	hwnd=CreateWindowEx(NULL,(LPCWSTR)"Hello",(LPCWSTR)"Hai",WS_OVERLAPPEDWINDOW,0,0,500,400,NULL,NULL,hi,NULL);
	ShowWindow(hwnd,i);
	UpdateWindow(hwnd);
	while(GetMessage(&msg,hwnd,NULL,NULL))
	{
		DispatchMessage(&msg);
		TranslateMessage(&msg);
	}
	return 1;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wp,LPARAM lp)
{
	switch(message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hwnd,message,wp,lp);
}


I have checked with the exception handle in the statement of Registering class and the message box is displayed while execution... so the error is in Registering WNDCLASSEX ,... what shall i do...

Thank you...
Dinesh Balu
 
Share this answer
 
v3
Comments
Aescleal 17-Sep-10 6:21am    
You're passing a pointer to an MSG rather than the address of an MSG structure. Basically stop declaring stuff as pointers and use a structure.

Also you need to return something from your window procedure.

Pick up a copy of "Programming Windows" by Charles Petzold - everything you're trying to do is covered in the first couple of chapters.

Cheers,

Ash
Alain Rist 18-Sep-10 18:51pm    
Try WNDCLASSEX wcex = {0}; it should take you one step further.
Dinesh Balu 19-Sep-10 14:06pm    
What's the definition mean.....
I found the answer... i have tried with the WNDCLASS instead of WNDCLASSEX,... it's working....and the classname was the problem... should use either L"" format or resource format.. now it's look good...

#include<windows.h>


LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hi,HINSTANCE pi,LPSTR str,int i)
{
    MSG msg;
    HWND hwnd;
    WNDCLASS wcex;

    //wcex.cbSize=sizeof(&wcex);
    wcex.style=0;
    wcex.lpfnWndProc=WndProc;
    wcex.hInstance=hi;
    wcex.lpszClassName=L"Hello";
    wcex.lpszMenuName=0;
    wcex.hbrBackground=(HBRUSH)GetStockObject(1);
    wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
    wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wcex.cbClsExtra=0;
    wcex.cbWndExtra=0;

    if(!RegisterClass(&wcex))
        MessageBox(NULL,TEXT("ERROR"),TEXT("ERROR"),0);

    hwnd=CreateWindow(L"Hello",L"Hai",WS_OVERLAPPEDWINDOW,0,0,500,400,NULL,NULL,hi,NULL);
    if(hwnd)
    {
    ShowWindow(hwnd,i);
    UpdateWindow(hwnd);
    while(GetMessage(&msg,hwnd,NULL,NULL))
    {
        DispatchMessage(&msg);
        TranslateMessage(&msg);
    }
    }
    else
        MessageBox(NULL,L"Error",L"Window Creation",0);

    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wp,LPARAM lp)
{
    switch(message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hWnd,message,wp,lp);
}


Thank you for your response friends...........
 
Share this answer
 
Comments
Alain Rist 28-Sep-10 15:31pm    
Your 'answer' only works by chance. Try changing your project character set to MBCS and see what happens.
Dinesh Balu 29-Sep-10 4:48am    
I have changed the character set to MBCS.... now it accepts direct Strings.
but
when i'm using WNDCLASSEX like this

WNDCLASSEX wcex;

wcex.cbSize=sizeof(&wcex);
wcex.style=0;
wcex.lpfnWndProc=WndProc;
wcex.hInstance=hi;
wcex.lpszClassName="Hello";
wcex.lpszMenuName=0;
wcex.hbrBackground=(HBRUSH)GetStockObject(1);
wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;

if(!RegisterClassEx(&wcex))
MessageBox(NULL,TEXT("ERROR"),TEXT("ERROR"),0);
it show error registering class,...... what's the problem with the class parameters...... I have also tried your code,... but your code is not working when i distribute the class parameter definition... from braces to individual definition like wcex.style=0; ,.. why is that..
Alain Rist 29-Sep-10 7:25am    
'but your code is not working when i distribute the class parameter definition... from braces to individual definition like wcex.style=0;' then it's not anymore my code :)
The reason is you don't initialize all WNDCLASSEX members.
Dinesh Balu 30-Sep-10 4:45am    
You mean I should initialize all WNDCLASSEX members... including smallicon member right?...
Dinesh Balu 30-Sep-10 4:50am    
I have checked with all WNDCLASSEX members... but still problem in registering class,...

WNDCLASSEX wcex;

wcex.cbSize=sizeof(&wcex);
wcex.style=0;
wcex.lpfnWndProc=WndProc;
wcex.hInstance=hi;
wcex.lpszClassName=#Quote#Hello#Quote#;
wcex.lpszMenuName=0;
wcex.hIconSm=0;
wcex.hbrBackground=(HBRUSH)GetStockObject(1);
wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;

if(!RegisterClassEx(&wcex))
MessageBox(NULL,TEXT(#Quote#ERROR#Quote#),TEXT(#Quote#ERROR#Quote#),0);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900