0

Im trying to return Interface about data encryption from Method in C# by RobertGiesecke UnmanagedExports

Here my codes :

    public class Crypter : ICrypter
    {

    public bool Encrypt(IntPtr data)
    {
        /* Sorry I Can't Show How I Do This */

        Marshal.Copy(encdata, 0, data, encdata.Length);

        return true;
    }

    public bool Decrypt(IntPtr data)
    {
        /* Sorry I Can't Show How I Do This */

        return true;
    }
}

public interface ICrypter
{
    bool Encrypt(IntPtr data);
    bool Decrypt(IntPtr data);
}

My exported function :

    [DllExport("CreateCrypter", CallingConvention.Cdecl)]
    public static ICrypter CreateCrypter()
    {
        return new Crypter();
    }

at C++ part :

class ICrypter
{
 public:
    virtual int testFunc1();
    virtual int testFunc2();
 private:

};

And here Wrapper

typedef ICrypter*(*CreateCrypter)();

HMODULE mylib = LoadLibrary(L"C:\\Users\\Moien\\Documents\\Visual Studio 2013\\Projects\\UnmanagedInterfaces\\MyLibrary\\bin\\Debug\\MyLibrary.dll");

After that code i use GetProcAddress and test my add function for make sure my functions exported but i call CreateCrypter and my program crash's

Solved by help from (http://code4k.blogspot.ae/2010/10/implementing-unmanaged-c-interface.html)

    public static IntPtr GetInterfacePointer(Delegate[] functions)
    {
        // Allocate object layout in memory 
        // - pointer to VTBL table
        // - following that the VTBL itself - count of functions
        IntPtr nativePointer = Marshal.AllocHGlobal(IntPtr.Size * (1 + functions.Count()));

        // virtual table
        IntPtr vtblPtr = IntPtr.Add(nativePointer, IntPtr.Size);

        Marshal.WriteIntPtr(nativePointer, vtblPtr);

        for (int i = 0; i < functions.Count(); i++)
        {
            Marshal.WriteIntPtr(IntPtr.Add(vtblPtr, IntPtr.Size * i),
                Marshal.GetFunctionPointerForDelegate(functions[i]));
        }

        return nativePointer;
    }
moien
  • 999
  • 11
  • 26
  • Make sure your interface is decorated with `Marshal` attributes like here: https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports. – zahir Oct 09 '16 at 11:05
  • Also you may want to check this out: http://stackoverflow.com/questions/4818850/is-is-possible-to-export-functions-from-a-c-sharp-dll-like-in-vs-c – zahir Oct 09 '16 at 11:05
  • @zahir, my project most ComVisible ? – moien Oct 09 '16 at 12:36

1 Answers1

0

Solved by help from (http://code4k.blogspot.ae/2010/10/implementing-unmanaged-c-interface.html)

public static IntPtr GetInterfacePointer(Delegate[] functions)
{
    // Allocate object layout in memory 
    // - pointer to VTBL table
    // - following that the VTBL itself - count of functions
    IntPtr nativePointer = Marshal.AllocHGlobal(IntPtr.Size * (1 + functions.Count()));

    // virtual table
    IntPtr vtblPtr = IntPtr.Add(nativePointer, IntPtr.Size);

    Marshal.WriteIntPtr(nativePointer, vtblPtr);

    for (int i = 0; i < functions.Count(); i++)
    {
        Marshal.WriteIntPtr(IntPtr.Add(vtblPtr, IntPtr.Size * i),
            Marshal.GetFunctionPointerForDelegate(functions[i]));
    }

    return nativePointer;
}
moien
  • 999
  • 11
  • 26