首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

Unhandled exception in 按钮.exe: 0xC0000005: Access Violation 解决办法

2012-02-25 
Unhandledexceptionin按钮.exe:0xC0000005:AccessViolation- C++ Builder / Windows SDK/API#includewind

Unhandled exception in 按钮.exe: 0xC0000005: Access Violation - C++ Builder / Windows SDK/API

#include<windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
TCHAR szAppName[]=TEXT("按钮");
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
  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) GetStockObject (WHITE_BRUSH) ;
  wndclass.lpszMenuName = szAppName ;
  wndclass.lpszClassName = szAppName ;
  if (!RegisterClass (&wndclass))
  {
  MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  szAppName, MB_ICONERROR) ;
  return 0 ;
  }
   
  hwnd = CreateWindow (szAppName, TEXT ("Menu Demonstration"),
  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 ;
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  TEXTMETRIC tm;
  HWND hwnda;
  static int cxChar,cyChar;
  switch(message)
  {
  case WM_CREATE:
{
 
  HDC hdc = GetDC (hwnd) ;
  GetTextMetrics (hdc, &tm) ;
  cxChar = tm.tmAveCharWidth ;
  cyChar = tm.tmHeight + tm.tmExternalLeading ;
  ReleaseDC (hwnd, hdc) ;
hwnda= CreateWindow ("button", "确定",
  WS_CHILD | WS_VISIBLE|BS_AUTO3STATE,
  0, 0,
  cxChar*10, cyChar*5,
  hwnd, (HMENU)1,
  ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
}break;
  case WM_SIZE:
{
 
}break;
  case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case 1:
int i=(int)SendMessage(hwnda,BM_GETCHECK,0,0);
TCHAR *i1;
itoa(i,i1,10);
MessageBox(hwnd,i1,TEXT(""),MB_OK);break;
}
}break;
  case WM_DESTROY :
  PostQuitMessage (0) ;
  break;
  }
  return DefWindowProc (hwnd, message, wParam, lParam) ;
}

不要看以上那么复杂的代码,关键是一下这句------
case 1:
int i=(int)SendMessage(hwnda,BM_GETCHECK,0,0);
TCHAR *i1;
itoa(i,i1,10);
MessageBox(hwnd,i1,TEXT(""),MB_OK);break;
我企图返回BS_AUTO3STATE这个控制的状态按不知为何抛出了以上异常?
高手帮忙。

[解决办法]
i1没分配内存
TCHAR i1[32];

热点排行