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)"
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C++
Developer technologies | 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.
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Minxin Yu 13,506 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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.