Link error for CoInitializeEx, unresolved token - c++

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!

Related

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 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.

Implementing extern WINAPI call in MFC C++ App

I am working with Windows Form App C++.
I have to use some header file that has
HRESULT extern WINAPI StartUp ( DWORD dwVRequired, LPVERSION lpVersion);
I have to execute this method within Form1.h
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
APPVERSION version;
HRESULT result = APPStartUp(APP_VERSIONS, &version);
}
But I am getting the compile error like this
Error 1 error LNK2028: unresolved token (0A000025) "extern "C" long
__stdcall APPStartUp(unsigned long,struct _version *)" (?WFSStartUp##$$J18YGJKPAU_wfsversion###Z) referenced in function
"private: void __clrcall MyAppPresenter::Form1::Form1_Load(class
System::Object ^,class System::EventArgs ^)"
(?Form1_Load#Form1#MyAppPresenter##$$FA$AAMXP$AAVObject#System##P$AAVEventArgs#4##Z) C:\Projects\MyAppPresenter\MyAppPresenter\MyAppPresenter.obj MyAppPresenter
I assume it has something to do with
MSVS2010 C++ Console Code Ported to MSVS2010 C++ GUI is Failing. Why?
But how it can be done?
Any clue how it could be fixed?
I checked the console C++ project and found that *.vsxproj has
<ItemGroup>
<Library Include="C:\Program Files\Common Files\GCFApp\lib\msmanager.lib" />
</ItemGroup>
So I just added this code to the Windows Form C++ project file manually and now it works.
Anyway I have no clue how to add it using GUI of VS 2010.

Using DirectXTex Library

I am trying to update my DirectX Project to the new Windows 8.1 sdk (2013 or something) and get rid of DirectX SDK june 2010. I have come far, but I am stuck at a Linker error LNK2001
Error 3 error LNK2001: unresolved external symbol "long __cdecl DirectX::CreateDDSTextureFromFile(struct ID3D11Device *,wchar_t const *,struct ID3D11Resource * *,struct ID3D11ShaderResourceView * *,unsigned int,enum DirectX::DDS_ALPHA_MODE *)" (?CreateDDSTextureFromFile#DirectX##YAJPAUID3D11Device##PB_WPAPAUID3D11Resource##PAPAUID3D11ShaderResourceView##IPAW4DDS_ALPHA_MODE#1##Z) C:\Users\DimmerFan\documents\visual studio 2013\Projects\MikaelD3D\MikaelD3D\TextureClass.obj MikaelD3D
I guess that doesnt say much. This is the code with the error:
HRESULT result;
result = DirectX::CreateDDSTextureFromFile(device, filename, nullptr, &m_texture, 0, nullptr);
if (FAILED(result))
{
return false;
}
return true;
Where I get this function DirectX::CreateDDSTextureFromFile from the DirectXTex SDK.
However I somehow fail to include this library to my Project. I dont know what could have gone wrong.
Intellisence pops up and Everything looks good. I have included the D:\DirectXTex\DDSTextureLoader To my Include Directorys and I am including #include <DDSTextureLoader.h> Thanks for any help about this error
//Mikael Törnqvist
You can try to add DDSTextureLoader.h and DDSTextureLoader.cpp to your project, DirectXTex was provided as source code, if you want to use it, you should add it to your solution/project.
You can also build the DirectXTex project yourself, and use the headers and libs.
DirectXTex's package includes the 'standalone' versions of DDSTextureLoader, WICTextureLoader, and ScreenGrab. You drop the .cpp and .h files into your project as they are not included in the DirectXTex.lib.
DirectX Tool Kit has the 'integrated' versions of those same modules, so they are in the DirectXTK.lib.
Why are there two versions of the same files you ask? See my blog post on the topic.

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.

Unresolved external symbols __RTC_* in Windows programming tutorial

Using Visual Studio Express 2010 I made a Windows project with the options Windows Application and Empty Project. I then tried the following code snippet from the MSDN Windows tutorials:
#include <windows.h>
#include <shobjidl.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
IFileOpenDialog *pFileOpen;
// Create the FileOpenDialog object.
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr))
{
// Show the Open dialog box.
hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.
if (SUCCEEDED(hr))
{
IShellItem *pItem;
hr = pFileOpen->GetResult(&pItem);
if (SUCCEEDED(hr))
{
PWSTR pszFilePath;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.
if (SUCCEEDED(hr))
{
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);
CoTaskMemFree(pszFilePath);
}
pItem->Release();
}
}
pFileOpen->Release();
}
CoUninitialize();
}
return 0;
}
I got the following errors:
1>------ Rebuild All started: Project: Test05, Configuration: Debug Win32 ------
1> Test05.cpp
1>Test05.obj : error LNK2019: unresolved external symbol #_RTC_CheckStackVars#8
referenced in function _wWinMain#16
1>Test05.obj : error LNK2019: unresolved external symbol __RTC_CheckEsp referenced in
function _wWinMain#16
1>Test05.obj : error LNK2001: unresolved external symbol __RTC_Shutdown
1>Test05.obj : error LNK2001: unresolved external symbol __RTC_InitBase
1>LINK : error LNK2001: unresolved external symbol _wWinMainCRTStartup
What's going on here? Best I can tell something to do with wWinMain, but it is copied directly from the site.
Compilers seem to be far more trouble for me than learning programming. I decided on Visual C++ after trying a few others (codeblocks mostly), but since Visual C++ seems to have the most support (or at least the majority of users) I figured it was better than never getting anywhere since they all are so unintuitive to a beginner.
The _RTC_xxx symbols are added when the 'Basic Runtime Checks' are used; to disable them, you can go on the project properties, and set the Configuration Properties>C/C++>All Options>Basic Runtime Checks option to 'Default'. Though, as mentioned in the other answers, it looks like you have a mismatch in the C-Runtime libraries.
It looks like you're linking againt a different verison of the runtime libraries than what you're compiling against.
Please make sure you have only one version of Visual Studio installed and in path.
If you have more than one version, try to temporarily rename root directories of other Visual Studio installations to see if this will cause any effect.
I would download the complete code sample from the link here rather than copying and pasting that code snippet you linked to. There may be important compiler/linker settings in the project files that are not being shown there. The sample is a VS 2008 solution, but I was able to upgrade it to a 2010 solution and build it. However, it gave me 'fatal linker error: could not find 'kernel32.lib'' when I tried to build it in VS 2008.