关于《Windows程序设计(第五版)》中一个实例程序的疑问

最近一直在看Charlse Petzold的《Windows程序设计》,作为一个新得不能再新的新手,只能先照着书的抄抄源码了,之前的例子一直都很正常,但昨天遇到一个很诡异的BUG。

先看实例源码吧:

 /*-----------------------------------------------------------------
ENVIRON.C -- Environment List Box
(c) Charles Petzold,1998
Copy by XXXX,2015/1/26
------------------------------------------------------------------*/ #include <windows.h> #define ID_LIST 1
#define ID_TEXT 2 LRESULT CALLBACK WndProc(HWND hwnd, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Environ");
HWND hwnd;
MSG msg;
WNDCLASS wndclass; wndclass.style = CS_VREDRAW | CS_HREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return ;
} hwnd = CreateWindow(szAppName, TEXT("Environment List Box"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} void FillListBox(HWND hwndList)
{
int iLength;
TCHAR * pVarBlock, *pVarBeg, *pVarEnd, *pVarName;
pVarBlock = GetEnvironmentStrings(); //Get pointer to environment block while (*pVarBlock)
{
if (*pVarBlock != '=') //Skip variable names begining with '='
{
pVarBeg = pVarBlock; //Beging of variable name
while (*(pVarBlock++) != '='); //Scan until '='
pVarEnd = pVarBlock - ; //Points to '=' sign
iLength = pVarEnd - pVarBeg; //Length of variable name //Allocate memory for the variable name and terminating
//zero.Copy the variable name and append a zero
pVarName = calloc(iLength + , sizeof(TCHAR));
CopyMemory(pVarName, pVarBeg, iLength * sizeof(TCHAR));
pVarName[iLength] = '\0'; //Put the variable name int the list box and free memory
SendMessage(hwndList, LB_ADDSTRING, , (LPARAM)pVarName);
free(pVarName);
}
while (*pVarBlock++ != '\0'); //Scna until terminating zero }
FreeEnvironmentStrings(pVarBlock);
int code;
code = GetLastError();
} LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndList, hwndText;
int iIndex, iLength, cxChar, cyChar;
TCHAR * pVarName, *pVarValue; switch (message)
{
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits()); //Create listbox and static text windows
hwndList = CreateWindow(TEXT("listbox"), NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD,
cxChar, cyChar * , cxChar * + GetSystemMetrics(SM_CXVSCROLL), cyChar * , hwnd,
(HMENU)ID_LIST, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
hwndText = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE | SS_LEFT, cxChar, cyChar,
GetSystemMetrics(SM_CXSCREEN), cyChar, hwnd, (HMENU)ID_TEXT,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
FillListBox(hwndList);
return ; case WM_SETFOCUS:
SetFocus(hwndList);
return ; case WM_COMMAND:
if (LOWORD(wParam) == ID_LIST&&HIWORD(wParam) == LBN_SELCHANGE)
{
//Get current selectiong
iIndex = SendMessage(hwndList, LB_GETCURSEL, , );
iLength = SendMessage(hwndList, LB_GETTEXTLEN, iIndex, ) + ;
pVarName = calloc(iLength, sizeof(TCHAR));
SendMessage(hwndList, LB_GETTEXT, iIndex, (LPARAM)pVarName); //Get environment string
iLength = GetEnvironmentVariable(pVarName, NULL, );
pVarValue = calloc(iLength, sizeof(TCHAR));
GetEnvironmentVariable(pVarName, pVarValue, iLength); //Show it in window
SetWindowText(hwndText, pVarValue);
free(pVarName);
free(pVarValue);
}
return ; case WM_DESTROY:
PostQuitMessage();
return ;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

编译什么的都没有错误提示,但是在本地调试时提示触发一个断点,但是我确定我没有下过断点!!!

提示如图:关于《Windows程序设计(第五版)》中一个实例程序的疑问

选择“继续”后会又提示一个中断,提示堆被损坏

关于《Windows程序设计(第五版)》中一个实例程序的疑问

作为一个小彩笔,真心弄不懂,求教大牛!!!

上一篇:网络爬虫中Fiddler抓取PC端网页数据包与手机端APP数据包


下一篇:【JAVA编码专题】深入分析 Java 中的中文编码问题