我有一个C程序需要一些用C#.NET编写的库的功能。在我用谷歌搜索了一下我怎么做的时间并没有花很长时间,直到我遇到另一个问题。
似乎从该库调用方法/函数不是问题,只要函数不返回补充类型(注意:到现在这只是我的猜测,但这是到目前为止我观察到的行为。)
我想向您展示代码的外观(注意:这不是实际代码,但它包含可能有趣的部分):
// 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();
到位,程序就无法正常工作。抛出一个异常,说“找不到请求的类型。”
如果您需要任何有关任何事情的进一步信息,请告诉我,我会尽力给您。
最后,我将向您展示我收到的控制台输出,以防有人可以使用该信息:
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).
感谢您的帮助!
答案 0 :(得分:1)
我的建议:您可以:
,而不是找出问题所在