C# 与C++通过ICLRRunTime::ExcUnEntRebug的域互操作 我的应用主要是在Windows平台上用C++开发的。现在,我想将一些由C实现的奇特功能集成到我的应用程序中 < >我从C++调用C代码的方式是,它要求C的方法是静态int pWZM
与C++通过ICLRRunTime::ExcUnEntRebug的域互操作C# 与C++通过ICLRRunTime::ExcUnEntRebug的域互操作 我的应用主要是在Windows平台上用C++开发的。现在,我想将一些由C实现的奇特功能集成到我的应用程序中 < >我从C++调用C代码的方式是,它要求C的方法是静态int pWZM,c#,c++,.net,interop,C#,C++,.net,Interop,与C++通过ICLRRunTime::ExcUnEntRebug的域互操作 我的应用主要是在Windows平台上用C++开发的。现在,我想将一些由C实现的奇特功能集成到我的应用程序中 < >我从C++调用C代码的方式是,它要求C的方法是静态int pWZMultDead字符串pWZTalk。 问题是,如何将实例C++类的指针作为调用调用方法传递到PWZToT中,并直接从C代码中消耗本机C++类? 例如,我有以下C++代码和C代码, // C++ code struct IUserContext
我的应用主要是在Windows平台上用C++开发的。现在,我想将一些由C实现的奇特功能集成到我的应用程序中
< >我从C++调用C代码的方式是,它要求C的方法是静态int pWZMultDead字符串pWZTalk。问题是,如何将实例C++类的指针作为调用调用方法传递到PWZToT中,并直接从C代码中消耗本机C++类?
例如,我有以下C++代码和C代码,
// C++ code
struct IUserContext
{
virtual const char* GetCompany() const = 0;
virtual const char* GetUser() const = 0;
};
class UserContextImpl : public IUserContext
{
public:
virtual const char* GetCompany() const { return m_company.c_str(); }
virtual const char* GetUser() const { return m_user.c_str(); }
private:
std::string m_company; // will be assigned in constructor
std::string m_user;
};
int main()
{
ICLRRuntimeHost *pClrHost = CreateHost();
UserContextImpl user;
IUserContext& cx = user;
DWORD dwRet;
wstringstream param;
param << (void*)&cx;
pClrHost->ExecuteInDefaultAppDomain(
L"MySharpAssembly.dll",
L"MyCompany.Namespace.MyClass",
L"Start",
param.str().c_str(),
&dwRet);
//...
}
// C# code
namespace MyCompany.Namespace
{
interface IUserContext
{
string GetCompany();
string GetUser();
}
public class MyClass
{
public static int Start(string pwzArgument)
{
IntPtr pointer = (IntPtr)int.Parse(pwzArgument, NumberStyles.AllowHexSpecifier);
// How to implement this "MagicMarshal.GetInterface" method?
IUserContext cx = MagicMarshal.GetInterface<IUserContext>(pointer);
MessageBox.Show(cx.GetUser());
}
}
}