I've been attempting to utilize the C++/CLI wrapper scheme in the following link.
Note: in all the searching I've done over the last week, the below project is the one most cited as providing a workable solution.
http://pragmateek.com/using-c-from-native-c-with-the-help-of-ccli-v2/
The below project is my attempt to link to a 3rd party app that accepts a Win32 Dll. Hy goal is to link the Win32 Dll to a managed C# DLL where the functions should be easier to write & maintain.
However, I keep running into variations of the below errors.
Error C2664 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(std::initializer_list<_Elem>,const std::allocator<char> &)' : cannot convert argument 1 from 'System::String ^' to 'const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &' Main_interface
Error (active) E0415 no suitable constructor exists to convert from "System::String ^" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" Main_interface
My C# Library: MainUtilities.dll
//Main_Utility.cs
public class MainUtilities
{
public string GetRevision(string CurrentRev)
{
return CurrentRev + "_rev";
}
}
Main_Interface.h
#pragma once
#define DLL_EXP extern "C" __declspec(dllexport)
DLL_EXP void GetRevision(char* data_in, char *data_out);
class QFUtilitiesWrapperPrivate;
#ifdef BUILDINGQFUTILITIES
#define DLL_EXP_WRAP __declspec(dllexport)
#else
#define DLL_EXP_WRAP __declspec(dllimport)
#endif
#include <string>
class DLL_EXP_WRAP QF_UtilitiesWrapper
{
private: static QFUtilitiesWrapperPrivate* _private;
public: QF_UtilitiesWrapper();
public: ~QF_UtilitiesWrapper();
public: static std::string GetRevisionFunc(const char* rev);
};
Main_Interface.cpp
#include "stdafx.h"
#include "Main_Interface.h"
#include <msclr\auto_gcroot.h>
#using "MainUtilities.dll"
using namespace System::Runtime::InteropServices; // Marshal
class QFUtilitiesWrapperPrivate
{
public: msclr::auto_gcroot<MainUtilities^> main_Utilities;
};
QF_UtilitiesWrapper::QF_UtilitiesWrapper()
{
_private = new QFUtilitiesWrapperPrivate();
_private->main_Utilities = gcnew MainUtilities();
}
DLL_EXP void GetRevision(char* data_in, char *data_out)
{
std::string s_b = QF_UtilitiesWrapper::GetRevisionFunc(data_in);
strcpy_s(data_out, 100, s_b.c_str());
}
std::string QF_UtilitiesWrapper::GetRevisionFunc(const char* data_in)
{
return _private->main_Utilities->GetRevision(gcnew System::String(data_in));
} //** ERROR ^^^^^^ **
QF_UtilitiesWrapper::~QF_UtilitiesWrapper()
{
delete _private;
}
Please note the the errors occur at: 'GetRevisionFunc'
EDIT: Ok - I've made the following changes:
//Main_Interface.h
private: QFUtilitiesWrapperPrivate* _private; // no longer static
public: const char* GetRevisionFunc(const char* rev); //function now: const char*
//Main_Interface.cpp
DLL_EXP void GetRevision(char* data_in, char *data_out)
{
data_out = QF_UtilitiesWrapper::GetRevisionFunc(data_in);
// ** ERROR ^^^^^^^^^^ **
}
const char* QF_UtilitiesWrapper::GetRevisionFunc( char* rev) // now: const char*
{
System::String^ managedCapi = _private->main_Utilities->GetRevision(gcnew System::String(rev));
return (const char*)Marshal::StringToHGlobalAnsi(managedCapi).ToPointer();
}
Now I'm tying to resolve the following errors:
Error C2352 'QF_UtilitiesWrapper::GetRevisionFunc' : illegal call of non-static member function
Error (active) E0245 a nonstatic member reference must be relative to a specific object
Well you have to marshal the types, this isn't done for you in C++/CLI. You have a System::String^
but you want to return it as an std::string
, so you have to marshal it:
std::string QF_UtilitiesWrapper::GetRevisionFunc(const char* data_in)
{
auto str = _private->main_Utilities->GetRevision(gcnew System::String(data_in));
return msclr::interop::marshal_as<std::string>(str);
}
For more details, checkout the docs.
Also your QF_UtilitiesWrapper
holds _private
as a static member and you set and delete this in your constructor / destructor. This will leak memory and probably cause unexpected behavior.