C# Silverlight中的Marshal.ptr结构
我得到了一份Silverlight 5的作业,但我半途而废。让我解释一下 我的代码在Windows窗体(.Net framework 4)中运行良好C# Silverlight中的Marshal.ptr结构,c#,marshalling,silverlight-5.0,C#,Marshalling,Silverlight 5.0,我得到了一份Silverlight 5的作业,但我半途而废。让我解释一下 我的代码在Windows窗体(.Net framework 4)中运行良好 请帮助我解决此问题。我尝试使用GCHandle解决此问题,但没有成功。首先,用类重写您的结构。 例如: 然后创建类的实例 BS2UserHdr clsTest= new BS2UserHdr(); Marshal.PtrToStructure(new IntPtr(userInfo.ToInt32() + i * Marshal.SizeO
请帮助我解决此问题。我尝试使用GCHandle解决此问题,但没有成功。首先,用类重写您的结构。 例如: 然后创建类的实例
BS2UserHdr clsTest= new BS2UserHdr();
Marshal.PtrToStructure(new IntPtr(userInfo.ToInt32() + i * Marshal.SizeOf(typeof(LiteSDK.BS2UserHdr)), clsTest);
现在您可以在clsTest中看到这些值
而Marshal.AllocHGlobal将无法在SL中工作,您需要实现
internal static IntPtr AllocHGlobal(int size)
{
IntPtr hGlobal = LocalAlloc(LPTR, size);
if (hGlobal == IntPtr.Zero)
{
throw new OutOfMemoryException("Unmanaged memory was not allocated.");
}
return hGlobal;
}
要克服这个问题。为什么要使用编组?您只需要为与非托管代码的互操作使用封送处理。LiteSDK是一个C dll…好的-那么为什么不能在Silverlight中使用
Marshal.PtrToStructure
?Silverlight中的Marshal.PtrToStructure不包含返回类型实例的重载
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class BS2UserHdr
{
public uint ID { get; set; }
public ushort headerVersion { get; set; }..........
BS2UserHdr clsTest= new BS2UserHdr();
Marshal.PtrToStructure(new IntPtr(userInfo.ToInt32() + i * Marshal.SizeOf(typeof(LiteSDK.BS2UserHdr)), clsTest);
internal static IntPtr AllocHGlobal(int size)
{
IntPtr hGlobal = LocalAlloc(LPTR, size);
if (hGlobal == IntPtr.Zero)
{
throw new OutOfMemoryException("Unmanaged memory was not allocated.");
}
return hGlobal;
}