none
an attempt was made to load a program with an incorrect format exception from hresult 0x8007000B RRS feed

  • Question

  • I'm getting the error as described in the title.  After some searching I realize that a 32 bit dll is trying to be loaded by a 64 bit program (or vice versa).  What I don't understand is why.  I am running VS Pro 2008 on a 64 bit machine.  My solution contains several C# projects, and one C++ project (this one creates the dll) that one of the C# projects calls.  I've tried making all of my C# projects target Any CPU, x86, and x64 and I get the same error everytime with all of them.  What I don't understand is why if i make everything have the same target (lets says x86), why I would still get this error.  My guess is there's something I'm not setting correctly in the DLL.  Any help would be greatly appreciated
    • Moved by lucy-liuModerator Thursday, December 23, 2010 8:12 AM it is not related to vc,it is about c# (From:Visual C++ Language)
    Tuesday, December 21, 2010 5:40 PM

Answers

  • Hi Sean.Whipple,

     

    I have made a sample code for researching this issue in my side. And you can download it from the following link.

    (note: correct the dll path to your path if you test my sample code in your side)

    http://cid-bb789f72272d4858.photos.live.com/self.aspx/2011/2011Y01M/CPPDllTest.zip

     

    When I create this solution in vs2008, I correct the Platform target to x86, then it runs fine in debug model.

     

    When I change the solution to release model, and correct the Platform target to x86, then it runs fine in this model, too.

     

    Change the  Platform target:

    Right click on the C# project -> Properties -> Build

     

    If there's any concern, please feel free to let me know.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 25, 2011 10:38 AM
    Moderator

All replies

  •  What I don't understand is why if i make everything have the same target (lets says x86), why I would still get this error. 
    You shouldn't. That's exactly the right thing to do. Look again. I suspect there is another C# assembly somewhere still marked as "Any CPU". Be sure to do a full rebuild.
    Tuesday, December 21, 2010 5:43 PM
  • So I went back and double checked all the C# projects.  All of them say platform target x86 (I triple checked).  So to me that suggests that my DLL is being compiled to x64.  But when I look at the project properties for that, I see a Platform for Win32 and the target Machine under the advanced linker settings is MachineX86.  Yet I still get the error.  Are there are any other references that come to mind that I might be missing?    Note for the C# projects I've only changed the platform Target to x86 under the Build tab, is there something else I'm supposed to change for them?
    Tuesday, December 21, 2010 6:06 PM
  • Hi Sean,

    I am moving this thread from “Visual C++ Language" forum to the “Visual C# General” forum.,
    since the issue is related to Visual C# .There are more experts in the “Visual C# General" forum.

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 23, 2010 8:11 AM
    Moderator
  •  

    Hi Sean.Whipple,

     

    Can you provide the code, especially the code related to how you use the C++ dll file in your C# project.

     

    1. Ensure the C# project's CPU and the target platform is X86
    1. Ensure the code you're using to import the C++ dll in C# is correct

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 28, 2010 9:52 AM
    Moderator
  •  

    Hi  Sean.Whipple,

     

    There's no update days ago, so I would like to know if this question has been solved.

     

    If there's any concern, please feel free to let me know.

     

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 30, 2010 1:09 PM
    Moderator
  • Unfortunately I cannot provide specific code as it is proprietary (I apologize I know this makes helping solve a problem extremely more difficult).  I am still having the problem however.

    I have ensure all C# projects are targeting x86 machines.  I'm fairly confident in the import code as well because I don't have this problem in visual studio 2005 professional.  And in 2008 I only have this problem in release mode, in debug it works fine
    Tuesday, January 11, 2011 4:02 PM
  •  

    Hi  Sean.Whipple,

     

    Can you show us the test environments information?

     

    Such as the system version, 32bit or 64bit, and cpu information, the IDE version and the target platform information. I would like to see every test environments' detailed information. It may help us to research this issue.

     

    If there's any concern, please feel free to let us know.

     

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 14, 2011 7:34 AM
    Moderator
  • OS: Windows XP Pro x64 Edition Ver 2003 Service Pack 2

    Computer Equip: AMD Opteron(tm) Proecessor 254 2.79 GHz, 3.25 GB of RAM

    IDE (from the "About page"): Microsoft Visual Studio 2008 Version 9.0.30729.1 SP Copyright 2007

         *NOTE: Also says Microsoft .NET Framework Version 3.5 SP1

    C# Projects (all are identical):

    Targeting a .NET Framework 2.0

    Currently set to target Any CPU (however I've tried specifying all of them to target x64 and x86)

    NOTE: All but one of the C# projects is creating a DLL, the last is the main project which references those DLL's, as far as I can tell these links seem to be working just fine

    C++ Project:

    Creating a .dll targeting a Win32 Platform (that's the only option I have in Visual Studio 2008...not sure why), Using standard windows librarying and not using ATL.  Calling convention is __cdecl.  This project is creating unmanaged C++ code that exports functions from the dll as follows:

    extern "C" _declspec(dllexport) double return2();
    

    The C# project imports these functions via the following:

    [DllImport(dllPath)]
    public static extern double return2();
    
    // I have also tried this
    [DllImport(dllPath, CallingConvention = CallingConvention.Cdecl)]
    public static extern double return2();
    

     

    A quick final thought (which totally perplexes me), is that this works perfectly in Visual Studio 2005 in both release and debug mode.  However when ported to Visual Studio 2008 only debug mode works.  When attempted to run in release mode the bad image format exception is thrown with the code : 0x8007000B

     

    One thing of note that I do do as well is I pass callback function delegates from the C# code to the C++ code through this interface as the following in C#:

    //I've also tried this with CallingConvention.Cdecl
    [UnmanagedFunctionPointerAttribute(CallingConvention.StdCall)]
    public delegate void callBackFunc();
    
    [DllImport(dllPath)]
    public static extern void PassCallBack( callBackFunc theCallBack);
    

    In C++ it's received as:

    typedef void (__stdcall * callBackFunc)();
    
    extern "C" _declspec(dllexport) void PassCallBack(callBackFunc theCallBack);
    
    Is this the proper method of a passing function delegates from managed C# to unamanged C++?

     

    I understand that this isn't a terrible amount of information to help solve the problem but unfortunately that's all I really know (I'm not much of a computer scientist).  Any help is greatly appreciated.  If there's anything unclear about this post (I know it's rather lengthy) just ask and I'll try to clarify as best I can

    Thursday, January 20, 2011 3:13 PM
  • Hi Sean.Whipple,

     

    I have made a sample code for researching this issue in my side. And you can download it from the following link.

    (note: correct the dll path to your path if you test my sample code in your side)

    http://cid-bb789f72272d4858.photos.live.com/self.aspx/2011/2011Y01M/CPPDllTest.zip

     

    When I create this solution in vs2008, I correct the Platform target to x86, then it runs fine in debug model.

     

    When I change the solution to release model, and correct the Platform target to x86, then it runs fine in this model, too.

     

    Change the  Platform target:

    Right click on the C# project -> Properties -> Build

     

    If there's any concern, please feel free to let me know.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 25, 2011 10:38 AM
    Moderator
  • Did you find a solution to this problem?  I have the same problem, but it only seems to manifest itself on an AMD x86/64 running 64 bit windows 7.  No matter what settings I try, the program loads 64 bit dlls when it should be loading 32 bit dlls.

    Tuesday, November 8, 2011 11:09 PM