Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a dll written in C++ that I want to use in C# (a common problem).
I am trying to make a test to understand how to use the dll.
So I created a new standard dll in C++ with these simple lines:

C#
extern "C" __declspec (dllexport) short __stdcall TestFunction(short i){
    return i+123;
}


Then I am trying to use this test function in an C# application using the code:

C#
[DllImport("Dll.dll")]
static extern short TestFunction(short i);


and then

C#
short x=3;
x=TestFunction(x);


Compiled!
But I got this error runtime:

An unhandled exception of type 'System.BadImageFormatException' occurred

I think the problem is that the dll is not managed, but I have to use it as standard C++ and unmanaged. Right?
Anyone knows hot to reach my target?
Thank you for any help
Posted

1 solution

This looks like you are running your .NET application on 64bit OS and your DLL is 32bits.
If this is the issue you can fix it by targetting your .NET application to x86 in project settings.
 
Share this answer
 
Comments
Russell' 28-Sep-11 9:40am    
I love you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900