1

I'm using Assembly.Load() and then EntryPoint.Invoke(null, null) in order to run a .NET assembly in memory. This works, unless if the host process is x86 and the executed assembly is AnyCPU. In this case, the executed assembly gets executed in x86 context and therefore malfunctions.

Is there a way to execute the Assemly object in an AnyCPU context, even if the host process is x86?

Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
bytecode77
  • 14,163
  • 30
  • 110
  • 141
  • You could load it up in its own AppDomain. Of course, you would have to use a remote communication technique to talk with it (remoting, WCF, etc) – RQDQ Feb 10 '12 at 19:51
  • 1
    Why is the assembly malfunctioning when loaded in x86, after being built as AnyCPU? AnyCPU, by definition, should work on AnyCPU. If something has broken that, process bitness safety is being violated somewhere. – ssube Feb 10 '12 at 19:56
  • @RQDQ An AppDomain is not going to solve bitness problems. – vcsjones Feb 10 '12 at 20:08
  • @vcsjones - quite correct. I just did a repro and it failed miserably. – RQDQ Feb 10 '12 at 20:25
  • @peachykeen: The assembly needs to be run with the same bitness as the OS, otherwise it will not work. Is there still a way to run it as x64 from an x86 host process? – bytecode77 Feb 11 '12 at 13:33
  • From, yes; in, no. If the assembly needs to run with the same bitness as the OS, instead of the bitness of the program it's running in, you've already broken something and the assembly shouldn't be AnyCPU (since it obviously *isn't*). Things will work out much better if you can fix the assembly so it actually is AnyCPU. – ssube Feb 11 '12 at 13:44

1 Answers1

1

Executing the assembly in AnyCPU context doesn't really make sense. An assembly that targeted AnyCPU will JIT to 64-bit if loaded into a 64-bit process and 32-bit for a 32-bit process. If this assembly is dependent on the host process being 64-bit then it's platform target should be x64 as opposed to Any CPU.

Matt Dearing
  • 9,286
  • 1
  • 23
  • 25
  • That's true in most cases. What I'm trying to do as part of my project is to compress a .NET assembly. The assembly gets loaded and executed using EntryPoint.Invoke. If the assembly expects to be run as 64 bit and it gets run as 32 bit because the Host Process is 32 bit, it will not work. The other way around, compiling the host process as AnyCPU will not work if the child process is x86. – bytecode77 Feb 10 '12 at 20:19