简体   繁体   English

在Windows 8中的WinRT上动态代码执行(C ++或.NET / C#)?

[英]Dynamic code execution on WinRT in Windows 8 (either C++ or .NET/C#)?

Does WinRT under windows 8 metro allow you to dynamically load and execute code? Windows 8 metro下的WinRT是否允许您动态加载和执行代码? For example, is it possible to download a dll into memory or to isolated storage and run code from it? 例如,是否可以将dll下载到内存或隔离存储并从中运行代码? Could code that JIT compiles a scripting language to native assembly language (eg third party browsers) be able to do the same in WinRT, or is it prohibited as an "unsafe" operation? 可以编写JIT编译脚本语言到本地汇编语言(例如第三方浏览器)的代码能够在WinRT中执行相同的操作,还是禁止将其作为“不安全”操作?

Is the answer to this question different for "managed" code running in WinRT? 对于在WinRT中运行的“托管”代码,这个问题的答案是否有所不同? For example, in managed code, could you download an assembly from the internet and have it be discoverable in MEF or otherwise be able to load it at runtime? 例如,在托管代码中,您是否可以从Internet下载程序集并使其在MEF中可被发现或以其他方式在运行时加载它? Can you use Reflection.Emit in some form? 你能用某种形式的Reflection.Emit吗? In C++, can you run assembly code generated at runtime by your application, or dynamically load a DLL at runtime (presumably some form of WinRT DLL)? 在C ++中,您可以运行应用程序在运行时生成的汇编代码,还是在运行时动态加载DLL(可能是某种形式的WinRT DLL)?

In general, you cannot load and execute new code in a Metro style app. 通常,您无法在Metro风格应用中加载和执行新代码。 What you can access is what you ship with the app. 您可以访问的是您随应用程序提供的内容。

LoadLibrary and GetProcAddress are missing so C++ can't dynamically load code. 缺少LoadLibrary和GetProcAddress,因此C ++无法动态加载代码。
Likewise, C# cannot because there is no Assembly.Load. 同样,C#也不能,因为没有Assembly.Load。
JavaScript can, but only in the web container and not in the fuller trust portions of the code. JavaScript可以,但只能在Web容器中,而不是在代码的更全面的信任部分。

The reason for all this is that the store's security/malware protection would be moot if an app could just load and run arbitrary code. 所有这一切的原因是,如果应用程序只能加载和运行任意代码,商店的安全/恶意软件保护将没有实际意义。

Actually, Metro style apps CAN dynamically load and execute code. 事实上,Metro风格应用程序可以动态加载和执行代码。 There are some restrictions; 有一些限制; Metro and Desktop apps work a bit differently in key ways. Metro和Desktop应用程序在关键方面的工作方式略有不同。

The mechanisms vary a bit, depending on the caller (LoadPackagedLibrary() Assembly.Load(), etc). 机制有所不同,具体取决于调用者(LoadPackagedLibrary()Assembly.Load()等)。 One key difference between Metro and Desktop - Metro apps can only dynamically load what's in your app's package graph (your package and s) and system code (that could be otherwise loaded statically). Metro和桌面之间的一个关键区别 - Metro应用程序只能动态加载应用程序包图(包和s)和系统代码(可以静态加载)中的内容。

See my post for some more details http://social.msdn.microsoft.com/Forums/en-US/wingameswithdirectx/thread/d1ebe727-2d10-430e-96af-46964dda8225 有关更多详细信息,请参阅我的帖子http://social.msdn.microsoft.com/Forums/en-US/wingameswithdirectx/thread/d1ebe727-2d10-430e-96af-46964dda8225

Does WinRT under windows 8 metro allow you to dynamically load and execute code? Windows 8 metro下的WinRT是否允许您动态加载和执行代码?

No. 没有。

For example, is it possible to download a dll into memory or to isolated storage and run code from it? 例如,是否可以将dll下载到内存或隔离存储并从中运行代码?

No. 没有。

Could code that JIT compiles a scripting language to native assembly language (eg third party browsers) be able to do the same in WinRT, or is it prohibited as an "unsafe" operation? 可以编写JIT编译脚本语言到本地汇编语言(例如第三方浏览器)的代码能够在WinRT中执行相同的操作,还是禁止将其作为“不安全”操作?

It would be prohibited. 这将被禁止。

Is the answer to this question different for "managed" code running in WinRT? 对于在WinRT中运行的“托管”代码,这个问题的答案是否有所不同?

No. 没有。

For example, in managed code, could you download an assembly from the internet and have it be discoverable in MEF or otherwise be able to load it at runtime? 例如,在托管代码中,您是否可以从Internet下载程序集并使其在MEF中可被发现或以其他方式在运行时加载它?

No. 没有。

Can you use Reflection.Emit in some form? 你能用某种形式的Reflection.Emit吗?

No. 没有。

In C++, can you run assembly code generated at runtime by your application, or dynamically load a DLL at runtime (presumably some form of WinRT DLL)? 在C ++中,您可以运行应用程序在运行时生成的汇编代码,还是在运行时动态加载DLL(可能是某种形式的WinRT DLL)?

No. 没有。

Everything you described would allow the safety guarantees of WinRT to be circumvented. 您描述的所有内容都可以避免WinRT的安全保障。

Windows 10 adds the codeGeneration capability for this purpose. Windows 10为此目的添加了codeGeneration功能。 This allows the functions VirtualAllocFromApp and VirtualProtectFromApp to be used in WinRT apps as VirtualAlloc and VirtualProtect would be used in Win32. 这允许在WinRT应用程序中使用VirtualAllocFromApp和VirtualProtectFromApp函数,因为VirtualAlloc和VirtualProtect将在Win32中使用。

You question is a bit unclear... so some general pointers: 你的问题有点不清楚...所以一些一般指示:

为了使它更有趣,IE 10实际上确实在其js代码上执行JIT,因此API显然允许它。

You can create a Windows Runtime Component (Universal Windows) C++, You access the Component from c# code, inside the component you can call LoadPackagedLibrary, which has the restriction that it can only load DLL's that are packaged with your app. 您可以创建Windows运行时组件(通用Windows)C ++,您可以从可以调用LoadPackagedLibrary的组件内部的c#代码访问组件,该组件的限制是它只能加载随应用程序打包的DLL。 Otherwise it is same as LoadLibrary. 否则它与LoadLibrary相同。

You cannot download and dynamically load, as ApplicationData and InstalledLocation are different locations. 您无法下载和动态加载,因为ApplicationData和InstalledLocation是不同的位置。 (LoadPackagedLibrary doesn not allow you to specify path). (LoadPackagedLibrary不允许您指定路径)。 And you can only write to ApplicationData... 而你只能写入ApplicationData ...

Example of dynamic assembly loading from AppX package directory (from MSDN Forums ): 从AppX包目录(来自MSDN论坛 )加载动态程序集的示例:

private async Task<IEnumerable<Assembly>> GetAssemblyListAsync()
{
    var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

    List<Assembly> assemblies = new List<Assembly>();
    foreach (StorageFile file in await folder.GetFilesAsync())
    {
        if (file.FileType == ".dll" || file.FileType == ".exe")
        {
            var name = file.Name.Substring(0, file.Name.Length - file.FileType.Length);
            Assembly assembly = Assembly.Load(new AssemblyName() { Name = name });
            assemblies.Add(assembly);
        }
    }

    return assemblies;
}

The assemblies must be added to the application package. 必须将程序集添加到应用程序包中。 You can't download them from external source. 您无法从外部源下载它们。

However, this approach does not work in .NET Native , because everything is merged into a single DLL. 但是, 此方法在.NET Native中不起作用 ,因为所有内容都合并到一个DLL中。 You should save a list of assembly names somewhere (in a simple file inside Assets) and call Assembly.Load for each item. 您应该在某处保存一个程序集名称列表(在Assets中的一个简单文件中),并为每个项目调用Assembly.Load

Example of dynamic assembly listing in debug mode and predefined array of assembly names in release mode (.NET Native tool chain). 调试模式下的动态程序集列表示例以及发布模式下的预定义程序集名称数组(.NET本机工具链)。

#if DEBUG
using Windows.Storage;
#endif

    // ...

    IEnumerable<string> assemblyNames;

#if DEBUG
    assemblyNames = Windows.ApplicationModel.Package.Current.InstalledLocation.GetFilesAsync().AsTask().Result
        .Where(file => file.FileType == ".dll" && file.Name.Contains("Business"))
        .Select(file => file.Name.Substring(0, file.Name.Length - file.FileType.Length));
#else
    assemblyNames = new[] { "California.Business", "Colorado.Business" };
#endif

    foreach (var name in assemblyNames)
    {
        var assembly = Assembly.Load(new AssemblyName() { Name = name });

        // Load required types.
        // ...

    }

雅可能,检查Windows 8中的谷歌Chrome应用程序,它们提供了一种在正常模式和Windows 8模式之间切换的方法,可以持久查看所查看的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM