-2

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 MessageBox() 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 MessageBoxW 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)

A Kubiesa
  • 141
  • 7
  • Your code is not C++. This is C++/CX or MC++. – Dai Nov 14 '16 at 03:55
  • 1
    But if this *was* a C++ project, ensure your linker has a reference to `User32.lib`. – Dai Nov 14 '16 at 03:56
  • You're trying to use the Windows C API? Not the .Net framework in C# or VB.Net? I think you're kinda mixed up. Follow these steps: https://msdn.microsoft.com/en-us/library/bb384843.aspx - or consider using c#, which it looks like you maybe saw an example of that and tried to use it in your c++ source. – mock_blatt Nov 14 '16 at 04:06
  • 1
    What are the linking errors? Remove `Console::WriteLine(L"Hello World");` and replace with `std::cout << "Hello world";`. Console::Writeline is C# not C++. – Janarth K Nov 14 '16 at 05:08
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IInspectable Nov 14 '16 at 09:55
  • @mock_blatt it is entirely possible to write .net programs in C++ using the C++/CLI language – andlabs Nov 14 '16 at 14:48
  • @JanarthK it is entirely possible to write .net programs in C++ using the C++/CLI language, which Visual Studio has templates for – andlabs Nov 14 '16 at 14:49

2 Answers2

0

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.

andlabs
  • 11,290
  • 1
  • 31
  • 52
  • Thanks, I thought I had made a Win32 Console Application but I may have accidentally chosen CLR. I made a new Win32 Console App in C++ just now, taking out the using statement and including std::cout. Now it works fine! – A Kubiesa Nov 16 '16 at 22:32
0

It is perfectly fine to utilize WinAPI interfaces in the C++/CLI project, if your intention is to use only managed interfaces it is simpler to create a fully managed project (C#).

What you're missing here is a library inputs for linker (User32.lib as mentioned before in the comments).

See Calling native Win32 code from .NET (C++/CLI and C#) for the similar issue.

n0ne
  • 103
  • 4
  • 17