|
From: Lists <li...@dr...> - 2003-01-01 21:21:10
|
Hello again, I'm having some trouble with a bit of code, and it's a strange bug to = track down. I found in a fit of frustration that 9 times out of 10 when = I run this program it terminates in the layout function (at the = bottom).. as far as I can tell it's the GetClientRect function that's = causing the error. I put in the lerror function to print the HWND in a = message box, and then the program started terminating on that line! I = have no idea if the two are related. But at any rate, the strangest = part is the fact that if I run the program over and over without = compiling, about 1 in 10 times a message box will pop up with a valid = HWND, and the program will go on from there without a problem. What on = earth could be causing this? If anybody wants to test this out I posted = netstalker.c and netstalker.h at http://dreamjunky.com/netstalker.c and = you can guess the other path. Here's the code below.. I would really appreciate any help you could = provide.. /****************************** * Webstalker v1.0 * Surf web pages searching for format strings and * extracting data from them * * Author: Tvienti 12/4/02 ******************************/ #include <stdio.h> #include <stdlib.h> #include <winsock.h> #include <windows.h> #include "netstalker.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 ghInst =3D hInst; //set instance to global identified wndclass.lpszClassName =3D NAME; wndclass.style =3D CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc =3D WndProc; wndclass.hInstance =3D hInst; wndclass.hIcon =3D LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor =3D LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground =3D (HBRUSH)GetStockObject(LTGRAY_BRUSH); wndclass.lpszMenuName =3D NULL; wndclass.cbClsExtra =3D 0; wndclass.cbWndExtra =3D 0; if(!RegisterClass(&wndclass)) { error("Unable to register class"); return 1; } hWnd =3D CreateWindow(NAME, TEXT("Webstalker v1.0"), = WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, = NULL); if(!hWnd) { error(TEXT("Unable to create window!")); return 1; } ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND handle, UINT msg, WPARAM wp, LPARAM lp) { DWORD style; switch(msg) { case WM_CREATE: //create style =3D WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP; hFldPath =3D CreateWindow(TEXT("EDIT"), "Enter Path", style | = ES_LEFT, 10, 10, 500, 25, handle, (HMENU) FIELD_PATH, ghInst, NULL ); hFldURL =3D CreateWindow(TEXT("EDIT"), "Enter URL", style | ES_LEFT, = 10, 50, 500, 25, handle, (HMENU) FIELD_URL, ghInst, NULL); hBtnGo =3D CreateWindow(TEXT("BUTTON"), "Search", style, 10, 90, 150, = 30, handle, (HMENU) BTN_SEARCH, ghInst, NULL); layout(handle, hFldURL, hFldPath, hBtnGo); //layout controls break; case WM_SIZE: //size layout(handle, hFldURL, hFldPath, hBtnGo); break; case WM_DESTROY: //close PostQuitMessage(0); break; default: //default return DefWindowProc(handle,msg,wp,lp); } return 0; } /**************** * display an error message * Arguments: * TCHAR* msg - error message to print ****************/ void error(TCHAR *msg) { MessageBox(NULL, msg, TEXT("Webstalker Error!"), MB_ICONERROR); } /**************** * See above * * Accepts long ****************/ void lerror(long msg) { TCHAR *tcMsg; _ltoa(msg,tcMsg,10); error(tcMsg); } /*************************** * Display text on the main display * * szDisplay text to display ***************************/ void display(TCHAR* szDisplay) { } /*************************** * Connect to a domain and get * the URL * * szDomain domain name in format nnnn.xxx * szPath path to get (must be at least "/\0" * lpHtml pointer to the HTML struct * * returns: * 0 * WSE_INVALID_PATH szPath contains invalid characters * WSE_INVALID_DOMAIN unable to locate szDomain * WSE_PATH_NOT_EXIST szPath doesn't exist on szDomain ***************************/ int get_page(TCHAR *szDomain, TCHAR *szPath, htStruct *lpHtml) { return 0; } /*************************** * Layout the controls according * to the current window dimensions * * hParent The containing window * hFldURL URL edit box * hFldPath Path edit box * hBtnGo "Search" button ***************************/ void layout(HWND hParent, HWND hFldURL, HWND hFldPath, HWND hBtnGo) { LPRECT prWindow; //window's rectangle int iWidth; //width of window /* The rules for the control layout: * URL and Path fields are stacked, spanning the * window, with the 'Go' button below, fixed width */ lerror((long)hParent); // GetClientRect(hParent, prWindow); iWidth =3D 500; // SetWindowPos(hFldURL, NULL, 10, 10, (10+iWidth), 25, SWP_NOACTIVATE | = SWP_NOZORDER); // SetWindowPos(hFldPath, NULL, 10, 45, (10+iWidth), 25, SWP_NOACTIVATE = | SWP_NOZORDER); // SetWindowPos(hBtnGo, NULL, 10, 90, (10+iWidth), 25, SWP_NOACTIVATE | = SWP_NOZORDER); } |