简体   繁体   中英

DLL function (in C# .NET) called from pure C program causes error

I have a C program that needs some functionallity of a library that is written in C# .NET. After I googled up a way how I could do that didn't took long until I hit another problem.

It seems that calling a method/function from that library is not a problem as long as there are no compley types are returned by functions ( note: by now this is just my guess but this is the behavior I observed so far).

I want to show you how the code looks like ( note: this is not the actual code but it contains those parts which could be interesting):

// 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
                 );

    // ...
}

The C# Code, note that all functions I am calling using ICLRRuntimeHost_ExecuteInDefaultAppDomain must have the same signature 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;
}

If I call a simpler function eg one that has no calls itself to functions which return a complex object like IComplexObject everything works fine. As soon as an internal call like (IComplexObject)SingletonObject.getComplexObject(); comes into place the program does not work correctly. An exception gets thrown saying " Requested type was not found. "

If you need any further information about anything let me know and I will try to give it to you.

At the end I'll show you the console output I am receiving in case someone can use that information:

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).

Thank you for any help!

My suggestion: instead of figuring out what is the problem, you can:

  1. Write a mix C++/CLI DLL, it calls the C# code, and exported the functionality as DLL exported functions.

  2. The C program calls the exported functions in the mixed DLL.

As how to do this, please check This .

Try to host the CLR in C program, it is possible, but complex, and hard to debug. Also, you need handle the data conversion from native to managed world, and vice verse.

Another way is: wrap the C# DLL as COM, then C DLL will use the C# DLL via COM. You don't need to create an additional DLL, but there're other drawbacks: each exported C# type has to have a GUID; data conversion goes through COM type, for plain types, they're fine; for arrays, you need to use SAFEARRY. For details: please check this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM