简体   繁体   中英

Am I calling this C++ DLL correctly?

I need to call a C++ DLL function from C#, the C++ function definition is

DWORD Init(char*[],int,int,int);

and I'm calling it in C# like so

[DllImport("DLLTest.dll")]
unsafe public static extern int Init(string[] detailsarray, int amount, int ID1, int ID2);

//then in main...

string[] details = new string[3];
details[0] = "blahblah";
details[1] = "bloobloo";
details[2] = "1234";

int result = Init(details, 1, 16, 170);

When I run it I get an unhandled exception of type System.BadImageFormatException. I believe It's ok to pass through strings when the C++ DLL is expecting char*s, and I also think its ok to use int in C# in place of DWORD in C++, but if I'm in correct please let me know.

Thanks.

EDIT - I should add, I've tested the DLL in a C++ app and it works fine.

A BadImageFormatException often occurs if you try to load a 32bit DLL into a 64bit process or vice versa. If DLLTest.dll is a 32bit DLL, you'll need to compile your C# DLL for the x86 platform, not Any CPU

http://msdn.microsoft.com/en-us/library/zekwfyz4(v=vs.100).aspx

http://davidstechtips.com/wp-content/uploads/2010/06/VisualStudioPlatformTargetx86.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM