首页 新闻 会员 周边 捐助

C#调用C++dll Struct函数

0
悬赏园豆:20 [已关闭问题] 关闭于 2019-04-23 14:49

[code=c]C++ Struct
struct double_dynamicArrayStruct
{
unsigned long length = 0;
double * data = nullptr;//这里原本是使用模板的
};

struct Xx_dynamicArrayStruct
{
unsigned long length = 0;
double * data = nullptr;//这里原本是使用模板的
};

C++ dll函数
double_dynamicArrayStruct newDynamicStruct(unsigned long length);
void Xx_deleteDynamicArrayStruct(double_dynamicArrayStruct&dynamicArrayStruct);

C++ exe运行结果图
[img=https://img-bbs.csdn.net/upload/201904/19/1555643149_684507.jpg][/img]

[code=csharp]C# struct
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct double_dynamicArrayStruct
{
public int length;
public double[] data;
};

引用C++Dll函数
//[DllImport("directShowUseonly.dll", EntryPoint = "getRec0_A2", CallingConvention = CallingConvention.StdCall)]
//public static extern IntPtr getReco_A2(int widths, int heights, IntPtr pData_16bitGray, IntPtr pData_16bitSrc, int widthM, int heightM, IntPtr pData_16bitModel, int referNumbersOfObject = 0);
[DllImport("directShowUseonly.dll", EntryPoint = "double_newDynamicArrayStruct", CallingConvention = CallingConvention.StdCall)]
public static extern double_dynamicArrayStruct double_newDynamicArrayStruct(int length);

    [DllImport("directShowUseonly.dll", EntryPoint = "Xx_deleteDynamicArrayStruct", CallingConvention = CallingConvention.StdCall)]
    public static extern void Xx_deleteDynamicArrayStruct(double_dynamicArrayStruct[] dynamicArrayStruct);


    [DllImport("directShowUseonly.dll", EntryPoint = "Xx_newDynamicArrayStruct", CallingConvention = CallingConvention.StdCall)]
    public static extern Xx_dynamicArrayStruct Xx_newDynamicArrayStruct(int length);

    //[DllImport("directShowUseonly.dll", EntryPoint = "?double_deleteDynamicArrayStruct@@YAXAEAUdouble_dynamicArrayStruct@@@Z", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    [DllImport("directShowUseonly.dll", EntryPoint = "Xx_deleteDynamicArrayStruct", CallingConvention = CallingConvention.StdCall)]
    public static extern void Xx_deleteDynamicArrayStruct(ref Xx_dynamicArrayStruct dynamicArrayStruct);

// 调用复杂结构体测试
int size = Marshal.SizeOf(typeof(double_dynamicArrayStruct)) * 100;
IntPtr pBuff = Marshal.AllocHGlobal(size); // 直接分配50个元素的空间,比Marshal.copy方便多了

        //int intsize = Marshal.SizeOf(typeof(double_dynamicArrayStruct));//获取结构体占用空间大小
        //IntPtr intptr= Marshal.AllocHGlobal(size);//声明一个同样大小的空间
        //Marshal.StructureToPtr(typeof(double_dynamicArrayStruct), intptr, true);//将结构体放到这个空间中

        double_newDynamicArrayStruct(10);

        double_dynamicArrayStruct[] pClass = new double_dynamicArrayStruct[10];
        for (int i = 0; i < 10; i++)
        {
            IntPtr ptr = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(double_dynamicArrayStruct)) * i);
           
            pClass[i] = (double_dynamicArrayStruct)Marshal.PtrToStructure(ptr, typeof(double_dynamicArrayStruct));
            Console.WriteLine(pClass[i]);
        }
       
        Marshal.FreeHGlobal(pBuff); // 释放内存  
        Console.Read();

总是提示尝试读取或写入受保护的内存,尝试了何种方式,都没有得到解决,有时候还出现其他错误

[/code]

zhouyi88728的主页 zhouyi88728 | 菜鸟二级 | 园豆:404
提问于:2019-04-19 11:03

支持 markdown 代码高亮,建议使用 markdown 语法

dudu 5年前
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册