这个MSVC ++编译错误意味着什么

时间:2011-06-12 05:57:06

标签: c++ winapi visual-c++

我有这个编译错误,我不明白有什么问题。我的Microsoft Visual Studio项目是一个Win32项目(而不是控制台):

1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16
1>C:\Users\Soribo\Desktop\C++ Programming\Visual C++ Programming\KeyboardHook\Release\KeyboardHook.exe : fatal error LNK1120: 1 unresolved externals

编辑:将#include“stdafx.h”作为第一行后编译错误为:

1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16
1>C:\Users\Soribo\Desktop\C++ Programming\Visual C++ Programming\KeyboardHook\Release\KeyboardHook.exe : fatal error LNK1120: 1 unresolved externals

编辑:嗯,我已经定义了WinMain功能还没有?见下面的代码:

/*
  Application: 
*/

#include <windows.h>
#include <cstdlib>
#include "stdafx.h"

using namespace std;

static HHOOK     keyboardHook;
static HINSTANCE gInstance;


// Functions List //
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam );
HHook ActivateKeyboardHook( HookProc hookProc, HINSTANCE hInstance );
bool DeactivateKeyboardHook( HHook keyboardHook );


int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
//int WINAPI WinMain( HINSTANCE gInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = gInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(DKGRAY_BRUSH);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = L"Custom Class";
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    // if registration of main class fails
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, L"Window Registration Failed!", L"Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        L"Custom Class",
        L"App Name",
        WS_CAPTION|WS_MINIMIZEBOX|WS_VISIBLE|WS_OVERLAPPED|WS_SYSMENU,
        CW_USEDEFAULT, CW_USEDEFAULT, 600, 500,
        NULL, NULL, gInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, L"Window Creation Failed!", L"Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )
{
    /*if ( code < 0 )
    {
        return CallNextHookEx( NULL, code, wParam, lParam );
    }*/  

    switch ( wParam )
    {
        case WM_KEYDOWN:
        {
            MessageBox( NULL, L"Notify", L"Key Down", MB_OK );        
        }
        break;
        case WM_KEYUP:
        {
            MessageBox( NULL, L"Notify", L"Key Up", MB_OK );                        
        }
        break;
        case WM_SYSKEYDOWN:
        {
            MessageBox( NULL, L"Notify", L"Sys Key Down", MB_OK );                        
        }
        break;
        case WM_SYSKEYUP:
        {
            MessageBox( NULL, L"Notify", L"Sys Key Up", MB_OK );                        
        }
        break;
        default:
        {
        } 
        break;
    }

    return CallNextHookEx( NULL, nCode, wParam, lParam );
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch(msg)
    {
        case WM_CREATE:
        {                  
             keyboardHook = ActivateKeyboardHook( &LowLevelKeyboardProc, gInstance );
        }    
        break;
        case WM_COMMAND:
        {
             switch(LOWORD(wParam)) 
             {

                  default:
                  break;
             }
        }
        break;
        case WM_CLOSE:
        {
            DeactivateKeyboardHook( keyboardHook );
            DestroyWindow(hwnd);
        }
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default: 
        break;
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}

HHOOK ActivateKeyboardHook( HookProc hookProc, HINSTANCE hInstance )
{
     return SetWindowsHookEx( WH_KEYBOARD_LL, hookProc, hInstance, 0 );
}

bool DeactivateKeyboardHook( HHook keyboardHook )
{
     return UnhookWindowsHookEx( keyboardHook );
}

2 个答案:

答案 0 :(得分:6)

这不是编译错误,即链接器错误,这意味着您的程序没有定义WinMain函数,这是程序的入口点。

确保您的程序具有此功能:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);

请参阅此MSDN文档:


#include <windows.h>
#include <cstdlib>
#include "stdafx.h"

如果您选择了预编译的头文件,那么上面的内容是错误的,stdafx.h应该包含在文件的开头。因此,将订单更改为:

#include "stdafx.h"  //this should be first line of the program!
#include <windows.h>
#include <cstdlib>

我认为您不需要包含<windows.h>,因为stdafx.h已经包含了stdafx.h。看看吧。

现在为什么要先包括它?因为顾名思义,预编译头是一个预编译的头。编译器不会每次都编译它。相反,它会编译一次其中的所有内容。如果您不首先包含它,编译器将不知道是否编译之前包含的文件,因为这些文件可能已包含在{{1}}中,因此已经编译。请参阅此主题:

答案 1 :(得分:0)

这不是编译错误,而是链接错误。基本上,您使用的是您在某处声明的函数,但从未在任何翻译单元中定义过。