2011-09-05 128 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 - 這可以比我更好地解釋它。長話短說,不要拿出來。 –

相關問題