winMain error C++

Komissarov Maxim 41 Reputation points
2022-11-22T22:25:59.15+00:00

I wrote a simple app code using WINAPI. Visual Studio detects no errors but while building appears an error of "external symbol". Unresolved external symbol WinMain referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ);

here's the code:

include "windows.h"

LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI winMain(In HINSTANCE hInst, _In_opt_ HINSTANCE hPrevInst, In LPSTR lpCmdLine, In int nCmdShow) {
TCHAR zcClassName[] = L"Window Class";
HWND hMainWnd;
MSG msg;
WNDCLASSEX wndClass;

wndClass.cbSize = sizeof(wndClass);  
wndClass.style = CS_HREDRAW | CS_VREDRAW;  
wndClass.lpfnWndProc = wndProc;  
wndClass.lpszMenuName = NULL;  
wndClass.lpszClassName = zcClassName;  
wndClass.cbWndExtra = NULL;  
wndClass.cbClsExtra = NULL;  
wndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);  
wndClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);  
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);  
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);  
wndClass.hInstance = hInst;  

if (!RegisterClassEx(&wndClass)) {  
    MessageBox(NULL, L"Class Not Registered", L"FATAL ERROR", MB_OK);  
    return NULL;  
}  

hMainWnd = CreateWindow(zcClassName, L"Test Program", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, NULL, CW_USEDEFAULT, NULL, (HWND)NULL, NULL, HINSTANCE(hInst), NULL);  

ShowWindow(hMainWnd, nCmdShow);  
UpdateWindow(hMainWnd);  
while (GetMessage(&msg, NULL, NULL, NULL)) {  
    TranslateMessage(&msg);  
    DispatchMessage(&msg);  
}  
return msg.wParam;  

}

LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return wndProc(hWnd, msg, wParam, lParam);
}
}

why isn't it compiling?

Windows development | Windows API - Win32
Developer technologies | C++
Developer technologies | Visual Studio | Other
{count} votes

Accepted answer
  1. RLWA32 49,636 Reputation points
    2022-11-22T23:43:38.973+00:00

    The function name should be WinMain, not winMain.

    The window procedure (wndProc) should call DefWindowProc when handling the default case in the switch statement. Also, the function as written fails to return a value after handling WM_CREATE and WM_DESTROY.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.