-3

I'm getting this errors:

LNK1120: 1 unresolved externals on line 1

error LNK2019: unresolved external symbol _winproc@20 referenced in function _WinMain@16 C:\Users\giorgi\Documents\Visual Studio 2013\Projects\Hello\Hello\Source.obj Hello

I'm new to WinApi please help.

#include <windows.h>


LRESULT CALLBACK winproc(WNDPROC lpPrevWndFunc, HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR LpCmdLine, int nCmdShow)
{

WNDCLASSEX class;
ZeroMemory(&class, sizeof(WNDCLASSEX));

class.cbSize = sizeof(WNDCLASSEX);
class.style = CS_HREDRAW | CS_VREDRAW;
class.lpfnWndProc = (WNDPROC)winproc;
class.cbClsExtra = 0;
class.cbWndExtra = 0;
class.hInstance = hInstance;
class.hIcon = NULL;
class.hCursor = LoadCursor(NULL, IDC_ARROW);
class.hbrBackground = (HBRUSH)COLOR_WINDOW;
class.lpszClassName = "window class";
class.lpszMenuName = NULL;
class.hIconSm = NULL;

RegisterClassEx(&class);



HWND hwnd = CreateWindowEx
    (
    WS_EX_ACCEPTFILES,
    "window class",
    "window",
    WS_OVERLAPPED,
    200,
    200,
    800,
    600,
    NULL,
    NULL,
    hInstance,
    NULL
    );


ShowWindow(hwnd, nCmdShow);


MSG msg;
ZeroMemory(&msg, sizeof(MSG));


while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return 0;
}


LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_DESTROY:
{
    PostQuitMessage(0);
    return 0;
}
    break;
}

return DefWindowProc(hwnd, msg, wp, lp);

}
  • 4
    C and C++ are case sensitive, so `winproc` (which you've declared and used) is different from `WinProc` (which is what you've defined). You also have a different signature in the definition than the declaration, so even with the case fixed they still won't match. – Jerry Coffin Jan 13 '15 at 18:24
  • 1
    On top of the case sensitivity you have 2 different signatures for WinProc and winproc. – drescherjm Jan 13 '15 at 18:24
  • [What is an undefined reference/unresolved external symbol error and how do I fix it?: Declared but did not define a variable or function](http://stackoverflow.com/a/12574403/902497). "Be careful that the function you implement exactly matches the one you declared." In your case, they do not match exactly. – Raymond Chen Jan 13 '15 at 18:28
  • 1
    I highly suggest getting a *working*, simple API program from an external source (such as a book), instead of trying to write this yourself first. – PaulMcKenzie Jan 13 '15 at 18:31
  • I highly suggest to pick *either* C or C++, not "both" or "it doesn't make a difference for my question". – Jongware Jan 13 '15 at 18:32

1 Answers1

1

As others have said, C is case-sensitive, so winproc and WinProc would be two different functions. You also need to insure that the signature of your windows procedure matches what Windows expect, so make the following changes:

  1. change LRESULT CALLBACK winproc(WNDPROC lpPrevWndFunc, HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); to LRESULT CALLBACK winProc(HWND, UINT, WPARAM, LPARAM);

  2. change class.lpfnWndProc = (WNDPROC)winproc; to class.lpfnWndProc = (WNDPROC)winProc;

  3. change LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) to LRESULT CALLBACK winProc(HWND hwnd, UINT mgs, WPARAM wp, LPARAM lp)

Finally, its been awhile since I programmed at the win32-API level, but I believe that you windows procedure should look like:

    LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
    {
        switch (msg)
        {
            case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            }
            break;

            default:
                return DefWindowProc(hwnd, msg, wp, lp);
        }
    }

In other words, you only want to return the default window proceedure (DefWindowProc) if you do not handle the message yourself.

thurizas
  • 2,473
  • 1
  • 14
  • 15