菜鸟写窗口,丢掉WNDCLASS变量的成员,后果很严重

刚照着一本Windows程序设计教程,用API写了个窗口,用VC++6.0 编译 连接 都无错误,但是运行时候出错,程序运行不出来。弹出如下报错信息:


我机子的操作系统是 WindowsXP Pro SP3
程序的代码如下:

#include<windows.h>
 
LONG WINAPI WndProc(HWND,UINT,WPARAM,LPARAM);
 
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpszCmdLine,int nCmdShow)
{
    WNDCLASS wc;
    HWND hwnd;
    MSG msg;
 
    wc.style=0;                                      //class style
    wc.lpfnWndProc=(WNDPROC)WndProc;                 //Window procedure address
    wc.cbClsExtra = 0;                               //Class extra bytes
    wc.cbWndExtra = 0;                               //Window extra bytes
    wc.hInstance = hInstance;                        //Instance handle
    wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);           //Icon handle
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);         //Cursor handle
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);     //Background color
    wc.lpszClassName = "MyWndClass";                 //WNDCLASS name
 
    RegisterClass(&wc);
    hwnd = CreateWindow(
        "MyWndClass",                               //WNDCLASS name
        "SDK Application",                          //Window title
        WS_OVERLAPPEDWINDOW,                        //Window style
        CW_USEDEFAULT,                              //Horizontal position
        CW_USEDEFAULT,                              //Vertical position
        CW_USEDEFAULT,                              //Initial width
        CW_USEDEFAULT,                              //Initial height
        HWND_DESKTOP,                               //Handle of parent window
        NULL,                                       //Menu handle
        hInstance,                                  //Application's instance handle
        NULL                                        //Window-creation data
 
         
    );
 
    ShowWindow(hwnd,nCmdShow);
    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)
{
    PAINTSTRUCT ps;
    HDC hdc;
    switch(message)
    {
    case WM_PAINT:
        hdc  = BeginPaint(hwnd,&ps);
        Ellipse(hdc,0,0,200,100);
        EndPaint(hwnd,&ps);
 
        return 0;
 
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
 
    }
 
    return DefWindowProc(hwnd,message,wParam,lParam);
}
Compile和Build都没有问题,就是按F5调试运行出现那个“该内存不能为‘read’”.程序运行不出来,点击“确定”直接回到编码区;点击“取消”就进入了VC编译器中的汇编命令区。搞得我一头雾水!

在论坛上发帖求助高人,原来在WinMain函数中WNDCLASS wc;给wc的成员赋值时,忘了给wc.lpszMenuName赋值。

这种问题的解决方法大致有两招:1、初始化wc:WNDCLASS wc={0};
2、给wc.lpszMenuName赋值:wc.lpszMenuName=NULL 或wc.lpszMenuName=“Name”//此处Name可以是任意字符

posted on 2015-01-15 15:17  call_from_dream  阅读(352)  评论(0编辑  收藏  举报

导航