I'm trying to use a third-party dll in a MinGW project; the dll came with a .lib and a .h file. Here is a boiled down example of what I'm trying to do:
#include <iostream>
using namespace std;
extern "C" {
long __declspec(dllimport) _stdcall C843_Connect(long iBoardNumber);
}
int main(int argc, char* argv[]) {
cout << "Attempting to connect..." << endl;
long ID = C843_Connect(1);
if (ID<0) {
cout << "Connection failed!" << endl;
return 1;
}
cout << "Connection succesful! ID: " << ID << endl;
return 0;
}
The function declaration is taken straight from the header file. The code compiles normally with MinGW, but the resulting program crashes every time when calling C843_Connect(long)
. However, the exact same code runs nicely when compiled with MS Visual Studio 2010. (I'm guessing the dll was built with Visual Studio, because it came with some VS sample projects.) Any ideas what might be going wrong and how I can get the dll to work with MinGW?
EDIT: I ran the program and a debugger, and the crash results from a call to a bad address. Here is the last four lines of disassembly, before the program crashes:
Address Hex dump Command
00401419 |. E8 B2300300 CALL 004344D0
0040141E |. C70424 010000 MOV DWORD PTR SS:[ESP],1
00401425 |. A1 68724700 MOV EAX,DWORD PTR DS:[477268]
0040142A |. FFD0 CALL EAX
The first call goes to somewhere in msvcrt.dll, the second call crashes the program. Can anyone make sense of this?