Calling static function of C# .NET in C (ICLRRuntimeHost_ExecuteInDefaultAppDomain)

0

I have this class KernelHelper which is written in C# .NET Framework 2.0. What I want to do is call its static functions in a C program.

namespace Kernel.Client {

    public class KernelHelper {

        public static int testc(string msg) {
            // Removing the message box does not change anything
            System.Windows.Forms.MessageBox.Show(msg);
            return 0;
        }

        // ...

    }
}

which compiles and does not seem to make any problems so far. But calling ICLRRuntimeHost_ExecuteInDefaultAppDomain() returns 0x80131513 which is according to this the fact that I didn't follow the correct signature convention. But this can not be the problem.

#if defined(_WIN32)
#   include <Windows.h>
#   define COBJMACROS
#   define CINTERFACE
#   include <mscoree.h>
#endif 

// ...

HRESULT status;
ICLRRuntimeHost *Host;
BOOL Started;
DWORD Result;

Host = NULL;
Started = FALSE;

status = CorBindToRuntimeEx(
             NULL,
             NULL,
             0,
             &CLSID_CLRRuntimeHost,
             &IID_ICLRRuntimeHost,
             (PVOID *)&Host
             );

if (FAILED(status)) {
    printf("failed 1\n");
}

status = ICLRRuntimeHost_Start(Host);
if (FAILED(status)) {
    printf("failed 2\n");
}

Started = TRUE;

status = ICLRRuntimeHost_ExecuteInDefaultAppDomain(
             Host,
             L"C:\\svn\\Server\\Kernel\\interface\\bin\\Kernel.Client.dll",
             L"Kernel.Client.KernelHelper",
             L"testc",
             L"My message",
             &Result
             ); 

if (FAILED(status)) {
    printf("failed 3\n");
}

Could anybody help me here?

Edit: I tried it also without the message box and let the function just return 0 but it didn't change a thing.

c#
.net
visual-studio-2010
dll
clr
asked on Stack Overflow Feb 3, 2014 by Stefan Falk • edited Feb 3, 2014 by Stefan Falk

1 Answer

0

Wow, I did not expect to find the solution so fast and on my own. If you experiance the same problem try this. It seems not to work for everyone.

All I needed to do is to change the Platform Target from Any CPU to x86 inside my project properties.

Edit: If anyone can show me another solution I'd still be glad since I can not tell if this might not be a problem anyway changing this setting.

enter image description here

answered on Stack Overflow Feb 3, 2014 by Stefan Falk

User contributions licensed under CC BY-SA 3.0