菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
335
0

pinvoke 数据交互笔记

原创
05/13 14:22
阅读数 42688

intptr to array string

string[]  _outputStrArray=null;

     int channelCount = 0;///返回数组大小
            IntPtr Channels = new IntPtr();
          
            int reslut = NativeMethods.getChannels(Global.Handle, nru, ref Channels, ref channelCount);
            if (reslut == 0) //将指针数据转换为string 数组
            {
                IntPtr[] OutPointers = new IntPtr[channelCount];
                Marshal.Copy(Channels, OutPointers, 0, channelCount);
                _outputStrArray = new string[channelCount];
                for (int i = 0; i < channelCount; i++)
                {
                    _outputStrArray[i] = Marshal.PtrToStringAnsi(OutPointers[i]);
                }
            }

结构体数组指针

定义结构体

 public struct out_TMitObjIdKVPair
    {

...

...

..

}

IntPtr timitobjs = new IntPtr();

 int count = 0;

 int result = NativeMethods.getSubObjs(Global.Handle, par, ref timitobjs, ref count);
            if (result == 0)
            {
                for (int i = 0; i < count; i++)
                {
                    out_TMitObjIdKVPair q = (out_TMitObjIdKVPair)Marshal.PtrToStructure((IntPtr)(timitobjs.ToInt32() + i * Marshal.SizeOf(typeof(out_TMitObjIdKVPair))), typeof(out_TMitObjIdKVPair));

}

}

 

发表评论

0/200
335 点赞
0 评论
收藏