How come this only works in Windows nt/2000?

This is a discussion on How come this only works in Windows nt/2000? within the Windows Programming forums, part of the Platform Specific Boards category; I have this simple example taken from the Windows programming book form Pretzold: Code: #include <windows.h> // Windows procedure function ...

  1. #1
    Unregistered
    Guest

    How come this only works in Windows nt/2000?

    I have this simple example taken from the Windows programming book form Pretzold:
    Code:
    #include <windows.h>
    
    // Windows procedure function prototype - deals with
    // messages
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    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.style = CS_HREDRAW | CS_LREDRAW;
    	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,
    			MB_ICONERROR);
    		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
    	UpdateWindow(hwnd);
    
    	// Message loop
    	while(GetMessage(&msg, NULL, 0, 0) {
    		TranslateMessage(&msg);
    		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
    	PAINTSTRUCT pt;
    	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 |
    			DT_CENTER | DT_VCENTER);
    
    		EndPaint(hwnd, &pt);
    		return 0;
    	case WM_DESTROY:
    		// PostQuitMessage return 0 which makes the message loop end
    		PostQuitMessage(0);
    		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?
    Thanks.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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 Cprogramming.com
    Replies: 12
    Last Post: 06-21-2003, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21