VC内存栈溢出_求解
#include <Windows.h>
#include <stdio.h>
HWND g_hWnd;
HRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool initWin(HINSTANCE hInstance, LPCTSTR pClassName, LPCTSTR pAppName)
{
WNDCLASS wc;
wc.cbClsExtra = 4;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = 0;
wc.hIcon = 0;
wc.hInstance = hInstance;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.lpszClassName = pClassName;
wc.lpszMenuName = 0;
wc.style = CS_HREDRAW|CS_VREDRAW;
if (!RegisterClass(&wc))
{
return false;
}
g_hWnd = CreateWindow(pClassName, pAppName, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (!g_hWnd)
{
return false;
}
ShowWindow(g_hWnd, SW_SHOW);
UpdateWindow(g_hWnd);
return true;
}
HRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int CALLBACK WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
LPCTSTR pClassName = TEXT("WinMsgClass");
LPCTSTR pAppName = TEXT("WinMsgApp");
initWin(hInstance, pClassName, pAppName);
int a = 100;
SetClassLong(g_hWnd, 0L, a);
int b = GetClassLong(g_hWnd, 0);
TCHAR buf[9] = {0};
int size = sizeof("CLS: %d"); //运行后,当点击了对话按钮后,点击窗口关闭按钮时出错。程序回弹出中断程序对话框
swprintf_s(buf, sizeof(buf), TEXT("CLS: %d"), b);
::MessageBox(0, buf, 0, 0);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}