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?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,875 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,516 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,631 questions
{count} votes

Accepted answer
  1. RLWA32 43,051 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