从纯C程序调用DLL函数(在C#.NET中)会导致错误
我有一个C程序,它需要一些用C#NET编写的库的功能。在我用谷歌搜索出一种方法之后,没过多久我就遇到了另一个问题 从该库调用方法/函数似乎不是问题,只要函数没有返回复杂类型(注意:现在这只是我的猜测,但这是我迄今为止观察到的行为) 我想向您展示代码的样子(注意:这不是实际的代码,但它包含一些可能很有趣的部分): 如果我调用一个更简单的函数,例如,一个本身没有调用返回复杂对象(如从纯C程序调用DLL函数(在C#.NET中)会导致错误,c#,.net,c,visual-studio-2010,exception,C#,.net,C,Visual Studio 2010,Exception,我有一个C程序,它需要一些用C#NET编写的库的功能。在我用谷歌搜索出一种方法之后,没过多久我就遇到了另一个问题 从该库调用方法/函数似乎不是问题,只要函数没有返回复杂类型(注意:现在这只是我的猜测,但这是我迄今为止观察到的行为) 我想向您展示代码的样子(注意:这不是实际的代码,但它包含一些可能很有趣的部分): 如果我调用一个更简单的函数,例如,一个本身没有调用返回复杂对象(如IComplexObject的函数),则一切正常。一旦出现内部调用,如(IComplexObject)Singleton
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).
谢谢你的帮助 我的建议:你可以:
编写一个混合C++/CLI DLL,它调用C#代码,并将功能导出为DLL导出函数
C程序调用混合DLL中的导出函数
至于如何做到这一点,请检查
尝试将CLR托管在C程序中,这是可能的,但复杂且难以调试。此外,您还需要处理从本机到托管世界的数据转换,反之亦然
另一种方法是:将C#DLL包装为COM,然后C DLL将通过COM使用C#DLL。您不需要创建额外的DLL,但还有其他缺点:每个导出的C#type都必须有一个GUID;数据转换通过COM类型进行,对于普通类型,它们是可以的;对于阵列,您需要使用SAFEARRY。有关详细信息:请检查我们无法看到ICLRRuntimeHost\u ExecuteInDefaultAppDomain
。为什么不使用非托管索引端口或混合模式C++/CLI DLL?看起来您需要将内部运行时指向IComplexObject
所需的程序集。此外,堆栈跟踪的完全异常也会有所帮助。(程序调试输出不太有用)@DavidHeffernanICLRRuntimeHost\u ExecuteInDefaultAppDomain
是调用DLL函数的C函数。非托管DexPorts能否解决此问题?其中一个程序是用纯C编写的,我所说的库是用C#混合模式C++/CLI如何帮助我@我将在大约一小时内添加完整的堆栈跟踪。需要在另一台计算机上才能获取。C++/CLI可以使用C#程序集。这是官方支持的完成非托管Dexports功能的方法。不受管理的运动会让你的生活变得非常轻松。您不必在加载和初始化.net库时乱来。这一切都会神奇地发生。@StefanFalk我认为DavidHeffernan是在暗示你编写了ICLRRuntimeHost_ExecuteInDefaultAppDomain,因为它不是标准API的一部分。(我看到C++函数非常相似,但不是你提到的)这就是David Heffernan建议的,对吧?这意味着混合使用C++/CLI包装DLL。我想知道是否没有其他方法可以解决这个问题,因为我们在这里讨论的库非常大,而且包装器可能会添加大量需要进行的维护。不确定在不进行非托管数据端口的额外工作(需要额外的编译步骤)的情况下,使用#2(即使DLL是C++/CLI)能走多远因此,C++/CLI这件事并不是让我高兴的吗?@ebyrob我认为C++CLI不需要非托管数据端口。它只是使用dllexport导出函数或类。@Matt I stand corrected在给出调试警告的情况下,看起来IJW会这样做。(尽管跟踪和控制台仍然可以正常工作)COM似乎永远都不是一个好主意,除非你出于某种原因不得不拥有它。我认为在.NET2.0和.NET4.0之间的某个地方,“额外的编译步骤”一定是自动完成的。
/// 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;
}
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).