11

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

2
  • For managed, at least, I don't see System.Reflection.Emit, but Expression<T>.Compile is there and seems to be working, so at least some form of runtime code generation is available. Commented Sep 19, 2011 at 17:25
  • 1
    To learn more about what you can do with C++ in Windows Store apps, check out this Roadmap for Windows Store apps using C++. Commented Nov 7, 2012 at 16:56

9 Answers 9

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

In addition, VirtualProtect is missing, so you can't write something that compiles code on the fly.
8

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

Comments

7

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.

4 Comments

It also excludes third party browsers, all manor of script engines (at least ones that use JIT to run scripts faster), any software that releases updates on a time-critical schedule (tax software, etc...). Software written in Ruby/Python/etc.. can't run as fast as they should, if at all, without JIT support. Most major PC games use some form of scripting engine internally, whether for the UI (World of Warcraft) or otherwise. The generated code can be validated at runtime for a small performance hit. I don't see a reason why this couldn't be a brokered API with an associated app capability
And you didn't want to vote my answer up because you are mad at Microsoft?
I'm not "mad" at anyone. This is just a technical Q&A. I meant no offense.
How would that allow the safety guarantees to be circumvented? It's not like this restriction limits what you can do: you can always dynamically run the code with an interpreter. The only difference is that that is much slower.
2

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

1 Comment

The question is completely clear, it is about WinRT. You are responding to another question not asked. Nowhere does the poster refer to a .NET app running things other than WinRT.
2

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.

Comments

1

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.

2 Comments

Yes. I believe technically you can use any Win32 api directly from C++, but if your application uses any non-winrt or any win32 api not on the approved list, your app won't pass certification. So, IE is using APIs not available to third party developers.
Or rather since IE10 on Win8 is a hybrid Metro/Win32 app, it gets to do things that standard Metro-only apps can't.
0

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

Comments

0

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

Comments

-1

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.

1 Comment

They are only making an exception browsers, and only on the x86 version of windows 8, Not Windows RT. Also, Chrome/FireFox are not windows store applications.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.