• Problem with some Windows API code C/C++
    18 replies, posted
[cpp] /* Windows test program Written by Chryseus, feel free to copy */ #include <windows.h> #include <strsafe.h> // For Error() // Some constants int const GAME_WIDTH = 800; int const GAME_HEIGHT = 600; int const GAME_BPP = 16; enum { GAME_INIT, GAME_START, GAME_RESTART, GAME_PAUSE, GAME_ERROR, GAME_QUIT }; // Function definitions void ProcessEvents(LPMSG msg); void CreateMainWindow(HINSTANCE, int nCmdShow); void Error(LPTSTR lpszFunction); // Main window procedure LRESULT CALLBACK WndProc(HWND hwin, UINT uMsg , WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostMessage(hwin, WM_QUIT, 0, 0); return 0; case WM_PAINT: return 0; case WM_SIZE: return 0; case WM_CREATE: return 0; default: return DefWindowProc(hwin, uMsg, wParam, lParam); } } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; // Setup window class WNDCLASSEX winClass; winClass.cbSize = sizeof(WNDCLASSEX); winClass.style = CS_HREDRAW | CS_VREDRAW; winClass.lpszClassName = "MainWindow"; winClass.lpfnWndProc = WndProc; winClass.hInstance = hInstance; winClass.cbClsExtra = 0; winClass.cbWndExtra = 0; winClass.hIcon = NULL; winClass.hIconSm = NULL; winClass.hCursor = LoadCursor(NULL, IDC_ARROW); winClass.hbrBackground = NULL; winClass.lpszMenuName = NULL; // Register the class if (!RegisterClassEx(&winClass)) { Error(TEXT("RegisterClassEx")); return -1; } // Create the window CreateMainWindow(hInstance, nCmdShow); // Process events ProcessEvents(&msg); return 0; } void CreateMainWindow(HINSTANCE hInstance, int nCmdShow) { HWND mainWin; if (!(mainWin = CreateWindow("MainWindow", "Window Title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, GAME_WIDTH, GAME_HEIGHT, NULL, NULL, hInstance, NULL))) { Error(TEXT("CreateWindowEx")); abort(); } // Show and update the window ShowWindow(mainWin, nCmdShow); UpdateWindow(mainWin); } void ProcessEvents(LPMSG msg) { bool bReturn; while(bReturn = GetMessage(msg, NULL, 0, 0) != 0) { if ( (int)bReturn == -1) { MessageBox(NULL, "Failed to get message from message queue", "Error", MB_OK | MB_ICONSTOP); abort(); } TranslateMessage(msg); DispatchMessage(msg); } } // Error display function, taken from MSDN void Error(LPTSTR lpszFunction) { DWORD dw = GetLastError(); LPVOID lpMsgBuf; LPVOID lpDisplayBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), TEXT("%s failed with error %d: %s"), lpszFunction, dw, lpMsgBuf); MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); } [/cpp] All problems fixed, here is the code if anyone wants it.
Where did you register the class?
... Line 47 [quote]if (!RegisterClassEx(&winClass))[/quote] [url]http://msdn.microsoft.com/en-us/library/ms633587%28VS.85%29.aspx[/url]
That's checking if you didn't register it You never actually [i]registered[/i] it
[QUOTE=raccoon12;16581890]That's checking if you didn't register it You never actually [i]registered[/i] it[/QUOTE] That's... not how it works. He's calling RegisterClassEx, which returns a value indicating success or error, he's checking the return value.
[quote=raccoon12]You never actually registered it [/quote] Do you even know C/C++ ? the if () checks the data returned by RegisterClassEx(&winClass); to see if there is any error, which in this case there is and it triggers the error message. Only problem is, I have no idea what is wrong with winClass to trigger the error message.
o lol And yes, I do know C++, but I'm not good with the Windows API Don't bite my ass because I tried helping
[QUOTE=raccoon12;16581955]And yes, I do know C++[/QUOTE] Well, if you can't read simple code like that (it has nothing to do with the Windows API), you don't even know [I]basic[/I] C++. That's ok, but you shouldn't claim that you do, it just makes you look dumb.
:\ I don't know whether to be offended or let it slide [editline]11:20AM[/editline] hrm
Did you try setting lpszMenuName? Did you try without CS_DBLCLKS and CS_OWNDC? Try that and if it works, revert each change one at a time to see what caused the error. (On a side note, cbClsExtra and cbWndExtra are not pointers, so you should be using 0 instead of NULL, although the result is the same.) [QUOTE=raccoon12;16582055]:\ I don't know whether to be offended or let it slide [editline]11:20AM[/editline] hrm[/QUOTE] I didn't mean to offend you, it was serious advice. Most people learn pretty fast though, so pick up your book (or tutorial) and learn about functions! If you want to use the Windows API, you might want to try a book/tutorial on C instead, as it will be more likely to use concepts applied in the Windows API than a C++ book/tutorial.
Well, I'm reading Beginning Programming with DirectX 9.0 or something, and it's pretty good so far.
[QUOTE=raccoon12;16582354]Well, I'm reading Beginning Programming with DirectX 9.0 or something, and it's pretty good so far.[/QUOTE] I'm doing pretty much the same thing. The debugger is complaining of winClass not being initialized :confused:
that's weird
Add this after the declaration of the winClass struct: [cpp] memset(&winClass, 0, sizeof(winClass)); [/cpp] Initializes all members to 0 (null), so you only need to define the ones you use, and Windows will set the rest to default. memset is declared in string.h. (I can't see you missing any members except for hIconSm though)
[quote=jA_c0p[/quote](I can't see you missing any members except for hIconSm though) [/quote] No wonder it did not work, I missed out hIconSm which resulted the the class having junk where hIconSm was. Thanks a lot, now to fix the remaining bugs and I shall post the full code for anyone interested. urgh CreateWindowEx is now failing to work as it should.
Kind of off topic but why are you using defines in place of enumerations? [cpp] enum { GAME_INIT = 0, GAME_START = 1, GAME_RESTART = 2, GAME_PAUSE = 3, GAME_ERROR = 4, GAME_QUIT = 5 }; [/cpp]
[QUOTE=Stickmoose;16583703]Kind of off topic but why are you using defines in place of enumerations? [/QUOTE] Blame my frequent use of C, I shall change it.
[QUOTE=Chryseus;16583878]Blame my frequent use of C, I shall change it.[/QUOTE] You have enumerations in C, too.
I normally use #define in C. Anyways problem solved, I did not implement the window procedure which caused it to fail. I shall tidy the code and post it, thanks for your help everyone.
Sorry, you need to Log In to post a reply to this thread.