3

I have trouble working with an unmanaged Assembly from my WPF Application. Here's my setup, boiled down to a simple sketch:

X64 RUNTIME____________________
|                              |
| AS_EXEC (executing Assy, x86)|
|   |                          |
|   |                          |
|   AS_INT (interfaces, AnyCpu)|
|   |                          |
|   |                          |
|   AS_WRA_1 (wrapper, x86)    |
|   | |                        |
|   | AS_UNM_1 (unmanaged, x86)|
|   |                          |
|   AS_WRA_2 (wrapper, x64)    |
|   | |                        |
|   | AS_UNM_2 (unmanaged, x64)|
|   |                          |
|   AS_WRA_3 (wrapper, x86)    |
|     |                        |
|     AS_UNM_3 (unmanaged, x86)|
|______________________________|

What I want to do is test AS_WRA_1.

  • Since AS_WRA_1 references unmanaged code that will work in X86 only, I consider it best to set this project to X86 itself - just to prevent usage that will cause BadImageFormat exceptions.

  • AS_INT contains interfaces for all AS_WRA implementations and is set to ANY CPU.

  • I'm in 64 Bit runtime, so I also set AS_EXEC to X86.

  • AS_WRA_2 works in 64 Bit only, AS_WRA_3 32 Bit (that means basically I cannot run them at the same time, but since I want to test AS_WRA_1 only, I more or less happily neglect that - welcome back, DLL hell!).

At runtime, I use reflection to create an instance from the wrapper the user selects in the UI. Strangely, the result is the following:

  • I can create instances from AS_WRA_2
  • I get BadImageFormatExceptions for AS_WRA_1 and AS_WRA_3.

This is exactly the opposite of what I had expected...What am I doing wrong here?

Sebastian Edelmeier
  • 4,095
  • 3
  • 39
  • 60
  • I guess your AS_INT will be x64 because you're on a x64 machine (afaik the underlying OS platform is used), which probably causes the BadImageException. I ran into this also a couple of times and had to set the platform explicit to make it working. – inva Aug 27 '12 at 09:32
  • Set it, but the result is even more confusing: My application won't even start and fail with a BadImageFormatException for `AS_INT`. `AS_INT` has no external references, just a few interfaces. – Sebastian Edelmeier Aug 27 '12 at 09:41
  • According to [this link](http://stackoverflow.com/questions/1953377/how-to-know-a-process-is-32-bit-or-64-bit-programmatically), my application starts as 64 Bit process, although I set it to compile as `X86`. My fault : Within the configuration Manager,`AS_EXEC` was set to `X86`, but in the project settings it was still set to target `ANY CPU`. Yikes. – Sebastian Edelmeier Aug 27 '12 at 10:02

1 Answers1

0

If you have an executable compiled with x86 (Assy) it will always run in x86, and all its dependencies have to be in x86 or in AnyCpu (they will get executed in x86 too).

You cannot have an executable that executes unmanaged resources in x86 and x64 a the same time so you won't be able to access AS_UNM_1 and AS_UNM_2 from Assy as AS_UNM_2 is x64 and Assy is x86. As AS_INT is called by Assy it becomes a x86 too.

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207