A Kubiesa A Kubiesa - 1 year ago 87
C++ Question

Linker errors in VS when using Windows API call in C++

I am trying to get to grips with using Windows API functions, but making them work properly is proving difficult.

In VS2015, my C++ code currently produces linker errors whenever I call

or anything similar:

#include "stdafx.h"
#include <Windows.h>
#include <iostream>

using namespace System;

int main()
Console::WriteLine(L"Hello World");
MessageBox(NULL, L"Stuff", L"Things", MB_OK | MB_ICONEXCLAMATION);
return 0;

The errors I get are an unresolved token and an unresolved external symbol, with
appearing in the messages.

One of the error messages:
Error LNK2028 unresolved token (0A0004E9) "extern "C" int stdcall MessageBoxW(struct HWND *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) referenced in function "extern "C" int cdecl MessageBox(struct HWND *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBox@@$$J0YAHPAUHWND__@@PB_W1I@Z)

Answer Source

You accidentally created a .net project, which implies you aren't going to be using the Windows API, at least not without some changes to your project. You need to create a Win32 project, not a .net project.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download