鱼C论坛

 找回密码
 立即注册
查看: 2264|回复: 1

windows程序设计书上的例子,运行时摄氏,为什么啊

[复制链接]
发表于 2013-12-12 11:05:13 | 显示全部楼层 |阅读模式
1鱼币
代码如下:

运行到这一句时报错了,是什么原因啊


FreeEnvironmentStrings (pVarBlock) ;
/*----------------------------------------
ENVIRON.C -- Environment List Box
(c) Charles Petzold, 1998
----------------------------------------*/

#include <windows.h>

#define ID_LIST 1
#define ID_TEXT 2

LRESULT CALLBACK WndProc (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_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

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, 0, 0))
{
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 beginning with '='
{
pVarBeg = pVarBlock ; // Beginning of variable name
while (*pVarBlock++ != '=') ; // Scan until '='
pVarEnd = pVarBlock - 1 ; // 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 + 1, sizeof (TCHAR)) ;
CopyMemory (pVarName, pVarBeg, iLength * sizeof (TCHAR)) ;
pVarName[iLength] = '\0' ;

// Put the variable name in the list box and free memory.

SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) pVarName) ;
free (pVarName) ;
}
while (*pVarBlock++ != '\0') ; // Scan until terminating zero
}
FreeEnvironmentStrings (pVarBlock) ;      //报错的地方
}

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 * 3,
cxChar * 16 + GetSystemMetrics (SM_CXVSCROLL),
cyChar * 5,
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 0 ;

case WM_SETFOCUS :
SetFocus (hwndList) ;
return 0 ;

case WM_COMMAND :
if (LOWORD (wParam) == ID_LIST && HIWORD (wParam) == LBN_SELCHANGE)
{
// Get current selection.

iIndex = SendMessage (hwndList, LB_GETCURSEL, 0, 0) ;
iLength = SendMessage (hwndList, LB_GETTEXTLEN, iIndex, 0) + 1 ;
pVarName = calloc (iLength, sizeof (TCHAR)) ;
SendMessage (hwndList, LB_GETTEXT, iIndex, (LPARAM) pVarName) ;

// Get environment string.

iLength = GetEnvironmentVariable (pVarName, NULL, 0) ;
pVarValue = calloc (iLength, sizeof (TCHAR)) ;
GetEnvironmentVariable (pVarName, pVarValue, iLength) ;

// Show it in window.

SetWindowText (hwndText, pVarValue) ;
free (pVarName) ;
free (pVarValue) ;
}
return 0 ;

case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-12-12 17:57:37 | 显示全部楼层
为毛我的编译都通过呢大哥! 刚开始报错说是“libcmtd.lib(crt0.obj) : error LNK2001: unresolved external symbol _main  ”,后来我把vc设置了一下,可以了没有报错!也没有出现你说的问题啊。有图有真相,看图:

你把报错的图传上来呗,有图有真相的!:lol:

运行后的图

运行后的图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-23 02:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表