none
Managed AnyCPU with unmanaged dll RRS feed

  • Question

  • Hello. I read everywhere that AnyCPU runs accordingly a platform on which it's running. Then why can't I set AnyCPU for managed program and let's say x64 for native dll? I expect that it will run according to my laptop platform but instead I have BadImageFormat exception when my program calls a function form this dll, despite the fact that this dll and my laptop both x64. Why is that?

    • Edited by Booster1 Wednesday, March 23, 2016 4:40 AM
    Wednesday, March 23, 2016 4:38 AM

All replies

  • AnyCPU allows it to be loaded into a 32 or 64 bit process, but does not guarantee that the process will be any particular bitness. Even if the exe used to launch the process is AnyCPU, it might be flagged as "Prefer 32-bit" (I believe that's the default in current versions of visual studio.)

    Wednesday, March 23, 2016 9:43 AM
  • Ok. How can we change or remove this "Prefer 32-bit"?
    Wednesday, March 23, 2016 10:07 AM
  • Visual Studio C# places the option on the "Build" tab of the project properties, just below the platform target. Alternatively, you can use CorFlags /32BITPREF- to turn it off after compilation.

    Wednesday, March 23, 2016 8:46 PM
  • Visual Studio C# places the option on the "Build" tab of the project properties, just below the platform target. Alternatively, you can use CorFlags /32BITPREF- to turn it off after compilation.

    It's embarrassing because in that case  it's not AnyCPU but x86. :) Why have they done it?
    Wednesday, March 23, 2016 9:31 PM
  • Hi Booster,

    >>despite the fact that this dll and my laptop both x64

    I would suggest you rebuild your program to x64 and try again.  and make sure that you are not having 32-bit / 64-bit conflict.

    If you're running on a 64-bit OS, the Assembly RevitAPI may be compiled as 32-bit and your process as 64-bit or "Any CPU".

    Or, the RevitAPI is compiled as 64-bit and your process is compiled as 32-bit or "Any CPU" and running on a 32-bit OS.

    Here is a thread about how to troubleshooting this exception.

    Calling unmanaged 32bit DLL exception occuring only on one user's computer: BadImageFormatException (Exception from HRESULT: 0x8007000B)

    Troubleshooting BadImageFormatException

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Thursday, March 24, 2016 2:12 AM
    Thursday, March 24, 2016 2:10 AM
  • An AnyCPU exe with the prefer 32-bit flag can still be loaded into a 64-bit process (ie. a 64-bit process loads the exe as if it were a dll) and it can still be run by an ARM32 processor. Neither of these would be possible if it were compiled to target x86 rather than AnyCPU.

    • Proposed as answer by Kristin Xie Friday, April 1, 2016 9:58 AM
    Thursday, March 24, 2016 10:25 PM