Recently has been looking at Charlse Petzold's "Windows Programming", as a new can not be new novice, can only follow the book copy copy source, the previous example has been very normal, but yesterday encountered a very strange bug.
Let's look at the example source code:
1 /*-----------------------------------------------------------------2 ENVIRON. C--Environment List Box3 (c) Charles petzold,19984 Copy by Xxxx,2015/1/265 ------------------------------------------------------------------*/6 7#include <windows.h>8 9 #defineId_list 1Ten #defineId_text 2 One A LRESULT CALLBACK WndProc (HWND hwnd, UINT, WPARAM, LPARAM); - - intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, PSTR szCmdLine,inticmdshow) the { - StaticTCHAR szappname[] = TEXT ("Environ"); - HWND hwnd; - msg msg; + wndclass wndclass; - +Wndclass.style = Cs_vredraw |Cs_hredraw; AWndclass.lpfnwndproc =WndProc; atWndclass.cbclsextra =0; -Wndclass.cbwndextra =0; -Wndclass.hinstance =hinstance; -Wndclass.hicon =LoadIcon (NULL, idi_application); -Wndclass.hcursor =loadcursor (NULL, idc_arrow); -Wndclass.hbrbackground =(Hbrush) getstockobject (White_brush); inWndclass.lpszmenuname =NULL; -Wndclass.lpszclassname =Szappname; to + if(! RegisterClass (&wndclass)) - { theMessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, mb_iconerror); * return 0; $ }Panax Notoginseng -hwnd = CreateWindow (Szappname, TEXT ("Environment List Box"), Ws_overlappedwindow, the Cw_usedefault, Cw_usedefault, Cw_usedefault, cw_usedefault, NULL, NULL, HINSTANCE, NULL); + ShowWindow (hwnd, icmdshow); A UpdateWindow (HWND); the + while(GetMessage (&msg, NULL,0,0)) - { $TranslateMessage (&msg); $DispatchMessage (&msg); - } - returnMsg.wparam; the } - Wuyi the voidFilllistbox (HWND hwndlist) - { Wu intilength; -TCHAR * Pvarblock, *pvarbeg, *pvarend, *Pvarname; AboutPvarblock = Getenvironmentstrings ();//Get Pointer to environment block $ - while(*Pvarblock) - { - if(*pvarblock! ='=')//Skip variable names begining with ' = ' A { +Pvarbeg = Pvarblock;//beging of variable name the while(* (pvarblock++)! ='=');//Scan until ' = ' -Pvarend = Pvarblock-1;//Points to ' = ' sign $Ilength = Pvarend-pvarbeg;//Length of variable name the the //Allocate memory for the variable name and terminating the //Zero. Copy the variable name and append a zero thePvarname = calloc (Ilength +1,sizeof(TCHAR)); -CopyMemory (Pvarname, Pvarbeg, Ilength *sizeof(TCHAR)); inPvarname[ilength] =' /'; the the //Put the variable name int the list box and free memory AboutSendMessage (Hwndlist, lb_addstring,0, (LPARAM) pvarname); the Free (pvarname); the } the while(*pvarblock++! =' /');//Scna until terminating zero + - } the freeenvironmentstrings (Pvarblock); Bayi intCode; theCode =GetLastError (); the } - - the LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) the { the StaticHWND hwndlist, Hwndtext; the intIIndex, Ilength, Cxchar, Cychar; -TCHAR * Pvarname, *Pvarvalue; the the Switch(message) the {94 Casewm_create: theCxchar =LoWord (Getdialogbaseunits ()); theCychar =HiWord (Getdialogbaseunits ()); the 98 //Create ListBox and static text Windows AboutHwndlist = CreateWindow (TEXT ("ListBox"), NULL, Ws_child | ws_visible |Lbs_standard, -Cxchar, Cychar *3, Cxchar * -+ GetSystemMetrics (sm_cxvscroll), Cychar *5, hwnd,101 (HMENU) id_list, (HINSTANCE) GetWindowLong (hwnd, gwl_hinstance), NULL);102Hwndtext = CreateWindow (TEXT ("Static"), NULL, Ws_child | ws_visible |Ss_left, Cxchar, Cychar,103 GetSystemMetrics (Sm_cxscreen), Cychar, hwnd, (HMENU) Id_text,104 (HINSTANCE) GetWindowLong (hwnd, gwl_hinstance), NULL); the Filllistbox (hwndlist);106 return 0;107 108 109 CaseWm_setfocus: the SetFocus (hwndlist);111 return 0; the 113 the CaseWm_command: the if(LoWord (wParam) = = Id_list&&hiword (wParam) = =Lbn_selchange) the {117 //Get Current Selectiong118IIndex = SendMessage (Hwndlist, Lb_getcursel,0,0);119Ilength = SendMessage (Hwndlist, Lb_gettextlen, IIndex,0) +1; -Pvarname = Calloc (Ilength,sizeof(TCHAR));121 SendMessage (hwndlist, Lb_gettext, IIndex, (LPARAM) pvarname);122 123 //Get Environment String124Ilength = GetEnvironmentVariable (Pvarname, NULL,0); thePvarvalue = Calloc (Ilength,sizeof(TCHAR));126 getenvironmentvariable (Pvarname, Pvarvalue, ilength);127 - //Show it in Window129 SetWindowText (Hwndtext, pvarvalue); the Free (pvarname);131 Free (pvarvalue); the }133 return 0;134 135 136 CaseWm_destroy:137PostQuitMessage (0);138 return 0;139 } $ returnDefWindowProc (hwnd, message, WParam, lParam);141}
There is no error in compiling anything, but I am prompted to trigger a breakpoint when debugging locally, but I am sure I have not broken the breakpoint!!!
Tips
When you select continue, you will be prompted for an interrupt to indicate that the heap is damaged
As a small color pens, really do not understand, ask Daniel!!!
Questions about one instance program in Windows Programming (Fifth Edition)