2011-09-05 127 views
0

我是Windows编程的新手。编程Windows编译链接错误 - 无法解析的外部

我用VS2005创建了一个win32控制台项目(没有预编译头文件)。代码如下。

// HelloWin.cpp : Defines the entry point for the console application. 
// 
#include <windows.h> 
#include "stdafx.h" 

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

int _tmain(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 XP"), -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); 
} 

现在有两个链接错误,因此,有谁能帮我解决这个错误。

是否由我的本地硬盘中没有hellowin.wav文件引起?如果是的话。什么目录可以放置一个类似的WAV文件?

谢谢。

1>链接... 1> HelloWin.obj:

错误LNK2019:解析的外部 符号imp__PlaySoundW @ 12在函数引用“长__stdcall 的WndProc(结构HWND *,无符号整型,无符号整型,长)” (的WndProc @@ YGJPAUHWND __ @@ @ IIJ Z) 1> d:\学习\ Windows \ ProgrammingWindows(5thEdition)\ HELLOWIN \调试\ HelloWin.exe :

致命错误LNK1120 :1个未解析的外部1>构建日志w保存在

“file:// d:\ learning \ windows \ ProgrammingWindows(5thEdition)\ HelloWin \ HelloWin \ Debug \ BuildLog”.htm“ 1> HelloWin - 2个错误,1个警告(s ) ==========全部重建:0次成功,1次失败,0次跳过==========

回答

1

这里有几个问题。首先你说你的应用程序是一个控制台应用程序。如果是,那么你使用的是错误的主体。您的子系统可能是WINDOWS在这种情况下,你的主要应该是这个:

int CALLBACK _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR szCmdLine, int iCmdShow) 

你也说你不使用预编译头,但你有一个包括stdafx.h。你的问题可以通过固定改变你的主,也改变你包括看起来像这样:

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

这使得链接器查找库文件,我们告诉它。我也摆脱了您的预编译头文件,并将其替换为tchar.h,因为这是您迄今唯一的其他依赖项。

事实hellowin.wav不在编译时间是无关紧要的。该程序仅在运行时查找它。

+0

我意识到我的问题。我重新创建了一个空的Win32项目。并添加了一个空的'helloWin.c'文件。这一次,我使用了你的主函数和头文件。它工作得很好。非常感谢。 –

+0

我测试了一下,发现'int CALLBACK _tWinMain()'中的'CALLBACK'可以被忽略。你能给我更多关于'CALLBACK'关键字的信息吗?谢谢。 –

+1

http://blogs.msdn.com/b/oldnewthing/archive/2011/05/06/10161590.aspx - 这可以比我更好地解释它。长话短说,不要拿出来。 –

相关问题