鱼C论坛

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

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

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

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


FreeEnvironmentStrings (pVarBlock) ;

  1. /*----------------------------------------
  2. ENVIRON.C -- Environment List Box
  3. (c) Charles Petzold, 1998
  4. ----------------------------------------*/

  5. #include <windows.h>

  6. #define ID_LIST 1
  7. #define ID_TEXT 2

  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

  9. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  10. PSTR szCmdLine, int iCmdShow)
  11. {
  12. static TCHAR szAppName[] = TEXT ("Environ") ;
  13. HWND hwnd ;
  14. MSG msg ;
  15. WNDCLASS wndclass ;

  16. wndclass.style = CS_HREDRAW | CS_VREDRAW ;
  17. wndclass.lpfnWndProc = WndProc ;
  18. wndclass.cbClsExtra = 0 ;
  19. wndclass.cbWndExtra = 0 ;
  20. wndclass.hInstance = hInstance ;
  21. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
  22. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
  23. wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
  24. wndclass.lpszMenuName = NULL ;
  25. wndclass.lpszClassName = szAppName ;

  26. if (!RegisterClass (&wndclass))
  27. {
  28. MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  29. szAppName, MB_ICONERROR) ;
  30. return 0 ;
  31. }

  32. hwnd = CreateWindow (szAppName, TEXT ("Environment List Box"),
  33. WS_OVERLAPPEDWINDOW,
  34. CW_USEDEFAULT, CW_USEDEFAULT,
  35. CW_USEDEFAULT, CW_USEDEFAULT,
  36. NULL, NULL, hInstance, NULL) ;

  37. ShowWindow (hwnd, iCmdShow) ;
  38. UpdateWindow (hwnd) ;

  39. while (GetMessage (&msg, NULL, 0, 0))
  40. {
  41. TranslateMessage (&msg) ;
  42. DispatchMessage (&msg) ;
  43. }
  44. return msg.wParam ;
  45. }

  46. void FillListBox (HWND hwndList)
  47. {
  48. int iLength ;
  49. TCHAR * pVarBlock, * pVarBeg, * pVarEnd, * pVarName ;

  50. pVarBlock = GetEnvironmentStrings () ; // Get pointer to environment block

  51. while (*pVarBlock)
  52. {
  53. if (*pVarBlock != '=') // Skip variable names beginning with '='
  54. {
  55. pVarBeg = pVarBlock ; // Beginning of variable name
  56. while (*pVarBlock++ != '=') ; // Scan until '='
  57. pVarEnd = pVarBlock - 1 ; // Points to '=' sign
  58. iLength = pVarEnd - pVarBeg ; // Length of variable name

  59. // Allocate memory for the variable name and terminating
  60. // zero. Copy the variable name and append a zero.

  61. pVarName = calloc (iLength + 1, sizeof (TCHAR)) ;
  62. CopyMemory (pVarName, pVarBeg, iLength * sizeof (TCHAR)) ;
  63. pVarName[iLength] = '\0' ;

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

  65. SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) pVarName) ;
  66. free (pVarName) ;
  67. }
  68. while (*pVarBlock++ != '\0') ; // Scan until terminating zero
  69. }
  70. FreeEnvironmentStrings (pVarBlock) ;      //报错的地方
  71. }

  72. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  73. {
  74. static HWND hwndList, hwndText ;
  75. int iIndex, iLength, cxChar, cyChar ;
  76. TCHAR * pVarName, * pVarValue ;

  77. switch (message)
  78. {
  79. case WM_CREATE :
  80. cxChar = LOWORD (GetDialogBaseUnits ()) ;
  81. cyChar = HIWORD (GetDialogBaseUnits ()) ;

  82. // Create listbox and static text windows.

  83. hwndList = CreateWindow (TEXT ("listbox"), NULL,
  84. WS_CHILD | WS_VISIBLE | LBS_STANDARD,
  85. cxChar, cyChar * 3,
  86. cxChar * 16 + GetSystemMetrics (SM_CXVSCROLL),
  87. cyChar * 5,
  88. hwnd, (HMENU) ID_LIST,
  89. (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  90. NULL) ;

  91. hwndText = CreateWindow (TEXT ("static"), NULL,
  92. WS_CHILD | WS_VISIBLE | SS_LEFT,
  93. cxChar, cyChar,
  94. GetSystemMetrics (SM_CXSCREEN), cyChar,
  95. hwnd, (HMENU) ID_TEXT,
  96. (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  97. NULL) ;

  98. FillListBox (hwndList) ;
  99. return 0 ;

  100. case WM_SETFOCUS :
  101. SetFocus (hwndList) ;
  102. return 0 ;

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

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

  111. // Get environment string.

  112. iLength = GetEnvironmentVariable (pVarName, NULL, 0) ;
  113. pVarValue = calloc (iLength, sizeof (TCHAR)) ;
  114. GetEnvironmentVariable (pVarName, pVarValue, iLength) ;

  115. // Show it in window.

  116. SetWindowText (hwndText, pVarValue) ;
  117. free (pVarName) ;
  118. free (pVarValue) ;
  119. }
  120. return 0 ;

  121. case WM_DESTROY :
  122. PostQuitMessage (0) ;
  123. return 0 ;
  124. }
  125. return DefWindowProc (hwnd, message, wParam, lParam) ;
  126. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

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

运行后的图

运行后的图
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-12 02:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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