Problem mit einfacher Windows Application



  • Hallo, hab mir ein einfaches Fenster gecoded, dabei tritt der Fehler "Undefined Reference to `WinMain@16'" auf. Woran kann das Liegen?

    Compiler: Code::Blocks

    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    const char szAppName[] = "Ein eigenes Fenster";
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpszCmdLine, UINT iCmdShow)
    {
        HWND hWnd;
        MSG msg;
        WNDCLASS wc;
    
        wc.style = CS_VREDRAW | CS_HREDRAW;
        wc.lpfnWndProc = WndProc;
    
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
    
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hInstance = hInstance;
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    
        wc.lpszClassName = szAppName;
        wc.lpszMenuName = NULL;
    
        RegisterClass(&wc);
    
        hWnd = CreateWindow(szAppName, "Titelleiste", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    
        ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);
    
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch(message)
        {
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
            break;
        }
    
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    


  • Falsches Forum bitte Verschieben.



  • Für ein Windows-Programm musst Du ein Projekt (In Code::Blocks z.B. "Win32 GUI Project") erstellen. Dafür liefert Dir Code::Blocks dann gleich den ganzen Sourcecode. 😉



  • Hab ich ja gemacht, (dieser Code ist von Dev-Cpp) Win-Gui Project.

    -> Sry, habs im Falschen Forum geposted :D, bitte ins Win-Api Forum verschieben

    Das Problem löst sich komischerweise, wenn ich eine Main() Funktion ohne Inhalt hinzufüge, allerdings bekomme ich dann kein Fenster zu sehen 😞



  • ja, so gesehen richtig...
    aber fenster basteln mit windows.h ist WinAPI und dafür gibts hier auch ein Sub-Forum



  • Arr0ws schrieb:

    Hab ich ja gemacht, (dieser Code ist von Dev-Cpp) Win-Gui Project.

    -> Sry, habs im Falschen Forum geposted :D, bitte ins Win-Api Forum verschieben

    Das Problem löst sich komischerweise, wenn ich eine Main() Funktion ohne Inhalt hinzufüge, allerdings bekomme ich dann kein Fenster zu sehen 😞

    Scheint so, als ob die lib nicht richtig gelinkt wurde. Überprüf das doch mal, oder erzeug mal ein neues Projekt.



  • Fehler gefunden - Durch überprüfung mit dem Code den Code::Blocks selber erstellt.

    Und er ist (wie immer) so einfach:

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpszCmdLine, UINT iCmdShow)

    Es gehört int iCmdShow (klarerweise)...
    Komischer Fehler, den der Compiler da ausgegeben hat ^^

    Hier nochmal der Code der Einwandfrei läuft:

    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    const char szAppName[] = "Ein eigenes Fenster";
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpszCmdLine, int iCmdShow)
    {
        HWND hWnd;
        MSG msg;
        WNDCLASS wc;
    
        wc.style = CS_VREDRAW | CS_HREDRAW;
        wc.lpfnWndProc = WndProc;
    
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
    
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hInstance = hInstance;
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    
        wc.lpszClassName = szAppName;
        wc.lpszMenuName = NULL;
    
        RegisterClass(&wc);
    
        hWnd = CreateWindow(szAppName, "Titelleiste", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    
        ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);
    
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch(message)
        {
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
            break;
        }
    
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    


  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.


Anmelden zum Antworten