windows program

i would like to do a windows program. My compiler is DEV C++. I wrote a program to create and show a blank window. But it didn't work. Do I have to download another compiler lika Visual Studio C++?

error: undefined reference to 'WinMain@16'


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg,
        WPARAM wParam, LPARAM lParam);
        
int WINAPI wWinMain (HINSTANCE hInstance, HINSTANCE,
        PWSTR pCmdLine, int nCmdShow) {
              
        const wchar_t CLASS_NAME[]  = L"Sample Window Class";
        WNDCLASS wc = { };
        wc.lpfnWndProc = WindowProc;
        wc.hInstance   = hInstance;
        wc.lpszClassName = CLASS_NAME;
        
        RegisterClass(&wc);
        
        HWND hwnd = CreateWindowEx (0, CLASS_NAME,
             L"Learn to Program Windows", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
             CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
        
        if (hwnd == NULL)
           return 0;
           
        ShowWindow(hwnd, nCmdShow);    
        
        MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);

            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd, &ps);
        }
        return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}         

Last edited on
You have to add the user32.lib/libuser32.a and kernel32.lib/libkernel32.a and gdi32.lib/libgdi32.a (whichever you found in your pc) to your build and/or linker settings -not too sure how they use this on Dev C++, hope you can find it

(I don't use Dev C++ and you shouldn't, too. Get Code::Blocks)
what those things you're telling me to add? where do I add it?
what those things you're telling me to add?

those are the libraries needed for win32 codes to compile and build win32 programs, those with *.lib extensions come with windows sdk, while those with *.a come with MingW-GCC

where do I add it?

there should be a linker/build option menu in the IDE to tell it where to find those libraries
or, perhaps Dev C++ has a 'make windows/win32 program wizard' that you can use
or, you can compile it from command line with these commands (put Dev C++'s gcc into your path)
gcc source.cpp -o output.exe -lgdi32



EDIT: Sorry, I seemed to be talking out of my ass here, I didn't read/test your code before responding. After trying to compile the code above I got the same error message too, now I'm stumped. Perhaps somebody else could shed a light to this problem. Apologies :\

EDIT2: after some research, I found out that MingW/GCC does not work too well with wchar_t and there's no wWinMain, so, you can try to change the code to the 'normal' way (use WinMain, CHAR* or TCHAR and LPSTR to replace the 'w' versions) or use Visual C++/Express
Last edited on
I have to do an classroom apresentation about ms-windows programing introduction. I don't know nothing about tchar, lpstr... I'll try to compile using visual studio now and see what will happen.
Topic archived. No new replies allowed.