-3

I've started a new VS2013 Win32API project to play around with and added in some simple drawing menu items and cooresponding code:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HPEN hPen;
HPEN hOldPen;
HBRUSH hBrush;
HBRUSH hOldBrush;

switch (message)
{
case WM_COMMAND:
    wmId    = LOWORD(wParam);
    wmEvent = HIWORD(wParam);
    // Parse the menu selections:
    switch (wmId)
    {
    case IDM_ABOUT:
        DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
        break;
    case IDM_EXIT:
        DestroyWindow(hWnd);
        break;
    // Need to randomize x,y coordinates
    case ID_DRAW_CIRCLE:
        /* draw a blue-bordered magenta-crosshatched circle */
        hdc = GetDC(hWnd);                 /* get a DC for painting */
        hPen = CreatePen(PS_SOLID, 3, RGB(0, 0, 255));  /* blue pen */
        hBrush = CreateHatchBrush(HS_DIAGCROSS, RGB(255, 0, 255));
        hOldPen = (HPEN)SelectObject(hdc, hPen);      /* select into DC & */
        hOldBrush = (HBRUSH)SelectObject(hdc, hBrush); /* save old object */
        Ellipse(hdc, 100, 30, 180, 110);       /* draw circle */
        SelectObject(hdc, hOldBrush);          /* displace brush */
        DeleteObject(hBrush);                  /* delete brush */
        SelectObject(hdc, hOldPen);            /* same for pen */
        DeleteObject(hPen);
        ReleaseDC(hWnd, hdc);   /* release the DC to end painting */
        break; 
    case ID_DRAW_RECTANGLE:
        /* draw a red-bordered, cyan-filled rectangle */
        hdc = GetDC(hWnd);                /* get a DC for painting */
        hPen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));   /* red pen */
        hBrush = CreateSolidBrush(RGB(0, 255, 255));  /* cyan brush */
        hOldPen = (HPEN)SelectObject(hdc, hPen);      /* select into DC & */
        hOldBrush = (HBRUSH)SelectObject(hdc, hBrush); /* save old object */
        Rectangle(hdc, 15, 15, 80, 60);        /* draw rectangle */
        SelectObject(hdc, hOldBrush);          /* displace new brush */
        DeleteObject(hBrush);                  /* delete it from DC */
        SelectObject(hdc, hOldPen);            /* same for pen */
        DeleteObject(hPen);
        ReleaseDC(hWnd, hdc);                   /* get rid of DC */
        break; 
    case ID_DRAW_CLEARSCREEN:
        hdc = GetDC(hWnd); //NULL gets whole screen
        hBrush = CreateSolidBrush(RGB(255, 0, 0)); //create brush
        SelectObject(hdc, hBrush); //select brush into DC
        Rectangle(hdc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); //draw rectangle over whole screen
        break; 
    case ID_DRAW_QUIT:
        DestroyWindow(hWnd);
        break; 
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code here...
    EndPaint(hWnd, &ps);
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

Nothing crazy, but when I go to compile this code I get about 30 unresolved symbol errors:

