Thread: RE: [Dev-C++] What's wrong with this code?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Marc H. <ma...@e-...> - 2002-11-27 07:28:40
|
Hi there, Change the TCHAR *NAME="OWDB"; to TCHAR NAME="OWDB"; in the header. _____________________________ Marc Heiligers -----Original Message----- From: Dreamjunky [mailto:li...@dr...] Sent: 27 November 2002 06:40 AM To: Post Dev-cpp Subject: [Dev-C++] What's wrong with this code? Hello again, I have a beginners question for anyone who would be willing to answer it for me. I wouldn't have bothered bringing it here if I hadn't gone over it for an hour already to no avail. It's not enough code to warrant going over for that long. When I run this my CreateWindow call fails. Any idea why? The only thing in the .h worth mentioning is TCHAR *NAME="OWDB". I'd appreciate any help you can offer. #include <windows.h> #include "OtherWorldsDB.h" int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR szCmd, int iCmdShow) { WNDCLASS wndclass; // main window HWND hWnd; // handle to main window MSG msg; // messages rec'd from msgserv wndclass.lpszClassName = NAME; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.hInstance = hInst; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); wndclass.lpszMenuName = NULL; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; if(!RegisterClass(&wndclass)) { error("Unable to register class"); return 1; } hWnd = CreateWindow(NAME, TEXT("Otherworlds Database"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 5, 5, 50, 50, NULL, NULL, hInst, NULL) if(!hWnd) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); error(lpMsgBuf); return 1; // this is where the prg exits every time } ShowWindow(hWnd, SW_SHOW); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND handle, UINT msg, WPARAM wp, LPARAM lp) { switch(msg) { case WM_DESTROY: // close PostQuitMessage(0); break; default: // default DefWindowProc(handle,msg,wp,lp); } return 0; } void error(TCHAR *msg) { MessageBox(NULL, msg, TEXT("OWDB Error!"), MB_ICONERROR); } |
From: Jaap de W. <wo...@Ne...> - 2002-11-27 08:12:32
|
You have to specify a menu in either the class or the CreateWindow() (From MS SDK doc about CreateWindow: hMenu [in] Handle to a menu, or specifies a child-window identifier depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be NULL if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window. ) Jaap -----Original Message----- From: Dreamjunky [mailto:li...@dr...] Sent: Wednesday, November 27, 2002 05:40 AM To: Post Dev-cpp Subject: [Dev-C++] What's wrong with this code? Hello again, I have a beginners question for anyone who would be willing to answer it for me. I wouldn't have bothered bringing it here if I hadn't gone over it for an hour already to no avail. It's not enough code to warrant going over for that long. When I run this my CreateWindow call fails. Any idea why? The only thing in the .h worth mentioning is TCHAR *NAME="OWDB". I'd appreciate any help you can offer. #include <windows.h> #include "OtherWorldsDB.h" int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR szCmd, int iCmdShow) { WNDCLASS wndclass; // main window HWND hWnd; // handle to main window MSG msg; // messages rec'd from msgserv wndclass.lpszClassName = NAME; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.hInstance = hInst; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); wndclass.lpszMenuName = NULL; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; if(!RegisterClass(&wndclass)) { error("Unable to register class"); return 1; } hWnd = CreateWindow(NAME, TEXT("Otherworlds Database"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 5, 5, 50, 50, NULL, NULL, hInst, NULL) if(!hWnd) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); error(lpMsgBuf); return 1; // this is where the prg exits every time } ShowWindow(hWnd, SW_SHOW); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND handle, UINT msg, WPARAM wp, LPARAM lp) { switch(msg) { case WM_DESTROY: // close PostQuitMessage(0); break; default: // default DefWindowProc(handle,msg,wp,lp); } return 0; } void error(TCHAR *msg) { MessageBox(NULL, msg, TEXT("OWDB Error!"), MB_ICONERROR); } |
From: Carlos d. M. <cg...@wo...> - 2002-11-27 11:12:21
|
TCHAR is one character never a string. For a string use either TCHAR* or LPTSTR. (Pointing about an answer by Marc Heiliger) Answering to Jaap, it's not needed to give a menu, you can use NULL if you don't want have one. That's just the default code for a window app and it works, giving a window with nothing. The answer is that you are not initializing the cbSize field of the WNDCLASS structure, you must initialize it so when the program watches at it it can use the memory space needed. You must add this line: wndclass.cbSize = sizeof(WNDCLASS); Remember this, always you need to use an structure you have ALWAYS to fill a member of it which is the size of the structure with structure.sizeMember = sizeof(STRUCTURE); The structures never know what's its own size, so you must provide it with that member. The answer can be too in the line, but I'm almost sure it is defined in the header, however I say it: wndclass.lpfnWndProc = WndProc; This member of the struct defines which is the window's callback function, which must have been defined before, but it is not defined, at least in this file. Add at the begin of it the line LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); And some place after in the file write the function. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); |
From: Marc H. <ma...@e-...> - 2002-11-27 09:22:03
|
I'm sorry. Once again I answer emails before I've finished my first cup of coffee. Of course, you're right Per, that's not what we want. So lets ignore what I said and pretend it never happened. _____________________________ Marc Heiligers -----Original Message----- From: Per Westermark [mailto:pw...@ia...] Sent: 27 November 2002 10:58 AM To: Marc Heiligers Cc: 'Dreamjunky'; Post Dev-cpp Subject: RE: [Dev-C++] What's wrong with this code? Explain what you mean. This program isn't using Unicode, so the TCHAR should be a normal char. Did you really want him to write char NAME="CWDB"; instead of char *NAME="CWDB"; or char NAME[] = "CWDB"; /Per W On Wed, 27 Nov 2002, Marc Heiligers wrote: > Hi there, > > Change the TCHAR *NAME="OWDB"; to TCHAR NAME="OWDB"; in the header. > > > _____________________________ > Marc Heiligers > > -----Original Message----- > From: Dreamjunky [mailto:li...@dr...] > Sent: 27 November 2002 06:40 AM > To: Post Dev-cpp > Subject: [Dev-C++] What's wrong with this code? > > > Hello again, > > I have a beginners question for anyone who would be willing to answer > it for me. I wouldn't have bothered bringing it here if I hadn't gone > over it for an hour already to no avail. It's not enough code to > warrant going over for that long. When I run this my CreateWindow > call fails. Any idea why? The only thing in the .h worth mentioning > is TCHAR *NAME="OWDB". I'd appreciate any help you can offer. > > #include <windows.h> > #include "OtherWorldsDB.h" > > int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR szCmd, int > iCmdShow) { > > WNDCLASS wndclass; // main window > HWND hWnd; // handle to main window > MSG msg; // messages rec'd from msgserv > > wndclass.lpszClassName = NAME; > wndclass.style = CS_HREDRAW | CS_VREDRAW; > wndclass.lpfnWndProc = WndProc; > wndclass.hInstance = hInst; > wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); > wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); > wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); > wndclass.lpszMenuName = NULL; > wndclass.cbClsExtra = 0; > wndclass.cbWndExtra = 0; > > if(!RegisterClass(&wndclass)) { > error("Unable to register class"); > return 1; > } > > hWnd = CreateWindow(NAME, TEXT("Otherworlds Database"), > WS_OVERLAPPEDWINDOW > | WS_VISIBLE, 5, > 5, 50, 50, NULL, NULL, hInst, NULL) > > if(!hWnd) { > LPVOID lpMsgBuf; > FormatMessage( > FORMAT_MESSAGE_ALLOCATE_BUFFER | > FORMAT_MESSAGE_FROM_SYSTEM | > FORMAT_MESSAGE_IGNORE_INSERTS, > NULL, > GetLastError(), > 0, // Default language > (LPTSTR) &lpMsgBuf, > 0, > NULL > ); > > error(lpMsgBuf); > return 1; // this is where the prg exits > every time > } > > ShowWindow(hWnd, SW_SHOW); > > while(GetMessage(&msg, NULL, 0, 0)) { > TranslateMessage(&msg); > DispatchMessage(&msg); > } > > return msg.wParam; > } > > LRESULT CALLBACK WndProc(HWND handle, UINT msg, WPARAM wp, LPARAM lp) > { > switch(msg) { > case WM_DESTROY: // close > PostQuitMessage(0); > break; > > default: // default > DefWindowProc(handle,msg,wp,lp); > } > > return 0; > } > > void error(TCHAR *msg) { > MessageBox(NULL, msg, TEXT("OWDB Error!"), MB_ICONERROR); > } > > > ------------------------------------------------------- > This SF.net email is sponsored by: Get the new Palm Tungsten T > handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
From: Jaap de W. <wo...@Ne...> - 2002-11-27 12:45:22
|
Ok, I was wrong about the menu. But Carlos, the WNDCLASS struct does not have a cbSize member. Thats = the WNDCLASSEX. What is wrong about the code is -not- in WinMain, (although there = should be a ; after CreateWindow()) but in WndProc(). You have to put one more return statement: In your old code you always return zero. This is wrong for for instance the WM_NCCREATE message, this will = prevent your window from being created. LRESULT CALLBACK WndProc(HWND handle, UINT msg, WPARAM wp, LPARAM lp) { switch(msg) { case WM_DESTROY: // close PostQuitMessage(0); break; =20 default: // default ->return<- DefWindowProc(handle,msg,wp,lp); } =20 return 0; } Jaap. > -----Original Message----- > From: Carlos Garc=EDa del Monte [mailto:cg...@wo...] > Sent: Wednesday, November 27, 2002 12:10 PM > To: dev-cpp-users-list > Subject: Re: [Dev-C++] What's wrong with this code? >=20 >=20 > TCHAR is one character never a string. For a string use=20 > either TCHAR* or LPTSTR. > (Pointing about an answer by Marc Heiliger) > Answering to Jaap, it's not needed to give a menu, you can=20 > use NULL if you don't > want have one. That's just the default code for a window app=20 > and it works, > giving a window with nothing. > The answer is that you are not initializing the cbSize field=20 > of the WNDCLASS > structure, you must initialize it so when the program watches=20 > at it it can use > the memory space needed. You must add this line: > wndclass.cbSize =3D sizeof(WNDCLASS); > Remember this, always you need to use an structure you have=20 > ALWAYS to fill a > member of it which is the size of the structure with=20 > structure.sizeMember =3D > sizeof(STRUCTURE); The structures never know what's its own=20 > size, so you must > provide it with that member. >=20 > The answer can be too in the line, but I'm almost sure it is=20 > defined in the > header, however I say it: > wndclass.lpfnWndProc =3D WndProc; > This member of the struct defines which is the window's=20 > callback function, which > must have been defined before, but it is not defined, at=20 > least in this file. > Add at the begin of it the line > LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); > And some place after in the file write the function. > LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,=20 > LPARAM lParam); >=20 >=20 >=20 > ------------------------------------------------------- > This SF.net email is sponsored by: Get the new Palm Tungsten T=20 > handheld. Power & Color in a compact size!=20 > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users >=20 |
From: Per W. <pw...@ia...> - 2002-11-27 08:59:42
|
Explain what you mean. This program isn't using Unicode, so the TCHAR should be a normal char. Did you really want him to write char NAME="CWDB"; instead of char *NAME="CWDB"; or char NAME[] = "CWDB"; /Per W On Wed, 27 Nov 2002, Marc Heiligers wrote: > Hi there, > > Change the TCHAR *NAME="OWDB"; to TCHAR NAME="OWDB"; in the header. > > > _____________________________ > Marc Heiligers > > -----Original Message----- > From: Dreamjunky [mailto:li...@dr...] > Sent: 27 November 2002 06:40 AM > To: Post Dev-cpp > Subject: [Dev-C++] What's wrong with this code? > > > Hello again, > > I have a beginners question for anyone who would be willing to answer it for > me. I wouldn't have bothered bringing it here if I hadn't gone over it for > an hour already to no avail. It's not enough code to warrant going over for > that long. When I run this my CreateWindow call fails. Any idea why? The > only thing in the .h worth mentioning is TCHAR *NAME="OWDB". I'd appreciate > any help you can offer. > > #include <windows.h> > #include "OtherWorldsDB.h" > > int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR szCmd, int > iCmdShow) { > > WNDCLASS wndclass; // main window > HWND hWnd; // handle to main window > MSG msg; // messages rec'd from msgserv > > wndclass.lpszClassName = NAME; > wndclass.style = CS_HREDRAW | CS_VREDRAW; > wndclass.lpfnWndProc = WndProc; > wndclass.hInstance = hInst; > wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); > wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); > wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); > wndclass.lpszMenuName = NULL; > wndclass.cbClsExtra = 0; > wndclass.cbWndExtra = 0; > > if(!RegisterClass(&wndclass)) { > error("Unable to register class"); > return 1; > } > > hWnd = CreateWindow(NAME, TEXT("Otherworlds Database"), WS_OVERLAPPEDWINDOW > | WS_VISIBLE, 5, > 5, 50, 50, NULL, NULL, hInst, NULL) > > if(!hWnd) { > LPVOID lpMsgBuf; > FormatMessage( > FORMAT_MESSAGE_ALLOCATE_BUFFER | > FORMAT_MESSAGE_FROM_SYSTEM | > FORMAT_MESSAGE_IGNORE_INSERTS, > NULL, > GetLastError(), > 0, // Default language > (LPTSTR) &lpMsgBuf, > 0, > NULL > ); > > error(lpMsgBuf); > return 1; // this is where the prg exits > every time > } > > ShowWindow(hWnd, SW_SHOW); > > while(GetMessage(&msg, NULL, 0, 0)) { > TranslateMessage(&msg); > DispatchMessage(&msg); > } > > return msg.wParam; > } > > LRESULT CALLBACK WndProc(HWND handle, UINT msg, WPARAM wp, LPARAM lp) { > switch(msg) { > case WM_DESTROY: // close > PostQuitMessage(0); > break; > > default: // default > DefWindowProc(handle,msg,wp,lp); > } > > return 0; > } > > void error(TCHAR *msg) { > MessageBox(NULL, msg, TEXT("OWDB Error!"), MB_ICONERROR); > } > > > ------------------------------------------------------- > This SF.net email is sponsored by: Get the new Palm Tungsten T > handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |