Thread: How come this only works in Windows nt/2000?

  1. #1

    How come this only works in Windows nt/2000?

    I have this simple example taken from the Windows programming book form Pretzold:
    #include <windows.h>
    // Windows procedure function prototype - deals with
    // messages
    int WINAPI WinMain(HINSTANCE hINstance, HINSTANCE hPrevInstance,
    				   LPSTR szCmdLine, int iCmdShow)
    	static TCHAR szAppName[] = TEXT("HelloWin");
    	HWND hwnd;			// handle to the main window
    	MSG msg;			// messages received
    	WNDCLASS wndclass;	// main window class
    	wndclass.lpfnWndProc = WndProc;
    	wndclass.cbClsExtra = 0;
    	wndclass.cbWndExtra = 0;
    	wndclass.hInstance = hInstance;
    	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    	wndclass.lpszMenuName = NULL;
    	wndclass.lpszClassName = szAppName;
    	// Register new window class
    	if(!RegisterClass(&wndclass) {
    		MessageBox(NULL, TEXT("This program requires Windows NT"), szAppName,
    		return 0;
    	// Create new window (handle)
    	hwnd = CreateWindow(szAppName,		// window class name
    		TEXT("Window"),					// window title
    		WS_OVERLAPPEDWINDOW,			// window style
    		CW_USEDEFAULT,					// initial x position
    		CW_USEDEFAULT,					// initial y position
    		CW_USEDEFAULT,					// initial x size
    		CW_USEDEFAULT,					// initial y size
    		NULL,							// parent window handle
    		NULL,							// window menu handle
    		hInstance,						// program instance handle
    		NULL);							// creation parameters
    	// Window is now created. Show it
    	ShowWindow(hwnd, iCmdShow);
    	// Update
    	// Message loop
    	while(GetMessage(&msg, NULL, 0, 0) {
    		DispatchMessage(&msg);		// dispatches to WinProc function
    	return msg.wParam;				// return 0
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    	HDC hdc;			// device context
    	RECT rect;
    	// select an aption accoding to the message received
    	switch(message) {
    	case WM_CREATE:
    		PlaySound(TEXT("hello.wav"), NULL, SND_FILENAME | SND_ASYNC);
    		return 0;
    	case WM_PAINT:
    		hdc = BeginPaint(hwnd, &pt);
    		GetClientRect(hwnd, &rect);
    		DrawText(hdc, TEXT("Hello Windows"), -1, &rect, DT_SINGLELINE |
    		EndPaint(hwnd, &pt);
    		return 0;
    	case WM_DESTROY:
    		// PostQuitMessage return 0 which makes the message loop end
    		return 0;
    	// windows automatically deals with messages that for some
    	// reason were not interpreted by the program
    	return DefWindowProc(wnd, msg, wParam, lParam);
    How come this only works under Windows NT and not Windows 98?

  2. #2
    Join Date
    Jan 2002
    Because you have a lot of typos. Have you tried to compile this code?

    The only way that the code would not compile and run under WinNT/2000/xp (assuming you have corrected the errors) is if you #define UNICODE prior to #include <windows.h>. You should also link with winmm.lib to use Playsound ( the windows multimedia library).

    Read what Petzold has to say about UNICODE and you will understand why the code wouldn't run on win98 if UNICODE is defined.
    Last edited by Ken Fitlike; 08-30-2002 at 07:04 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I dont get this working on gcc it works fine in windows
    By wise_ron in forum C Programming
    Replies: 8
    Last Post: 05-08-2006, 05:33 AM
  2. GUI library that works on windows and linux?
    By Logan in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2006, 08:40 PM
  3. Program works on Windows XP and 2000, not on 98 or ME
    By MidnightlyCoder in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 02:36 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  5. Windows Rant followed by installation question
    By confuted in forum A Brief History of
    Replies: 12
    Last Post: 06-21-2003, 04:42 PM