Error   1   error LNK2019: unresolved external symbol __imp__LoadStringW@16 referenced in function _wWinMain@16 C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   2   error LNK2019: unresolved external symbol __imp__CreateHatchBrush@8 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   3   error LNK2019: unresolved external symbol __imp__CreatePen@12 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj    Win32Draw
Error   4   error LNK2019: unresolved external symbol __imp__CreateSolidBrush@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   5   error LNK2019: unresolved external symbol __imp__DeleteObject@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   6   error LNK2019: unresolved external symbol __imp__Ellipse@20 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   7   error LNK2019: unresolved external symbol __imp__Rectangle@20 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   8   error LNK2019: unresolved external symbol __imp__SelectObject@8 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   9   error LNK2019: unresolved external symbol __imp__GetMessageW@16 referenced in function _wWinMain@16 C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   10  error LNK2019: unresolved external symbol __imp__TranslateMessage@4 referenced in function _wWinMain@16 C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   11  error LNK2019: unresolved external symbol __imp__DispatchMessageW@4 referenced in function _wWinMain@16 C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   12  error LNK2019: unresolved external symbol __imp__DefWindowProcW@16 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)  C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   13  error LNK2019: unresolved external symbol __imp__PostQuitMessage@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)  C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   14  error LNK2019: unresolved external symbol __imp__RegisterClassExW@4 referenced in function "unsigned short __cdecl MyRegisterClass(struct HINSTANCE__ *)" (?MyRegisterClass@@YAGPAUHINSTANCE__@@@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   15  error LNK2019: unresolved external symbol __imp__CreateWindowExW@48 referenced in function "int __cdecl InitInstance(struct HINSTANCE__ *,int)" (?InitInstance@@YAHPAUHINSTANCE__@@H@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   16  error LNK2019: unresolved external symbol __imp__DestroyWindow@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)    C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   17  error LNK2019: unresolved external symbol __imp__ShowWindow@8 referenced in function "int __cdecl InitInstance(struct HINSTANCE__ *,int)" (?InitInstance@@YAHPAUHINSTANCE__@@H@Z)   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   18  error LNK2019: unresolved external symbol __imp__DialogBoxParamW@20 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   19  error LNK2019: unresolved external symbol __imp__EndDialog@8 referenced in function "int __stdcall About(struct HWND__ *,unsigned int,unsigned int,long)" (?About@@YGHPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   20  error LNK2019: unresolved external symbol __imp__LoadAcceleratorsW@8 referenced in function _wWinMain@16    C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   21  error LNK2019: unresolved external symbol __imp__TranslateAcceleratorW@12 referenced in function _wWinMain@16   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   22  error LNK2019: unresolved external symbol __imp__GetSystemMetrics@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   23  error LNK2019: unresolved external symbol __imp__UpdateWindow@4 referenced in function "int __cdecl InitInstance(struct HINSTANCE__ *,int)" (?InitInstance@@YAHPAUHINSTANCE__@@H@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   24  error LNK2019: unresolved external symbol __imp__GetDC@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)    C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   25  error LNK2019: unresolved external symbol __imp__ReleaseDC@8 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)    C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   26  error LNK2019: unresolved external symbol __imp__BeginPaint@8 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   27  error LNK2019: unresolved external symbol __imp__EndPaint@8 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   28  error LNK2019: unresolved external symbol __imp__LoadCursorW@8 referenced in function "unsigned short __cdecl MyRegisterClass(struct HINSTANCE__ *)" (?MyRegisterClass@@YAGPAUHINSTANCE__@@@Z)  C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   29  error LNK2019: unresolved external symbol __imp__LoadIconW@8 referenced in function "unsigned short __cdecl MyRegisterClass(struct HINSTANCE__ *)" (?MyRegisterClass@@YAGPAUHINSTANCE__@@@Z)    C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   30  error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\MSVCRTD.lib(crtexe.obj) Win32Draw
Error   31  error LNK1120: 30 unresolved externals  C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Debug\Win32Draw.exe   Win32Draw

What's going on here?!?!

Riptyde4
  • 5,134
  • 8
  • 30
  • 57
  • 1
    You need to include ``, and link with user32.lib, gdi32.lib and kernel32.lib. Maybe one or two others, but those cover the majority of what I saw glancing at your list. Oh, you also want a function named `WinMain`. Bottom line: you need an elementary book on Windows programming to tell you how to get started. – Jerry Coffin Oct 02 '14 at 21:49
  • 1
    check the documentation of each function. find which library needs to be linked. add it to the libraries. most likely the reason for this is that you've created a console program project. this is indicated by the missing "main", you have probably defined that Microsoft monstrosity `WinMain` instead (don't: define a standard `main`, and set entry point as `mainCRTStartup` when you change to GUI subsystem). – Cheers and hth. - Alf Oct 02 '14 at 21:50

1 Answers1

0

The simplest solution to your woes is probably to create a new Visual Studio project, where you take care to create a GUI project.

That will add the most common libraries.

A more general solution is to take charge of things, add each library that you need to the linker settings. Microsoft's documentation, called MSDN Library and available both on-line and as downloadable local help, lists the required header and library for each function. You simply need to read the documentation when, in the future, you get this kind of error.


Regarding

unresolved external symbol _main referenced

… note that there is no need or reason to use Microsoft's non-standard startup function WinMain (or tWinMain). Use a standard C++ main function. For the GUI subsystem, with Microsoft's tools you then have to specify entry point mainCRTStartup, because Microsoft's tools are non-standard by default (vendor lock-in and all that).

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • You've got it all backwards. When using the CRT that ships with Visual Studio, you cannot change the name or signature of the 'startup' function. It's hardcoded into `mainCRTStartup`. If you do not want to use the CRT, you are free to name your entry point **anything** you like, including `main`. *Microsoft's tools* do not prevent this. Plus, when you `#include ` you no longer care about portability. Details: [WinMain is just the conventional name for the Win32 process entry point](http://blogs.msdn.com/b/oldnewthing/archive/2011/05/25/10168020.aspx). – IInspectable Oct 04 '14 at 16:29
  • @IInspectable's statements here are not technically meaningful. For example, "It's hardcoded into `mainCRTStartup`" is trivially true but also completely meaningless; there are four such CRT entry point functions, each calling a different hardwired startup function (e.g. `wmainCRTStartup` calls `wmain`). It appears that he's arguing against something but there is no referent; it appears this his link is in support of some argument but it isn't; and so it's all empty rhetoric, a little bit of deception. – Cheers and hth. - Alf Oct 04 '14 at 20:38
  • When using the CRT that ships with Visual Studio, you should not change a GUI application's entry point to `mainCRTStartup`, for the sole reason of being able to use `main`. Doing so will produce an application that is not well behaved. It will not respect the initial window show mode, it fails to properly initialize error reporting, and it fails to set the correct application type. The application type is used internally by the CRT; a mismatch has unknown implications. – IInspectable Oct 06 '14 at 10:11