从纯C程序调用的DLL函数(在C#.NET中)会导致错误

时间:2014-02-04 17:17:16

标签: c# .net c visual-studio-2010 exception

我有一个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).

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我的建议:您可以:

,而不是找出问题所在
  1. 编写一个混合C ++ / CLI DLL,它调用C#代码,并将该功能导出为DLL导出函数。

  2. C程序调用混合DLL中的导出函数。

  3. 如何执行此操作,请检查This

    尝试在C程序中托管CLR,这是可能的,但很复杂,而且很难调试。此外,您需要处理从原生世界到托管世界的数据转换,反之亦然。

    另一种方法是:将C#DLL包装为COM,然后C DLL将通过COM使用C#DLL。您不需要创建其他DLL,但还有其他缺点:每个导出的C#类型都必须具有GUID;数据转换通过COM类型,对于普通类型,它们很好;对于数组,您需要使用SAFEARRY。有关详情:请查看this

相关问题