2 s594102234 s594102234 于 2015.05.27 15:01 提问

win32编程中窗口不显示是什么问题

《windows程序设计》第9章ENVIRON程序
把光盘中的源代码粘贴出来发现还是没有窗口在运行,实在想不明白
(运行环境:VS2013,WINDOWS7)
以下源代码:

#include

#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) ;

}
运行时没有显示错误
图片说明

4个回答

oyljerry
oyljerry   Ds   Rxr 2015.05.27 15:27
已采纳

估计窗口没有创建成功等,失败了,你可以设置断点跟踪一下,看你的代码在哪里出错了

s594102234
s594102234 回复s594102234: 不是书太旧了,是个bug,指针已经改变了原来的位置,应该设置一个临时指针,最后释放临时指针
接近 3 年之前 回复
s594102234
s594102234 我已经解决这个问题了,我在FreeEnvironmentStrings( pVarBlock )前面加上一句if ( *pVarBlock ) ,就可以运行了,看来是书太旧了,估计在以前的VS上可以运行
接近 3 年之前 回复
s594102234
s594102234 我点了一下调试,发现有一个断点在 FreeEnvironmentStrings( )这个函数上,但我还是不知道怎么解决这个问题
接近 3 年之前 回复
albertbush
albertbush   2015.05.27 15:11

CreateWindow后,看一下是否取得hWnd。如果hWnd==INVALID_HANDLE_VALUE(或者NULL),要去做GetLastError()获取错误原因。根据LastError分析原因

caozhy
caozhy   Ds   Rxr 2015.05.27 15:08

你的程序用了好几个资源,比如图标、光标字符串,这些资源你都附加到程序里了么?还是你只是复制粘贴了程序。

s594102234
s594102234 那些东西是系统里就有的还要添加吗?运行之前的程序也用了这些东西,没有添加也可以运行
接近 3 年之前 回复
u014316901
u014316901   2015.05.27 15:46

转换函数calloc 的返回指针类型后编译通过,并出现界面。
编译环境:
vs2012 WINDOWS8.1

s594102234
s594102234 那个问题VS检测出来了,c++语法不通过,c可以通过
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!