本文共 5747 字,大约阅读时间需要 19 分钟。
在Silverlight 4中受信应用已经被支持,受信应用简单的说就是可以访问一些本地受限资源的Silverlight应用。在Silverlight 4中受信应用必须是OOB类型,即必须先安装在本地,同时受信应用能访问的资源也有限,例如只能访问我的文档、我的图片等位置的文件。而在Silverlight 5中受信应用的功能做了很大的改进和增强:
可以看到,在Silverlight 5中受信应用的权限几乎获得了与桌面应用相当的权限,在这里,我将为大家介绍如何创建浏览器内的受信应用。以下示例将通过Silverlight来监视本地的网络流量。
public static class WIN32API { [DllImport("kernel32.dll")] public static extern bool GetComputerName(StringBuilder computerName, out Int32 nameLength); [DllImport("Iphlpapi.dll")] private static extern Int32 GetIfTable(Byte[] pIfTable, out long pdwSize, bool bOrder); [DllImport("kernel32.dll")] private static extern int MultiByteToWideChar(uint CodePage,Int32 dwFlags,byte[] lpMultiByteStr,Int32 cbMultiByte,byte[] lpWideCharStr, int cchWideChar); public static MIB_IFTABLE GetIfTable() { MIB_IFTABLE table = new MIB_IFTABLE(); long size = 0; GetIfTable(null, out size, false); if (size != 0) { Byte[] data = new Byte[size]; long ret = GetIfTable(data, out size, false); if (ret == 0) { MemoryStream ms = new MemoryStream(data); ms.Position = 0; BinaryReader br = new BinaryReader(ms, Encoding.Unicode); table.dwNumEntries = br.ReadInt32(); table.table = new MIB_IFROW[table.dwNumEntries]; for (int i = 0; i < table.dwNumEntries; i++) { table.table[i] = new MIB_IFROW(); MIB_IFROW curRow = table.table[i]; FieldInfo[] fis = typeof(MIB_IFROW).GetFields(); foreach (FieldInfo fi in fis) { if (!fi.IsStatic) { MarshalAsAttribute[] attrs = fi.GetCustomAttributes(typeof(MarshalAsAttribute), true) as MarshalAsAttribute[]; if (attrs != null && attrs.Length > 0 && attrs[0].SizeConst != 0) { if (fi.FieldType == typeof(String)) { Byte[] tmpChars = br.ReadBytes(attrs[0].SizeConst*2); fi.SetValue(curRow, Encoding.Unicode.GetString(tmpChars, 0, tmpChars.Length).TrimEnd('\0')); } else if (fi.FieldType == typeof(Byte[])) { fi.SetValue(curRow, br.ReadBytes(attrs[0].SizeConst)); } } else { if (fi.FieldType == typeof(Int32)) { Int32 tmpValue = br.ReadInt32(); fi.SetValue(curRow, tmpValue); } } } } } } } return table; } public static String AsciiToUTF8(Byte[] asciiBytes) { int mustBytes = MultiByteToWideChar(WIN32CONST.CP_ACP, WIN32CONST.MB_PRECOMPOSED, asciiBytes, -1, null, 0); if (mustBytes > 0) { Byte[] tmpBytes = new Byte[mustBytes*2]; if (MultiByteToWideChar(WIN32CONST.CP_ACP, WIN32CONST.MB_PRECOMPOSED, asciiBytes, -1, tmpBytes, mustBytes) != 0) { return Encoding.Unicode.GetString(tmpBytes, 0, tmpBytes.Length); } } return ""; } }
转载地址:http://wxcbz.baihongyu.com/