CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2006
    Posts
    128

    BadImageFormatException

    HI All,
    I am trying to call a c++ dll from a c# application. But i get BadImageFormatException.
    The example i tried is one from this website
    www.adp-gmbh.ch/win/misc/mingw/dll.html

    Also i tried to create a simple dll in c++ , adding two integers and calling that in the c# ...no good - same error message..
    This is my second day of learning c#, so not sure where to go from.
    Any clue?

    Thanks
    Rach

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: BadImageFormatException

    Fancy posting some more code?
    matt
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Oct 2006
    Posts
    128

    Re: BadImageFormatException

    this is the example i used.... I am using visualstudio 2005 ...created the dll using win32 dll wizard.

    <code>
    mydll.h

    int AddInt(int a , int b);




    mydll.cpp

    #include <stdio.h>
    #include "mydll.h"

    int AddInt(int a, int b){
    int c;
    c=a+b;
    return c;
    }


    c# file

    using System.Runtime.InteropServices;
    using System;

    class callDll{

    [DllImport("<fullpathname>mydll.dll")]
    private static extern int AddInt(int a, int b);

    public static void Main() {
    int aa = 100;
    int bb= 150;
    int cc;
    cc=AddInt(aa, bb);
    Console.WriteLine("Value returned is "+cc);
    }
    }


    </code>

    This is a simple program, though i am not doing any useful in here, i want to understand the concept of calling c++ dll from c# as i have a bigger project to work on.
    All the files compile fine... no errors. Only at runtime, when to comes to call cc=AddInt(aa,bb) it errors with BadImageFormatException.

    Thanks for your reply.

  4. #4
    Join Date
    Jan 2007
    Posts
    491

    Re: BadImageFormatException

    Sorry, double posting...

  5. #5
    Join Date
    Jan 2007
    Posts
    491

    Re: BadImageFormatException

    Next time use those: [CODE [/CODE (with ] in the end of the [/CODE & [CODE)...

    emm.. I don't know what's the problem, but you can do this:
    Code:
    return a+b;
    instead:
    Code:
    int c;
    c=a+b;
    return c;
    Sorry :/

  6. #6
    Join Date
    Oct 2006
    Posts
    128

    Re: BadImageFormatException

    Thanks for you reply..
    i don't think the problem is with the way the value is returned.

    Have i done everything right in the c# program? Why is the dll in badimageformat.....? any clues?

  7. #7
    Join Date
    Oct 2006
    Posts
    128

    Unhappy Re: BadImageFormatException

    Any suggestion anyone?

  8. #8
    Join Date
    Mar 2011
    Posts
    2

    Re: BadImageFormatException

    Rachael,

    i dont think you can call the c++ code directly from c#. You need to create a CLR project to interface with the C++ project. Then you need to call the CLR project from C#. The CLR project will call the C++ code.

  9. #9
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: BadImageFormatException

    Quote Originally Posted by jobin1983 View Post
    Rachael,

    i dont think you can call the c++ code directly from c#. You need to create a CLR project to interface with the C++ project. Then you need to call the CLR project from C#. The CLR project will call the C++ code.
    this is not about thinking but about knowing... if you don't know don't post such nonsense

    sure you can call native code from C#.

    now to OP: I have still a similar problem http://www.codeguru.com/forum/showth...ormatException
    though the post is quite old the same error comes. what's more, it happens to my own wrapper that I cannot use in C# because of this stupid error. there are no clues how to solve it.

    I don't think anybody will be able to help us. this must be a very very very rare issue and no one ever has seen it
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured