Habe ein Problem mit der MessageBox unter C++ Visual Studio 2022

Richard Wagner 1 Reputation point
2022-08-06T09:20:51.603+00:00

Ich möchte gerne programmieren lernen. Zu diesem Zweck arbeite ich gerade ein relativ altes Buch durch. Aus dem Jahre 2000. Ich habe ein leeres C++ Projekt erstellt und dort eine cpp-Datei eingefügt mit folgendem Inhalt:

define WIN32_LEAN_AND_MEAN

include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance, LPWSTR lpcmdline, int ncmdshow)
{
MessageBox(0, L"Hurra, man kann mich sehen!",
L"Mein erstes Windows-Programm",
MB_OK | MB_ICONEXCLAMATION);
return 0;
}

Dieser Code ist insofern angepasst, als dass ich vor den Zeichenketten ein L geschrieben habe, was offenbar notwendig ist, warum auch immer und ich habe statt LPSTR LPWSTR geschrieben. Fakt ist, dass es unter Dev C++ vollkommen normal funktioniert. Ohne irgendwelche Anpassungen. Dort habe ich einfach den Originalquellcode des alten Lehrbuches eingegeben. Bei Visual Studio 2022 schreibt er allerdings immer "Inkonsistente Anmerkungen.." (C28251) und "Überladen der Funktion nicht möglich" (C2731).

Meine Frage ist jetzt, warum startet dieses harmlose, primitive Programm unter Visual Studio nicht. Was muss ich tun, damit der Debugger nicht meckert? Das ist insofern auch nicht nachvollziehbar, als dass die Codebeispiele, die man im Internt von MICROSOFT finden kann. Im Prinzip genauso aussehen.

Developer technologies | C++
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-08-06T09:48:27.77+00:00

    If you check in the code generated by the C++/Win32 template, you can see the declaration in Unicode :

    int APIENTRY wWinMain(_In_ HINSTANCE hInstance,  
                         _In_opt_ HINSTANCE hPrevInstance,  
                         _In_ LPWSTR    lpCmdLine,  
                         _In_ int       nCmdShow)  
    
    0 comments No comments

  2. RLWA32 49,636 Reputation points
    2022-08-06T09:56:40.333+00:00

    There are various alternative that are related to the character set that your project is using.

    Written specifically for Unicode (UTF-16LE) -

    #define WIN32_LEAN_AND_MEAN  
    #include <Windows.h>  
      
    int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevious, LPWSTR lpCommandline, int nShow)  
    {  
        MessageBoxW(NULL, L"Unicode Text", L"Unicode Caption", MB_OK | MB_ICONINFORMATION);  
        return 0;  
    }  
    

    Written specifically for Non-Unicode -

    #define WIN32_LEAN_AND_MEAN  
    #include <Windows.h>  
      
    int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevious, LPSTR lpCommandline, int nShow)  
    {  
        MessageBoxA(NULL, "Non-Unicode Text", "Non-Unicode Caption", MB_OK | MB_ICONINFORMATION);  
        return 0;  
    }  
      
    

    Will build for either character set -

    #define WIN32_LEAN_AND_MEAN  
    #include <Windows.h>  
    #include <tchar.h>  
      
    int WINAPI _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevious, LPTSTR lpCommandline, int nShow)  
    {  
        MessageBox(NULL, TEXT("Build correctly for Unicode/Non-Unicode Text"), TEXT("Generic Text Caption"), MB_OK | MB_ICONINFORMATION);  
        return 0;  
    }  
      
    

  3. Richard Wagner 1 Reputation point
    2022-08-06T10:13:31.247+00:00

    Sorry but your answer is a joke. First I am not a professional programmer. I dont even know what you are talking about. I am a beginner who wanna learn C++. Second if I change the code completely like your template into

    #define WIN32_LEAN_AND_MEAN  
    #include <windows.h>  
      
    int APIENTRY wWinMain(_In_ HINSTANCE hInstance,  
    	_In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpcmdline, _In_ int ncmdshow)  
    {  
    	MessageBox(0, L"Hurra, man kann mich sehen!",  
    		L"Mein erstes Windows-Programm",  
    		MB_OK | MB_ICONEXCLAMATION);  
    	return 0;  
    }  
      
    

    Now I get two error messages by Visual Studio: LNK2019 and LNK1120. What ist this? I just wanna pop up a MessageBox with C++ under Visual Studio 2022. Whats the problem? Do you actually wanna help or just say something nobody can understand who doesnt is a professional programmer already?

    0 comments No comments

  4. Richard Wagner 1 Reputation point
    2022-08-06T10:32:52.817+00:00

    Ich habe das Problem selbst gelöst. Ich musste unter Projekteigenschaften gehen. Dort unter Linker dort unter System und dann bei Subsystem Windows einstellen. Offenbar hat er davor nur Konsolenanwendungen akzeptiert. Leute, Leute. Ihr arbeitet für Microsoft und habt keine Ahnung von euren eigenen Produkten. Lächerlich. Danke, dass ich mir selbst geholfen habe. Bin auch der Einzige, der das offenbar will.


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.