在Silverlight 5中为Marshal.PtrToStructure调用Win32函数 [英] pInvoke Win32 function for Marshal.PtrToStructure in Silverlight 5

查看:85
本文介绍了在Silverlight 5中为Marshal.PtrToStructure调用Win32函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用过

public static Object PtrToStructure(
IntPtr ptr,
Type structureType
)

private static object ReadStruct(byte[] buffer, Type t)
{
    GCHandle handle =
        GCHandle.Alloc(buffer,
        GCHandleType.Pinned);
    Object temp =
        Marshal.PtrToStructure(
        handle.AddrOfPinnedObject(),
        t);
    handle.Free();
    return temp;
}

用于在.net 4.0中封送数据. Silverlight不支持此方法.根据下面的文章,.net方法仅是本机win32函数的包装器.给出了Marshal.AllocHGlobal的示例(来自Kernel32.dll的Win32 LocalAlloc函数)

for marshaling data in .net 4.0. Silverlight does not support this method. According to the following article, the .net method is simply a wrapper for a native win32 function. An example was given for Marshal.AllocHGlobal (Win32 LocalAlloc function from Kernel32.dll)

http://blogs.msdn.com/b/silverlight_sdk/archive/2011/09/27/pinvoke-in-silverlight5-and-net-framework.aspx

由于SL5刚刚启用了受信任的应用程序的pInvoke功能(在浏览器中也是如此),所以这是新的领域.PtrToStructure包装的win32函数是什么,是否有任何阻止在SL5中使用它的功能?

These are new waters since SL5 just enabled pInvoke for trusted applications (in browser as well) What is the win32 function that PtrToStructure wraps and is there anything preventing using this in SL5?

推荐答案

使用您选择的反编译器,您将看到 mscorlib.dll 程序集中确实实现了PtrToStructure(IntPtr, Type). Silverlight 5:

Using a decompiler of your choice, you will be able to see that PtrToStructure(IntPtr, Type) is indeed implemented in the mscorlib.dll assembly of Silverlight 5:

[SecurityCritical]
[ComVisible(true)]
[FriendAccessAllowed]
internal static object PtrToStructure(IntPtr ptr, Type structureType)

但是,internal不能被其他程序集访问. PtrToStructure依次调用以下方法:

Being internal it is however not accessible to other assemblies. PtrToStructure in turn calls the following method:

[MethodImpl(MethodImplOptions.InternalCall)]
private static void PtrToStructureHelper(IntPtr ptr, object structure, bool allowValueClasses);

在公共语言运行时中实现.

which is implemented in the common language runtime.

我无法识别 win32 函数.但是,由于PtrToStructure方法保留[SecurityCritical]属性,因此如果对Silverlight P/Invoke 调用这些 win32 函数进行调用,则很有可能会导致安全性例外.

I have not been able to identify the win32 function(s) called by PtrToStructureHelper. However, since the PtrToStructure method holds the [SecurityCritical] attribute it is highly likely that Silverlight P/Invoke calls to these win32 functions, if identified, would lead to security exceptions.

这篇关于在Silverlight 5中为Marshal.PtrToStructure调用Win32函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