注册 登录
编程论坛 C++教室

Windows程序设计上的代码 怎么会编译不通过呢

墨香555 发布于 2011-09-27 23:20, 1476 次点击
#include <windows.h>
        

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

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
        
                   PSTR szCmdLine, int iCmdShow)
        
{
        
    static TCHAR szAppName[] = TEXT ("HelloWin") ;
        
    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  = NULL ;
        
    wndclass.lpszClassName= szAppName ;
        

    if (!RegisterClass (&wndclass))
        
    {
        
            MessageBox (  NULL, TEXT ("This program requires Windows NT!"),
        
                                  szAppName, MB_ICONERROR) ;
        
            return 0 ;
        
    }
        
    hwnd = CreateWindow( szAppName,      // window class name
        
                   TEXT ("The Hello Program"),   // window caption
        
                   WS_OVERLAPPEDWINDOW,  // window style
        
                   CW_USEDEFAULT,// initial x position
        
                   CW_USEDEFAULT,// initial y position
        
                   CW_USEDEFAULT,// initial x size
        
                   CW_USEDEFAULT,// initial y size
        
                   NULL,                 // parent window handle
        
                   NULL,            // window menu handle
        
                   hInstance,   // program instance handle
        
                   NULL) ;      // creation parameters
        
   
        
    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)
        
{
        
    HDC                   hdc ;
        
    PAINTSTRUCT ps ;
        
    RECT          rect ;
        
   
        
    switch (message)
        
    {
        
    case WM_CREATE:
        
    PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
        
            return 0 ;
        

    case   WM_PAINT:
        
            hdc = BeginPaint (hwnd, &ps) ;
        
        
        
            GetClientRect (hwnd, &rect) ;
        
        
        
            DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
        
                   DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
        
                 EndPaint (hwnd, &ps) ;
        
                   return 0 ;
        
        
        
    case   WM_DESTROY:
        
            PostQuitMessage (0) ;
        
            return 0 ;
        
    }
        
  return DefWindowProc (hwnd, message, wParam, lParam) ;
        
}


我是新手 请大家帮我解释一下 为什么 谢谢啦


运行时提示错误:
1>------ 已启动生成: 项目: MJ9, 配置: Debug Win32 ------
1>  5.cpp
1>5.obj : error LNK2019: 无法解析的外部符号 __imp__PlaySoundW@12,该符号在函数 "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) 中被引用
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
1>d:\my documents\visual studio 2010\Projects\MJ9\Debug\MJ9.exe : fatal error LNK1120: 2 个无法解析的外部命令
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
9 回复
#2
八画小子2011-09-28 00:21
把   PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ; 一句删了
#3
墨香5552011-09-28 08:14
回复 2楼 八画小子
还是不行啊 还是那个提示
#4
czsbc2011-09-28 09:05
以Win32 Application建立工程。
#5
墨香5552011-09-28 09:51
回复 4楼 czsbc
为什么不能用win32 console application 呢
#6
czsbc2011-09-28 10:17
win32 console application以main()函数作为程序入口点
而Win32 Application以WinMain()函数作为程序入口点
因为你用win32 console application建立工程,在链接时就会去找main()确定程序入口点。
因为没有找到main(),所以会产生"无法解析的外部符号 _main"这个错误。

#7
mengcan5552011-10-02 22:33
楼上正解
#8
易木马2011-10-04 18:16
以下是引用czsbc在2011-9-28 10:17:31的发言:

win32 console application以main()函数作为程序入口点
而Win32 Application以WinMain()函数作为程序入口点
因为你用win32 console application建立工程,在链接时就会去找main()确定程序入口点。
因为没有找到main(),所以会产生"无法解析的外部符号 _main"这个错误。


正确!顶一个
#9
longwu872011-10-04 22:53
即使你编程很牛,你回头学基础的时候也回发现很多收获
#10
pauldf2013-07-05 14:58
楼主尝试在cpp开头补上一句:

#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")


我刚才遇到同样的问题,加了这个就解决了。
1