c# 两个intPtr之间内存拷贝
2017-07-12 17:31
253 查看
c#中的March 提供了intPtr数组与intPtr之间的转换,但intPtr之间的却没有,尝试过引入window的copymemory但是拷贝出来的是错的,经多次尝试发现借助byte[]是可以完成的。
下面直接上代码:
public struct SCENE
{
public uint dwSize;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 40, ArraySubType = System.Runtime.InteropServices.UnmanagedType.I1)]
public byte[] Name;
}
int tempSize=Marshal.SizeOf(typeof(SCENE));
int tempTolSize= Marshal.SizeOf(typeof(uint))+tempSize*10;
intPtr pTotal=Marshal.AllocHGlobal(tempTolSize);
//过程处理获得pTotal的数据
//获取有效个数
byte[] tempbyte = new byte[Marshal.SizeOf(typeof(uint))];
Marshal.Copy(pstruWinWallParam, tempbyte, 0, Marshal.SizeOf(typeof(uint)));
uint count = System.BitConverter.ToUInt32(tempbyte,0);
//获取详细信息
byte[] tempbyte1 = new byte[tempSize];
for (int i = 0; i < count; i++)
{
Marshal.Copy(pstruWinWallParam + uintSize + dwsize * i, tempbyte1, 0, dwsize);
SCENE scene = (SCENE)BytesToStruct(tempbyte1, typeof(SCENE));
}
//BytesToStruct方法
public static object BytesToStruct(byte[] bytes, Type strcutType)
{
int size = Marshal.SizeOf(strcutType);
IntPtr buffer = Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(bytes, 0, buffer, size);
return Marshal.PtrToStructure(buffer, strcutType);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
下面直接上代码:
public struct SCENE
{
public uint dwSize;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 40, ArraySubType = System.Runtime.InteropServices.UnmanagedType.I1)]
public byte[] Name;
}
int tempSize=Marshal.SizeOf(typeof(SCENE));
int tempTolSize= Marshal.SizeOf(typeof(uint))+tempSize*10;
intPtr pTotal=Marshal.AllocHGlobal(tempTolSize);
//过程处理获得pTotal的数据
//获取有效个数
byte[] tempbyte = new byte[Marshal.SizeOf(typeof(uint))];
Marshal.Copy(pstruWinWallParam, tempbyte, 0, Marshal.SizeOf(typeof(uint)));
uint count = System.BitConverter.ToUInt32(tempbyte,0);
//获取详细信息
byte[] tempbyte1 = new byte[tempSize];
for (int i = 0; i < count; i++)
{
Marshal.Copy(pstruWinWallParam + uintSize + dwsize * i, tempbyte1, 0, dwsize);
SCENE scene = (SCENE)BytesToStruct(tempbyte1, typeof(SCENE));
}
//BytesToStruct方法
public static object BytesToStruct(byte[] bytes, Type strcutType)
{
int size = Marshal.SizeOf(strcutType);
IntPtr buffer = Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(bytes, 0, buffer, size);
return Marshal.PtrToStructure(buffer, strcutType);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
相关文章推荐
- c#两个数据库之间进行表拷贝
- (转载)在C#用WM_COPYDATA消息来实现两个进程之间传递数据
- 两个或多个进程之间通过Win32 API实现内存共享的方法(转)
- 将字符串指针ps1 和 ps2之间的内存拷贝出来
- C#中求两个日期之间相隔的天数
- 如何在C#用WM_COPYDATA消息来实现两个进程之间传递数据
- 如何在C#用WM_COPYDATA消息来实现两个进程之间传递数据
- 将字符串指针ps1 和 ps2之间的内存拷贝出来
- C#:求两个日期之间的天数
- 计算两个日期之间工作日天数(c#实现)
- 写一个函数,完成内存之间的拷贝。[考虑问题是否全面](转)
- 如果在C#用WM_COPYDATA消息来实现两个进程之间传递数据
- 如何在C#用WM_COPYDATA消息来实现两个进程之间传递数据
- c# 取string中的两个字符串之间的一段字符串
- 如果在C#用WM_COPYDATA消息来实现两个进程之间传递数据
- 在两个表之间根据相同名称栏位拷贝描述
- C#中两个类之间有继承关系时的执行顺序~
- 两个类之间复制(相当于深拷贝),
- C#获取两个日期之间天数
- 如果在C#用WM_COPYDATA消息来实现两个进程之间传递数据