Share via

winMain error C++

Komissarov Maxim 41 Reputation points
Nov 22, 2022, 10:25 PM

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.
5,449 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,751 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,899 questions
{count} votes

Accepted answer
  1. RLWA32 47,951 Reputation points
    Nov 22, 2022, 11:43 PM

    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.