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

发布于 2024-12-05 20:54:07 字数 339 浏览 3 评论 0原文

Windows 8 Metro 下的 WinRT 是否允许动态加载和执行代码?例如,是否可以将 dll 下载到内存或隔离存储并从中运行代码? JIT 将脚本语言编译为本机汇编语言(例如第三方浏览器)的代码是否能够在 WinRT 中执行相同的操作,或者它是否被视为“不安全”操作而被禁止?

对于在 WinRT 中运行的“托管”代码,这个问题的答案是否不同?例如,在托管代码中,您能否从 Internet 下载程序集并使其在 MEF 中可发现,或者能够在运行时加载它?你能以某种形式使用 Reflection.Emit 吗?在 C++ 中,您可以运行应用程序在运行时生成的汇编代码,或者在运行时动态加载 DLL(可能是某种形式的 WinRT DLL)?

Does WinRT under windows 8 metro allow you to dynamically load and execute code? For example, is it possible to download a dll into memory or to isolated storage and run code from it? Could code that JIT compiles a scripting language to native assembly language (e.g. third party browsers) be able to do the same in WinRT, or is it prohibited as an "unsafe" operation?

Is the answer to this question different for "managed" code running in 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? Can you use Reflection.Emit in some form? 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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

╰◇生如夏花灿烂 2024-12-12 20:54:07

一般来说,您无法在 Metro 风格应用程序中加载和执行新代码。您可以访问的内容就是您随应用程序提供的内容。

LoadLibrary 和 GetProcAddress 缺失,因此 C++ 无法动态加载代码。
同样,C# 也不能,因为没有 Assembly.Load。
JavaScript 可以,但仅限于 Web 容器,而不是代码的更完全信任部分。

所有这一切的原因是,如果应用程序可以加载并运行任意代码,则商店的安全/恶意软件保护将毫无意义。

In general, you cannot load and execute new code in a Metro style app. What you can access is what you ship with the app.

LoadLibrary and GetProcAddress are missing so C++ can't dynamically load code.
Likewise, C# cannot because there is no Assembly.Load.
JavaScript can, but only in the web container and not in the fuller trust portions of the code.

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.

友欢 2024-12-12 20:54:07

实际上,Metro 风格的应用程序可以动态加载和执行代码。
有一些限制; Metro 和桌面应用程序在关键方面的工作方式略有不同。

机制略有不同,具体取决于调用者(LoadPackagingLibrary() Assembly.Load() 等)。 Metro 和桌面之间的一个关键区别 - Metro 应用程序只能动态加载应用程序包图中的内容(您的包和 s)和系统代码(否则可以静态加载)。

请参阅我的帖子了解更多详细信息
<一href="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

Actually, Metro style apps CAN dynamically load and execute code.
There are some restrictions; Metro and Desktop apps work a bit differently in key ways.

The mechanisms vary a bit, depending on the caller (LoadPackagedLibrary() Assembly.Load(), etc). 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).

See my post for some more details
http://social.msdn.microsoft.com/Forums/en-US/wingameswithdirectx/thread/d1ebe727-2d10-430e-96af-46964dda8225

伊面 2024-12-12 20:54:07

Windows 8 Metro 下的 WinRT 是否允许动态加载和
执行代码?

不。

<块引用>

例如,是否可以将 dll 下载到内存或隔离存储并从中运行代码?

不。

<块引用>

JIT 将脚本语言编译为本机汇编语言(例如第三方浏览器)的代码是否能够在 WinRT 中执行相同的操作,或者是否被视为“不安全”操作而被禁止?

这将被禁止。

<块引用>

对于在 WinRT 中运行的“托管”代码,此问题的答案是否不同?

不。

<块引用>

例如,在托管代码中,您能否从 Internet 下载程序集并使其在 MEF 中可发现,或者能够在运行时加载它?

不。

<块引用>

你能以某种形式使用Reflection.Emit吗?

不。

<块引用>

在 C++ 中,您可以运行应用程序在运行时生成的汇编代码,或者在运行时动态加载 DLL(可能是某种形式的 WinRT DLL)吗?

不。

您所描述的一切都会导致 WinRT 的安全保证被规避。

Does WinRT under windows 8 metro allow you to dynamically load and
execute code?

No.

For example, is it possible to download a dll into memory or to isolated storage and run code from it?

No.

Could code that JIT compiles a scripting language to native assembly language (e.g. third party browsers) be able to do the same in WinRT, or is it prohibited as an "unsafe" operation?

