Archived

This topic is now archived and is closed to further replies.

Sarlok

Illegal Instruction?

Recommended Posts

Sarlok    122
'allo. Working with a friends computer today, just put together a skeleton window program. It compiles fine, but when I run it it "caused an error in <unknown>". With the debugger, it tells me "0xC000001D: Illegal Instruction.". It's not like it's anything major, just my window class and the message loop. Any ideas why it's doing this? The source to those interested: Oh, and so I know for next time, what's the tag I need to embed for putting code into posts so it nests them the way they're typed? Ta. ----- #include <windows.h> LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM); char szWinName[] = "MyWindow"; int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode) { HWND hwnd; MSG msg; WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.hInstance = hInst; wc.lpszClassName = szWinName; wc.style = 0; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.lpszMenuName = NULL; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); if(!RegisterClassEx(&wc)) return 0; hwnd = CreateWindow( szWinName, "Base Window Code", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, hInst, NULL); ShowWindow(hwnd, nWinMode); UpdateWindow(hwnd); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; } ------ "When you kill one, it is a tragedy. When you kill ten million, it is a statistic." - Sarlok [edited by - Sarlok on May 1, 2002 2:07:24 PM]

Share this post


Link to post
Share on other sites
S1CA    1418
1. When you get the error in the debugger you should look at the call stack/context where it blew up - that should be the exact place where the problem is caused.

2. You aren''t setting the WindowProcedure in the WNDCLASSEX!!! - The "lpfnWndProc" parameter should be pointing at WindowFunc().
When you call CreateWindow (assuming it even gets there), that will try to call your window procedure specified in the WNDCLASSEX structure.
Since you don''t clear the structure, if it''s a debug build the lpfnWndProc member will be set to 0xCDCDCDCD (the debug "uninitialised memory" value) which is where CreateWindow will try and jump to - which doesn''t contain valid program code which results in an illegal instruction....

3. Your app probably isn''t really a child of the desktop - so don''t set HWND_DESKTOP as your window parent. Instead set that to NULL.

4. When you do get some messages (by telling Windows where to send those messages - see#1), you''ll find that it''s very rare that Windows would ever send you a WM_DESTROY message. Really you should also handle WM_CLOSE and call DestroyWindow() when you do... which will in turn send out the WM_DESTROY

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
Sarlok    122
quote:
Original post by S1CA
2. You aren''t setting the WindowProcedure in the WNDCLASSEX!!! - The "lpfnWndProc" parameter should be pointing at WindowFunc().
When you call CreateWindow (assuming it even gets there), that will try to call your window procedure specified in the WNDCLASSEX structure.
Since you don''t clear the structure, if it''s a debug build the lpfnWndProc member will be set to 0xCDCDCDCD (the debug "uninitialised memory" value) which is where CreateWindow will try and jump to - which doesn''t contain valid program code which results in an illegal instruction....

3. Your app probably isn''t really a child of the desktop - so don''t set HWND_DESKTOP as your window parent. Instead set that to NULL.

4. When you do get some messages (by telling Windows where to send those messages - see#1), you''ll find that it''s very rare that Windows would ever send you a WM_DESTROY message. Really you should also handle WM_CLOSE and call DestroyWindow() when you do... which will in turn send out the WM_DESTROY



D''oh. I thought the window class looked a bit small.
Left all my books and source at home, so didn''t have anything to go over to make sure I didn''t miss anthing.
And as for the other thing, yeah. Hadn''t finished, was just starting the message loop when I thought I should make sure it even works... which it didn''t.

"When you kill one, it is a tragedy. When you kill ten million, it is a statistic."
- Sarlok

Share this post


Link to post
Share on other sites
Anthracks    122
Also, just so you know in the future, the tag for formatting source is [ source ] and [ /source ] without the extra spaces between the brackets.

Anthracks

Share this post


Link to post
Share on other sites