[英]DLL function (in C# .NET) called from pure C program causes error
我有一个C程序,需要使用C#.NET编写的库的某些功能。 在我搜索了一种方法之后,并没有花很长时间,直到遇到另一个问题。
只要函数没有返回compley类型,从该库中调用方法/函数似乎就不是问题( 注意:到目前为止,这只是我的猜测,但这是我到目前为止观察到的行为)。
我想向您展示代码的样子( 注意:这不是实际的代码,但其中包含一些可能很有趣的部分):
// C Code calling C# function from dll
#if defined(_WIN32)
# include <Windows.h>
# define COBJMACROS
# define CINTERFACE // For C-Interface
# include <mscoree.h>
#endif
void callCSharpFunction ()
{
HRESULT status;
BOOL Started;
DWORD result;
Started = FALSE;
// preparation code ..
status = ICLRRuntimeHost_ExecuteInDefaultAppDomain(
Host,
L"C:\\path\\mydll.dll",
L"mynamespace.myclass",
L"myfunction",
L"paramAsString",
&result
);
// ...
}
C#代码, 请注意 ,我使用ICLRRuntimeHost_ExecuteInDefaultAppDomain
调用的所有函数必须具有相同的签名int fncname(string param);
。
/// C#
/// @param parameter is ignored
public static int myfunction(string strParam)
{
try {
IComplexObject co = (IComplexObject)SingletonObject.getComplexObject();
} catch(Exception ex) {
Console.WriteLine(ex.Message);
return ERR_EXCEPTION;
}
if (co.IsConfigured == false)
co.Configure();
if (co.IsConfigured == false)
return ERR_COULD_NOT_CONFIG;
return OK;
}
如果我调用一个更简单的函数,例如一个没有自身调用的函数,这些函数会返回一个复杂的对象(如IComplexObject
一切正常。 只要像(IComplexObject)SingletonObject.getComplexObject();
这样的内部调用(IComplexObject)SingletonObject.getComplexObject();
到位程序无法正常运行。 引发异常,指出“ 未找到请求的类型。 ”
如果您需要任何其他进一步的信息,请告诉我,我会尽力提供给您。
最后,我将向您展示我收到的控制台输出,以防有人可以使用该信息:
First-chance exception at 0x758f812f in agentsib.exe: Microsoft C++ exception: EEFileLoadException at memory location 0x0012d140..
First-chance exception at 0x758f812f in agentsib.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x758f812f in agentsib.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x758f812f in agentsib.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x758f812f in agentsib.exe: Microsoft C++ exception: EEFileLoadException at memory location 0x0012d140..
First-chance exception at 0x758f812f in agentsib.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x758f812f in agentsib.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x758f812f in agentsib.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x758f812f in agentsib.exe: 0xE0434F4D: 0xe0434f4d.
First-chance exception at 0x758f812f in agentsib.exe: 0xE0434F4D: 0xe0434f4d.
'agentsib.exe': Loaded 'C:\Windows\System32\sspicli.dll', Cannot find or open the PDB file
'agentsib.exe': Loaded 'C:\Windows\System32\dhcpcsvc.dll', Cannot find or open the PDB file
'agentsib.exe': Loaded 'C:\Windows\System32\dhcpcsvc6.dll', Cannot find or open the PDB file
The thread 'Win32 Thread' (0x550) has exited with code 2 (0x2).
The thread 'Win32 Thread' (0xf30) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x910) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x1dd8) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x1b64) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x1dfc) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x128c) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0xa0c) has exited with code -1073741510 (0xc000013a).
The program '[5068] agentsib.exe: Native' has exited with code -1073741510 (0xc000013a).
感谢您的任何帮助!
我的建议是:除了找出问题所在之外,您还可以:
编写一个混合C ++ / CLI DLL,它调用C#代码,并将功能导出为DLL导出功能。
C程序在混合DLL中调用导出的函数。
至于如何做到这一点,请检查此 。
尝试将CLR托管在C程序中,这是可能的,但是很复杂,并且难以调试。 此外,您还需要处理从本地到托管世界的数据转换,反之亦然。
另一种方法是:将C#DLL包装为COM,然后C DLL将通过COM使用C#DLL。 您不需要创建其他DLL,但是还有其他缺点:每个导出的C#类型都必须具有GUID。 数据转换通过COM类型进行,对于普通类型,它们很好; 对于数组,您需要使用SAFEARRY。 有关详细信息:请检查此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.