日期:2014-05-17  浏览次数:20960 次

由指针得到结构体数据的问题,请高手指点

我调用第三方的dll来读取设备中的设置,第三方dll中的方法要我给出数据缓冲区地址,和缓冲区长度。执行后,确无法从该缓冲区中取得任何数据,请问原因及处理办法。代码如下:

C# code

public override int GetServerNTP(IntPtr hDev, ref string NTPUrl, ref int timezone, ref bool enable, ref int reserve)
        {
            DVSNETClient.DVSNET_NTP_PARAM ntp = new DVSNETClient.DVSNET_NTP_PARAM();
            IntPtr pntp = Marshal.AllocHGlobal(Marshal.SizeOf(ntp));

            //Marshal.StructureToPtr(ntp, pntp, true);

            //IntPtr pntp = IntPtr.Zero;
            uint len = (uint)Marshal.SizeOf(new DVSNETClient.DVSNET_NTP_PARAM());
            int r = GetServerParam(hDev, (uint)(0x10000 + 224), 0,ref pntp, len);

            DVSNETClient.DVSNET_NTP_PARAM ntp = (DVSNETClient.DVSNET_NTP_PARAM)Marshal.PtrToStructure(pntp, typeof(DVSNETClient.DVSNET_NTP_PARAM));
            NTPUrl = new string(ntp.strNTPUrl);
            timezone = (int)ntp.nTimeZone;
            enable = ntp.bEnabled == 1 ? true : false;
            reserve = (int)ntp.dwReserve;
            return r;
        }



------解决方案--------------------
断点调试下,看看r是多少
------解决方案--------------------
IntPtr pntp = Marshal.AllocHGlobal(Marshal.SizeOf(ntp));

//Marshal.StructureToPtr(ntp, pntp, true);

//IntPtr pntp = IntPtr.Zero;
uint len = (uint)Marshal.SizeOf(new DVSNETClient.DVSNET_NTP_PARAM());
int r = GetServerParam(hDev, (uint)(0x10000 + 224), 0,ref pntp, len);//这句话有问题
如果用ref intptr 的话,c++相当于** , 意思是这个地址,是由dll方面创建....
改成intptr 试试,另外看看c++如何调用的?

------解决方案--------------------
oletime 在你的C# 里怎么转的?