2016-10-09 6 views
0

Im Versuch von Methode in C# durch RobertGiesecke UnmanagedExportsC# Return-Schnittstelle von Dllexport

Hier meine Codes Schnittstelle zur Datenverschlüsselung zurückzukehren:

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

Meine exportierte Funktion:

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

bei C++ Teil:

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

}; 

Und hier Wrapper

typedef ICrypter*(*CreateCrypter)(); 

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

Nach diesem Code i GetProcAddress verwenden und testen Sie meine Add-Funktion für sicherstellen, dass meine Funktionen exportiert, aber ich nenne CreateCrypter und mein Programmabsturz des

mit Hilfe Gelöst von (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; 
    } 
+0

Stellen Sie sicher, Ihre Schnittstelle ist dekoriert mit 'Marshal' Attribute wie hier: https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports. – zahir

+0

Auch Sie möchten dies überprüfen: http://StackOverflow.com/Questions/4818850/IS-Possible-To-Export-Funktions-von-Ac-Sharp-Dll-like-in-VS-C – zahir

+0

@zahir, mein Projekt am meisten ComVisible? – moien

Antwort

0

gelöst mit Hilfe von (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; 
} 
Verwandte Themen