Exception in Wrapper for using managed c# code in unmanaged c application/dll

0

I wrote a wrapper to use managed c# in an unmanaged c/c++ applications.

I got an example from: https://www.codeproject.com/Tips/695387/Calling-Csharp-NET-methods-from-unmanaged-C-Cplusp?

The unmanaged application (with the lib of the wrapper) is compiling without any errors. Calling of the wrapper (WrapperCheckLicense) also works fine until the call of "ECSlicCheckLicenseWrapper_CheckLicense". When "ECSlicCheckLicenseWrapper_CheckLicense" is called, there is an exception (in the code in try/catch).

Exception at 0x750D17D2 (KernelBase.dll) in Win32Project1.exe: 0xE0434352 (Parameter: 0x80131513, 0x00000000, 0x00000000, 0x00000000, 0x725F0000)

In EventLog:

Exception: System.MissingMethodException in .ECSlicCheckLicenseWrapper.ECSlicCheckLicenseWrapperCheckLicense.ECSlicCheckLicenseWrapper_CheckLicense(Int32, Int32*, Int32*)

My question is, why the "MissingMethodException" is thrown, if someone has an idea what could be wrong with my wrapper

C# code:

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace ECSlic
{
    public interface IECSlicCheckLicenseInterface
    {
        int ECSlic_CheckLicense(int nAppID, ref int nLicType, ref int nSerial);
    }

    public sealed class ECSlicCheckLicense : ECSlic, IECSlicCheckLicenseInterface
    {
        public ECSlicCheckLicense() : base()
        {

        }

        public int ECSlic_CheckLicense(int nAppID, ref int nLicType, ref int nSerial)
        {
            ...

Wrapper:

// ECSlicCheckLicenseWrapper.h

#pragma once
#include <iostream>
#include <exception>
#include "stdio.h"

using namespace System;
using namespace System::Reflection;

namespace ECSlicCheckLicenseWrapper
{
    public ref class ECSlicCheckLicenseWrapperCheckLicense
    {
      public:void ECSlicCheckLicenseWrapper_CheckLicense(int nAppId, int* nLicType, int* nSerial)
        {
            ECSlic::ECSlicCheckLicense^ MyClass = gcnew ECSlic::ECSlicCheckLicense();
            MyClass->ECSlic_CheckLicense(nAppId, *nLicType, *nSerial);
        }
    };
}

__declspec(dllexport) int WrapperCheckLicense(int nAppId, int* nLicType, int* nSerial)
{
    FILE* pFile;
    errno_t err;

    ECSlicCheckLicenseWrapper::ECSlicCheckLicenseWrapperCheckLicense work;

    try
    {
        work.ECSlicCheckLicenseWrapper_CheckLicense(nAppId, nLicType, nSerial);
    }
    catch (...)
    {
        ....
    }

    return (0);

}

C++-Test-Application (Win32Project1)

__declspec(dllexport) int WrapperCheckLicense(int nAppId, int *nLicType, int *nSerial);
...
...
nResult = WrapperCheckLicense(1, nLicType, nSerial);

Edit: The problem seems to be the call by ref parameters of the C# function. In the example from codeproject the C# function hasn't call by ref parameters and works fine.

c#
c
wrapper
unmanaged
managed
asked on Stack Overflow Aug 7, 2019 by Mikro79 • edited Aug 9, 2019 by Mikro79

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0