WinMain code doesn't work

Gavin Williams 761 Reputation points
2023-11-13T01:17:39.2933333+00:00
#include <Windows.h>

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevious, PSTR cmdLine, int nCmdShow)
{
	return MessageBox(NULL, "hello, world", "caption", 0);
}

This code, straight off the docs page doesn't work in a couple of ways.

  1. The string literals in the call to MessageBox are invalid - argument of type const char * is incompatible with parameter of type LPCWSTR
  2. Even if I replace the MessageBox call with MessageBoxA to workaround that issue I get unresolved external symbol main referenced in function "int__cdecl invoke_main(void)"
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,448 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,889 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
11,143 questions
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 13,246 Reputation points Microsoft External Staff
    2023-11-13T01:30:27.7066667+00:00

    Hi, Gavin Williams

    1. MessageBox(NULL, "hello, world", "caption", 0); applies to non-Unicode character sets. Add L if using Unicode
    return MessageBox(NULL, L"hello, world", L"caption", 0);
    

    User's image

    1. Set Sub System-> Windows. enter image description here

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

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.