问题描述
我有一个托管 C# 测试 DLL,我想从我的非托管 C++ 代码中调用它的函数。
问题是我找不到显示如何从我的 c++ 代码中调用任意函数的示例,我只能让 ICLRRuntimeHost::ExecuteInDefaultAppDomain
工作,但该函数确实使用了预定义的方法签名。>
问题
我可以从我的 C# DLL 中执行 ShowMsg
函数,但我不知道如何使用除 int(int)
之外的其他签名调用方法。有人可以帮我吗?
C++ 代码
// interface deFinitions of functions from c# dll
typedef int (*TestCallbackPtr)(std::wstring info,int value1,double value2);
typedef int (*TestFunctionPtr)(std::wstring string1,int int1,double double1,TestCallbackPtr callback);
// path of my c# dll
auto pathDll = L"..."
void Runtest()
{
ICLRMetaHost* clrMetaHost = NULL;
ICLRRuntimeInfo* clrRuntimeInfo = NULL;
ICLRRuntimeHost* clrRuntimeHost = NULL;
// loading the data above is excluded for simplicity!
// ...
// -------------
// Test 1: run ShowMsg => WORKING
// -------------
DWORD pReturnValue;
HRESULT result = clrRuntimeHost->ExecuteInDefaultAppDomain(pathDll,L"Test.Managed",L"ShowMsg",L"Hello from C++",&pReturnValue);
// -------------
// Test 2: run TestFunction
// -------------
TestCallbackPtr callback = [](std::wstring info,double value2) {
return 21;
};
TestFunctionPtr function = NULL;
// how to continue? how can I execute the function here and assign my local function pointer so that following will work:
// (*function)(L"Test",45,1.23,callback);
}
C# 代码
namespace Test
{
public class Managed
{
public static int ShowMsg(string msg)
{
MessageBox.Show(msg);
return 0;
}
public delegate int TestCallback([MarshalAs(UnmanagedType.LPWStr)] string info,double value2);
public static int TestFunction(
[MarshalAs(UnmanagedType.LPWStr)] string string1,TestCallback callback)
{
string info = $"string1 = {string1} | int1 = {int1} | double1 = {double1}";
int returnValue = callback(info,5,10.34);
return returnValue;
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)