首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于C#的DllExport返回接口

基于C#的DllExport返回接口
EN

Stack Overflow用户
提问于 2016-10-09 10:59:58
回答 1查看 507关注 0票数 0

我试图用C#中的RobertGiesecke UnmanagedExports从方法返回数据加密接口

这里我的密码:

代码语言:javascript
运行
复制
    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);
}

我的导出功能:

代码语言:javascript
运行
复制
    [DllExport("CreateCrypter", CallingConvention.Cdecl)]
    public static ICrypter CreateCrypter()
    {
        return new Crypter();
    }

在C++部分:

代码语言:javascript
运行
复制
class ICrypter
{
 public:
    virtual int testFunc1();
    virtual int testFunc2();
 private:

};

在这里包装

代码语言:javascript
运行
复制
typedef ICrypter*(*CreateCrypter)();

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

在这段代码之后,我使用GetProcAddress并测试我的add函数,以确保我的函数导出,但是我调用了CreateCrypter和我的程序崩溃的

通过(http://code4k.blogspot.ae/2010/10/implementing-unmanaged-c-interface.html)的帮助解决

代码语言:javascript
运行
复制
    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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-13 13:34:53

通过(http://code4k.blogspot.ae/2010/10/implementing-unmanaged-c-interface.html)的帮助解决

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39942829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档