Sign in to follow this  
Mantrid

winmain

Recommended Posts

hi all, i'm only new so sorry if this is the wrong forum or anything. but my problem is that if i build in vis.net as a release no window will open at all. it exits at the "hWnd" failure case. yet it will ok and accept input if i build as debug :( can anyone take a look at this code? i'm not getting any warnings at all :(
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//global so visualisation class can use it
HWND hWnd;




int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	// TODO
	// 1. Register our window class. Use IDI_CGPICON as the icon and IDI_CGPICON_SMALL as the small icon, the menu is IDC_OURMENU
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc= (WNDPROC)WndProc;
	wcex.cbClsExtra= 0;
	wcex.cbWndExtra= 0;
	wcex.hInstance= hInstance;
	//wcex.hIcon= LoadIcon(hInstance, (LPCTSTR)IDI_ICON1);//'TT' icon
	wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName= NULL;//(LPCSTR)IDC_OURMENU;
	wcex.lpszClassName= "MainWindowClass";
	//wcex.hIconSm= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_ICON1);

	RegisterClassEx(&wcex);

	// 2. Create the window using the WS_OVERLAPPEDWINDOW style

	hWnd = CreateWindow("MainWindowClass", "Audio Program", WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

	   if (!hWnd) 
        return FALSE;
	// 3. Display the window and force an initial paint


	//initialise sound+video
	gMedia.Initialise();


	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);



	// 4. Enter our program loop - this loop continues until the program exits	
	MSG msg;


	
	//BADLY BEHAVED WINDOWS PROGRAM MESSAGE CODE


	while(GetMessage(&msg, NULL, 0, 0))
	{

			TranslateMessage(&msg);
			DispatchMessage(&msg);

	}


	//return msg.wParam;		
	return 0;
}

Share this post


Link to post
Share on other sites
The only real difference between that code and some of mine is that I pass HWND_DESKTOP as the hWndParent parameter of CreateWindow(). This may or may not be the issue. If it isn't, the problem could lie in your WndProc function, especially if you handle WM_CREATE and it fails. See the CreateWindow documentation from the MSDN Library.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
First thing, can you post your WndProc function.
Second, try calling

ShowWindow( hWnd, TRUE );

Just as a test.

Lastly, you should ZERO out your WNDCLASSEX, as some members are not initialized by you.

ZeroMemory( &wcex, sizeof(WNDCLASSEX) );

Share this post


Link to post
Share on other sites
Hi.

I found two holes in your code (maybe more):
1.You did not check for the return value of RegisterClassEx ()
2.You did not specified wcex.hIcon and wcex.hIconSm. Even you dont't want to specify any icon, you must set it to NULL.

It should solve your problem. Hope this helps.
V@T

[Edit]: These guys are faster tham me :)

Share this post


Link to post
Share on other sites
cheers mister anonymouse guy i'll try that


here's my wndproc, it's probably been mauled by the trial-and-error of up-all-night desperate debugging but it should more or less be there:




LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

// int KEYPRESS;



switch (message)
{
case WM_PAINT:






break;
case WM_DESTROY:

PostQuitMessage(0);

break;


///DEBUG KEYS STUFF
case WM_KEYDOWN:
{

switch(wParam)

{
case'W':
case 'w':
{
PlaySound(NULL, NULL, SND_FILENAME);
}
break;
case'S':
case 's':
{
PlaySound("test.wav", NULL, SND_FILENAME | SND_LOOP);
}
break;
case'D':
case 'd':
{
gMedia.PlayMediaFile(logo);
}
break;
case'A':
case 'a':
{
PlaySound("test2.wav", NULL, SND_FILENAME | SND_LOOP);
}
break;
}

default:

return DefWindowProc(hWnd, message, wParam, lParam); // We do not want to handle this message so pass back to Windows
// to handle it in a default way

}
}
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Skeleton_V@T

2.You did not specified wcex.hIcon and wcex.hIconSm. Even you dont't want to specify any icon, you must set it to NULL.

It should solve your problem. Hope this helps.
V@T

[Edit]: These guys are faster tham me :)


we have a winner! thanks very much fella, and to everyone who replied, i'll try all your other things as well so as not to tempt fate

now does anyone know why the 'w' key in wndproc won't stop the wave file from playing? :-/

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster

Lastly, you should ZERO out your WNDCLASSEX, as some members are not initialized by you.

ZeroMemory( &wcex, sizeof(WNDCLASSEX) );


awesome. zeroing that out seemed to solve a minor problem with mpeg-playback.. we're on a roll here!

Share this post


Link to post
Share on other sites
Another hole :(
The WM_PAINT message process nothing, in this case, you can call BeginPaint () and Endpaint () functions or let Windows do this for you by not processing the message at all.
The 'w' and 'W' is working properly, I guess because your window incorrectly processes WM_PAINT message, the multimedia system has no chance to play and stop sound.

Hth
V@T

Share this post


Link to post
Share on other sites
Quote:
Original post by Skeleton_V@T
Another hole :(
The WM_PAINT message process nothing, in this case, you can call BeginPaint () and Endpaint () functions or let Windows do this for you by not processing the message at all.
The 'w' and 'W' is working properly, I guess because your window incorrectly processes WM_PAINT message, the multimedia system has no chance to play and stop sound.

Hth
V@T

yeah i had of game-specific direct x calls in there which i remvoed for this demo so i can make it more black-boxy. commenting out wm_paint still doesn't let 'w' reach anything.

actually since you mention that i'll set up a breakpoint now to make sur eit's reach (i havent slept for a day or two, sorry!)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The problem is that PlaySound normally doesn't return until it finishes playing the sound, which in your case will be never because you set it to loop. You need to tell PlaySound to return immediately but keep playing the sound, which you do by adding in the SND_ASYNC flag.
-Extrarius

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
The problem is that PlaySound normally doesn't return until it finishes playing the sound, which in your case will be never because you set it to loop. You need to tell PlaySound to return immediately but keep playing the sound, which you do by adding in the SND_ASYNC flag.
-Extrarius


YES!

i'd rate you if you weren't anonymous, message me when you log in and i'll sort that :D

Share this post


Link to post
Share on other sites
It should be like that because you specified SNDLOOP without SND_ASYNC, there will be no messages sent to your window while the function doesn't return yet. Try including SND_ASYNC and the function will immediately return, let your window continue running gracefully.
To be more specified, SND_ASYNC stands for "asynchronously playing".

Hth
V@T

[Edit]
Huh ?
Once again I'm the slower guy :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this