为什么我的Windows程序试图调用main()而不是WinMain()?

时间:2012-06-05 17:17:21

标签: c winapi visual-c++

我正在尝试向OpenGL迈出第一步。

但是,由于尝试调试解决方案时出现此错误,似乎不会发生这种情况:

  

MSVCRTD.lib(crtexe.obj):错误LNK2019:函数_ _tmainCRTStartup

中未解析的外部符号主要引用

我知道编译器希望看到int main() ...,但它不会看到WinMain调用吗?

以下是代码:

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    HWND hWnd;
} Glab_t;

static Glab_t glab;

char szClassName[ ] = "GLab";

static LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {

    MSG messages;
    RECT rect;
    WNDCLASSEX wndClass;
    int screenWidth, screenHeight;
    int x, y, w, h;

    screenWidth = GetSystemMetrics(SM_CXSCREEN);
    screenHeight = GetSystemMetrics(SM_CYSCREEN);

    rect.left = (screenWidth - 582) / 2;
    rect.top = (screenHeight - 358) / 2;
    rect.right = rect.left + 582;
    rect.bottom = rect.top + 358;

    x = rect.left;
    y = rect.top;
    w = 640;
    h = 480;



    wndClass.hInstance = hInstance;
    wndClass.lpszClassName = szClassName;
    wndClass.lpfnWndProc = WindowProcedure;
    wndClass.style = CS_DBLCLKS;
    wndClass.cbSize = sizeof (WNDCLASSEX);
    wndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wndClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
    wndClass.lpszMenuName = NULL;
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;
    wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);

    if (!RegisterClassEx (&wndClass)) {
        return 0;
    }

    glab.hWnd = CreateWindowEx (
           0,
           szClassName,
           "GLab - OpenGL",
           WS_OVERLAPPEDWINDOW,
           x,
           y,
           w,
           h,
           HWND_DESKTOP,
           NULL,
           hInstance,
           NULL 
           );

    ShowWindow (glab.hWnd, nCmdShow);
    while (GetMessage (&messages, NULL, 0, 0)) {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

    return true;
}

我正在使用MS Visual C ++ 2010 Express。

2 个答案:

答案 0 :(得分:4)

您有一个子系统Console而非Windows的项目。从项目属性中更改它,它将起作用。那是在链接器 - &gt;系统 - &gt;子系统

答案 1 :(得分:2)

您必须更改项目的属性; Console项目通常会查找main()函数,而Windows项目则会查找WinMain()。