-
January 26th, 2007, 07:06 AM
#1
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
-
January 26th, 2007, 09:24 AM
#2
Re: BadImageFormatException
Fancy posting some more code?
matt
-
January 26th, 2007, 09:43 AM
#3
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.
-
January 26th, 2007, 10:01 AM
#4
Re: BadImageFormatException
-
January 26th, 2007, 10:02 AM
#5
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:
instead:
Code:
int c;
c=a+b;
return c;
Sorry :/
-
January 26th, 2007, 10:10 AM
#6
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?
-
January 26th, 2007, 11:38 AM
#7
Re: BadImageFormatException
-
March 19th, 2011, 10:31 AM
#8
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.
-
March 21st, 2011, 12:46 PM
#9
Re: BadImageFormatException
Originally Posted by jobin1983
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|