C++/CLI 包装器实现错误
C++/CLI Wrapper Implementation Errors
我一直在尝试在以下 link 中使用 C++/CLI 包装器方案。
注意:在上周我进行的所有搜索中,以下项目是被引用最多的提供可行解决方案的项目。
http://pragmateek.com/using-c-from-native-c-with-the-help-of-ccli-v2/
下面的项目是我尝试 link 接受 Win32 Dll 的第 3 方应用程序。我们的目标是 link 将 Win32 Dll 转换为托管 C# DLL,其中的函数应该更易于编写和维护。
但是,我将 运行 保留为以下错误的变体。
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
我的 C# 库: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;
}
请注意错误发生在:'GetRevisionFunc'
编辑:好的 - 我做了以下更改:
//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();
}
现在我要解决以下错误:
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
好吧,您必须整理类型,这在 C++/CLI 中没有为您完成。你有一个 System::String^
但你想 return 它作为一个 std::string
,所以你必须编组它:
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);
}
有关更多详细信息,请查看 the docs。
您的 QF_UtilitiesWrapper
也持有 _private
作为 static 成员,您在构造函数/析构函数中设置和删除它。这将泄漏内存并可能导致意外行为。
我一直在尝试在以下 link 中使用 C++/CLI 包装器方案。
注意:在上周我进行的所有搜索中,以下项目是被引用最多的提供可行解决方案的项目。
http://pragmateek.com/using-c-from-native-c-with-the-help-of-ccli-v2/
下面的项目是我尝试 link 接受 Win32 Dll 的第 3 方应用程序。我们的目标是 link 将 Win32 Dll 转换为托管 C# DLL,其中的函数应该更易于编写和维护。
但是,我将 运行 保留为以下错误的变体。
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
我的 C# 库: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;
}
请注意错误发生在:'GetRevisionFunc'
编辑:好的 - 我做了以下更改:
//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();
}
现在我要解决以下错误:
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
好吧,您必须整理类型,这在 C++/CLI 中没有为您完成。你有一个 System::String^
但你想 return 它作为一个 std::string
,所以你必须编组它:
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);
}
有关更多详细信息,请查看 the docs。
您的 QF_UtilitiesWrapper
也持有 _private
作为 static 成员,您在构造函数/析构函数中设置和删除它。这将泄漏内存并可能导致意外行为。