在C中调用C#.NET的静态函数(ICLRRuntimeHost_ExecuteInDefaultAppDomain) [英] Calling static function of C# .NET in C (ICLRRuntimeHost_ExecuteInDefaultAppDomain)
问题描述
我有用C#.NET Framework 2.0编写的此类 KernelHelper
。我想做的就是在C程序中调用其静态函数。
I have this class KernelHelper
which is written in C# .NET Framework 2.0. What I want to do is call its static functions in a C program.
namespace Kernel.Client {
public class KernelHelper {
public static int testc(string msg) {
// Removing the message box does not change anything
System.Windows.Forms.MessageBox.Show(msg);
return 0;
}
// ...
}
}
可以编译,到目前为止似乎没有任何问题。但是调用 ICLRRuntimeHost_ExecuteInDefaultAppDomain()
会返回 0x80131513
,这根据这我没有遵循正确的签名约定的事实。但这不是问题。
which compiles and does not seem to make any problems so far. But calling ICLRRuntimeHost_ExecuteInDefaultAppDomain()
returns 0x80131513
which is according to this the fact that I didn't follow the correct signature convention. But this can not be the problem.
#if defined(_WIN32)
# include <Windows.h>
# define COBJMACROS
# define CINTERFACE
# include <mscoree.h>
#endif
// ...
HRESULT status;
ICLRRuntimeHost *Host;
BOOL Started;
DWORD Result;
Host = NULL;
Started = FALSE;
status = CorBindToRuntimeEx(
NULL,
NULL,
0,
&CLSID_CLRRuntimeHost,
&IID_ICLRRuntimeHost,
(PVOID *)&Host
);
if (FAILED(status)) {
printf("failed 1\n");
}
status = ICLRRuntimeHost_Start(Host);
if (FAILED(status)) {
printf("failed 2\n");
}
Started = TRUE;
status = ICLRRuntimeHost_ExecuteInDefaultAppDomain(
Host,
L"C:\\svn\\Server\\Kernel\\interface\\bin\\Kernel.Client.dll",
L"Kernel.Client.KernelHelper",
L"testc",
L"My message",
&Result
);
if (FAILED(status)) {
printf("failed 3\n");
}
有人可以在这里帮助我吗?
Could anybody help me here?
编辑:
我也尝试了不带消息框的操作,并让该函数只返回 0
,但是它没有
推荐答案
哇,我没想到自己会这么快找到解决方案。如果您遇到相同的问题,请尝试此。
Wow, I did not expect to find the solution so fast and on my own. If you experiance the same problem try this. It seems not to work for everyone.
我要做的就是将平台目标
从我的项目属性中的任何CPU
到 x86
。
All I needed to do is to change the Platform Target
from Any CPU
to x86
inside my project properties.
编辑:如果有人可以向我展示其他解决方案,我仍然很高兴,因为我仍然无法确定这是否不是问题更改此设置。
If anyone can show me another solution I'd still be glad since I can not tell if this might not be a problem anyway changing this setting.
这篇关于在C中调用C#.NET的静态函数(ICLRRuntimeHost_ExecuteInDefaultAppDomain)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!