It would be prohibited.

Is the answer to this question different for "managed" code running in 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?

No.

Can you use Reflection.Emit in some form?

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)?

No.

Everything you described would allow the safety guarantees of WinRT to be circumvented.

只等公子 2024-12-12 20:54:07

您的问题有点不清楚......所以一些一般性的指示:

  • .NET 应用程序使用 WinRT 等(但不是新的 UI 模型!)
    在这种情况下,一切皆有可能,您今天(也许不是 OLEDB)但反射等。

  • 为 Metro UI 构建的 .NET 应用程序
    AFAIK这是不可能的(参见http://blogs.microsoft.co.il/blogs/sasha/archive/2011/09/17/metro-net-framework-profile-windows-tailored.aspxhttp://tirania.org/blog/) 至少只要您想通过 Windows 应用商店进行销售...在此范围之外(Windows 应用商店)可能有一些技巧可以规避该限制,正如某些人已经演示的那样...但我不会指望这一点...也许您可以使用一些 JavaScript(eval 等)来做一些动态的事情,但我不确定目前

You question is a bit unclear... so some general pointers:

  • .NET app using among other things WinRT (but NOT the new UI model!)
    In this case everything is possible that you today (perhaps not OLEDB) but Reflection etc.

  • .NET app built for Metro UI
    AFAIK this is not possible (see http://blogs.microsoft.co.il/blogs/sasha/archive/2011/09/17/metro-net-framework-profile-windows-tailored.aspx and http://tirania.org/blog/) at least as long as you want to sell via Windows Store... outside of this scope (Windows Store) there might some tricks to circumvent that restriction as already demonstrated by some... but I wouldn't count on that... MAYBE you can use some JavaScript (eval etc.) to do something dynamic but I am not sure currently

离旧人 2024-12-12 20:54:07

为此,Windows 10 添加了codeGeneration 功能。这允许在以下位置使用函数 VirtualAllocFromApp 和 VirtualProtectFromApp VirtualAlloc 和 VirtualProtect 等 WinRT 应用程序将在 Win32 中使用。

Windows 10 adds the codeGeneration capability for this purpose. This allows the functions VirtualAllocFromApp and VirtualProtectFromApp to be used in WinRT apps as VirtualAlloc and VirtualProtect would be used in Win32.

墨小墨 2024-12-12 20:54:07

更有趣的是,IE 10 实际上对其 js 代码执行 JIT,因此 API 显然允许这样做。

To make it more interesting, IE 10 does in fact do JIT on its js code, so the API is clearly there to allow it.

酒几许 2024-12-12 20:54:07

您可以创建一个Windows运行时组件(通用Windows)C++,
您可以从 C# 代码访问组件,在组件内部您可以调用 LoadPackagesLibrary,它的限制是它只能加载与您的应用程序一起打包的 DLL。否则与 LoadLibrary 相同。

您无法下载和动态加载,因为 ApplicationData 和 InstalledLocation 是不同的位置。 (LoadPackagesLibrary 不允许您指定路径)。而且你只能写入ApplicationData...

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. Otherwise it is same as LoadLibrary.

You cannot download and dynamically load, as ApplicationData and InstalledLocation are different locations. (LoadPackagedLibrary doesn not allow you to specify path). And you can only write to ApplicationData...

如日中天 2024-12-12 20:54:07

从 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;
}

必须将程序集添加到应用程序包中。您无法从外部源下载它们。

但是,这种方法在 .NET Native 中不起作用,因为所有内容都合并到单个 DLL 中。您应该将程序集名称列表保存在某处(在资产内的一个简单文件中),并为每个项目调用 Assembly.Load

调试模式下的动态程序集列表和发布模式下的预定义程序集名称数组的示例(.NET Native 工具链)。

#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.
        // ...

    }

Example of dynamic assembly loading from AppX package directory (from MSDN Forums):

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. You should save a list of assembly names somewhere (in a simple file inside Assets) and call Assembly.Load for each item.

Example of dynamic assembly listing in debug mode and predefined array of assembly names in release mode (.NET Native tool chain).

#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.
        // ...

    }
本王不退位尔等都是臣 2024-12-12 20:54:07

是的,这是可能的,检查 Windows 8 中的 Google Chrome 应用程序,他们提供了一种在正常模式和高级模式之间切换的方法。 Windows 8 模式会保留所查看的页面。

Ya its possible , Check the Google Chrome app in Windows 8 , They are providing a way to switch between normal mode & windows 8 mode persisting the pages viewed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文