Linker errors in VS when using Windows API call in C++ - 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 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)

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.

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.

Related

Changing Runtime Library broke std::string. Why?

EDIT:
I've fixed the problem by setting my Runtime Library to inherit from project default.
I was using Multi-treaded DLL(/MT) because a SDL-2 tutorial I used did that.
Can someone explain why this broke the string's of c++?
Whenever I try to use a string my compiler gives me the following errors:
LNK2019 unresolved external symbol __imp___invalid_parameter referenced in function "void * __cdecl std::_Allocate(unsigned int,unsigned int,bool)"
LNK2019 unresolved external symbol __imp___CrtDbgReportW referenced in function "void * __cdecl std::_Allocate(unsigned int,unsigned int,bool)"
I've stripped all my code to the bare minimum and it is as followed:
#include <string>
int main(int argc, char *argv[]) {
std::string resPath;
return 0;
}
I have no idea where to look. Does anyone have an idea?
I'm using MSVS Community 2015.
I just had the same problem with VS2017. It turned out that I had my runtime library settings wrong. I had accidently set it to Multi-threaded DLL (/MD) for the debug configuration. It should have been Multi-threaded Debug DLL (/MDd) or Multi-threaded Debug (/MTd).

Link error for CoInitializeEx, unresolved token

I created a CLR Class Library where I am wanting to do some WASAPI Core Audio processing. Finally got around to adding the COM stuff, and I've stripped it all down to simply this:
bool AudioLibrary::AudioClass::initializeAudioDevices()
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr))
{
// Big problem
return hr;
}
}
This is extremely basic, and compiles just fine (my namespace is obviously AudioLibrary and the class is AudioClass). However, during link I get this:
Error 9 error LNK2028: unresolved token (0A000029) "extern "C" long __stdcall CoInitializeEx(void *,unsigned long)" (?CoInitializeEx##$$J18YGJPAXK#Z) referenced in function "private: bool __clrcall AudioLibrary::AudioClass::initializeAudioDevices(void)" (?initializeAudioDevices#AudioClass#AudioLibrary##$$FA$AAM_NXZ) C:\Users\Administrator\Documents\Visual Studio Projects\WindowsFormsApplication1\AudioLibrary\AudioLibrary.obj AudioLibrary
I've got Windows.h and objbase.h included, but I'm obviously missing some type of link reference. I've tried both 'inherit from parent' and no inherit in the link options for the project.
Been doing too much IOS/Android/OSX programming lately so I'm apparently pretty stale on Visual Studio stuff. What am I missing to include CoInitializeEx? I'm using Visual Studio 2013 Ultimate. Thanks!

Unresolved External Symbol LNK2019 with WinRT component for COM-based lib

I'm using a 3rd party COM-based library called Engine in a native WinRT component which should later act as a wrapper for the 3rd party lib.
An Engine.lib and a Engine.h file for the 3rd party lib is setup in my project.
I'm getting the LNK2019 for my following cpp file:
#include "pch.h"
#include "Engine.h"
void Component::Init()
{
ComPtr<IEngine> spEngine;
Settings settings;
CreateEngine(&settings, &spEngine);
}
The code compiles fine and the Engine.lib is setup in the project settings of VS2012. Also does DUMPBIN /EXPORTS for the Engine.lib show that CreateEngine is exposed. I can also use other types defined in the Engine.h, but as soon as CreateEngine is called, a linker error is raised:
Error 1 error LNK2019: unresolved external symbol CreateEngine#8 referenced in function "public: virtual void __cdecl
Engine.h defines CreateEngine like this:
STDAPI CreateEngine(
_In_ Settings * pSettings,
_Outptr_ IEngine **ppEngine );
Where the STDAPI is the usual macro:
#define STDAPI extern "C" HRESULT __stdcall
Any ideas?
Figured it out with the help of Inspired: I was using the lib built for ARM with an x86 build configuration. After changing that it was linking fine.

C++/CLI LNK2028 when trying to use static lib of another library

I am having a LNK2028 error when I try to build my C++/CLI dll. I am using a static lib called pano13 in my program, and I am using one method of it. Everything in my program is fine except the one method call I make to the library, where I get these exact two exceptions.
Error 21 error LNK2028: unresolved token (0A00013B) "int __cdecl panoCreatePanorama(struct fullPath * const,int,struct fullPath *,struct fullPath *)" (?panoCreatePanorama##$$FYAHQAUfullPath##HPAU1#1#Z) referenced in function "public: int __clrcall Surgeon::Stitcher::StitchImage(class System::Collections::Generic::List<class System::String ^> ^,class System::String ^)" (?StitchImage#Stitcher#Surgeon##$$FQ$AAMHP$AAV?$List#P$AAVString#System###Generic#Collections#System##P$AAVString#6##Z) C:\Users\ndean_000\Documents\Visual Studio 2012\Projects\C#\CameraTest\Surgeon\Surgeon.obj Surgeon
Error 22 error LNK2019: unresolved external symbol "int __cdecl panoCreatePanorama(struct fullPath * const,int,struct fullPath *,struct fullPath *)" (?panoCreatePanorama##$$FYAHQAUfullPath##HPAU1#1#Z) referenced in function "public: int __clrcall Surgeon::Stitcher::StitchImage(class System::Collections::Generic::List<class System::String ^> ^,class System::String ^)" (?StitchImage#Stitcher#Surgeon##$$FQ$AAMHP$AAV?$List#P$AAVString#System###Generic#Collections#System##P$AAVString#6##Z) C:\Users\ndean_000\Documents\Visual Studio 2012\Projects\C#\CameraTest\Surgeon\Surgeon.obj Surgeon
I am including the lib file in the project settings, and I even added the #pragma comment statement for including the library, however I am getting this error. I understand that it has to do with the mixing of native and managed C++, however I am not compiling the program with clr/pure, it is being compiled with the default clr compilation of /clr. Anyone have any ideas how to fix it?
By the way, I solved this a WHILE ago, but I should probably say what the issue was. The panotools library is a C library, not a C++ library. I didn't know that C libraries needed the extern C directive to be used in C++. So all I had to do to fix my problem was
extern "C"
{
#include <panorama.h>
}
Where panorama.h is the include file for the panotools C library. I've always wondered what extern C was for and now I finally understand its purpose.

Link windows.h (WNDCLASSEX) code in VS2010 console application

I have a VS2010 Win32 console application. Within this console application I try to attach a window as child to another Win32 app. But I do not get the application linked (it compiles, just the linker issue AFAIK):
Error 1 error LNK2019: unresolved external symbol "long __stdcall viewproc(struct HWND__ *,unsigned int,unsigned int,long)" (?viewproc##YGJPAUHWND__##IIJ#Z) referenced in function "void __cdecl createFSXWindow(void)" (?createFSXWindow##YAXXZ) H:\Projects\VisualStudioNet2010\FSXTests\Menu Items\Menu Items\MenuItems.obj Menu Items
I have added all libs from a Win32 Windows project under Linker/include:
kernel32.lib;user32.lib;gdi32.lib;winspool.lib; comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib; odbccp32.lib;%(AdditionalDependencies)
Remark: Just to clarify, I do NOT(!) try to attach the window to the console window, but another Win32 application, so it is not about "Make a win32 console app display a window"
The issue comes in once I add the window code such as:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
....
// window
WNDCLASSEX wc;
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = viewproc;
When I check the command line of the linker the only difference between my console app and an Win32 app is SUBSYSTEM:CONSOLE vs. SUBSYSTEM:WINDOWS
Any hints?
The linker error just tells you that you forgot to write the viewproc() function.
You declared it, the compiler is happy, but didn't implement it, the linker is unhappy. Or you wrote it but mistyped the definition. It is not a function that's implemented in one of the standard libraries, you have to provide it. The window procedure is what you use to give a window custom behavior.