PDA

View Full Version : Can't pass argument correctly from MSVC DLL to minGW App



radory
20th September 2010, 09:43
hi all,

i get truble when passing argument in a Foo function from MSVC DLL to a minGW application....

i tried the following code and get unexpected result:

/// MSVC DLL code

// XyzLibrary.h


#ifdef XYZLIBRARY_EXPORTS
#define XYZAPI __declspec(dllexport)
#else
#define XYZAPI __declspec(dllimport)
#endif

#ifdef __cplusplus
# define EXTERN_C extern "C"
# define XYZHANDLE IXyz*

struct IXyz
{
virtual int Foo(int n) = 0;
virtual void Release() = 0;
};

#endif

EXTERN_C XYZAPI int hzf (int);
EXTERN_C XYZAPI XYZHANDLE hzfXyz (VOID);




// XyzLibrary.cpp



#include "stdafx.h"
#include "stdio.h"
#include "XyzLibrary.h"


class XyzImpl : public IXyz
{
public:
int Foo(int n);
void Release();
};

int XyzImpl::Foo(int n)
{
printf("Foo called....%d\n",n);
return n * n;
}

void XyzImpl::Release()
{
delete this;
}


XYZHANDLE hzfXyz()
{
printf("\n\nhzfXyz() called ....\n");
return new XyzImpl;
}

int hzf(int n)
{
printf("\n\nhzf(int) called ....\n");
XyzImpl *xyz = new XyzImpl;
return xyz->Foo(n);
}



I compiled the code and get a XyzLibrary.dll



/// minGW code.....


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


int main(int ,char** )
{
IXyz *xxx = hzfXyz();
printf(" hzfXyz call: %d\n" , xxx->Foo(5));

printf(" hzf call: %d\n" , hzf(8));

return 0;
}


i compiled the code above with minGW in Qt creator2, got main.exe and then run it....


the consloe outputs the following result:

hzfXyz() called ....
Foo called....169091488 /// here the number should be 5 , but i got 169091488
hzfXyz call: -982866944 ///here the number should be 25, buit i got 169091488 * 169091488 ,flow over....

hzf(int)
Foo called....8
hzf call: 64 //// here hzf(int) called ,and everything goes well




i have attached the disassembler window, 5205

could somebody tell me why can't transfer correct value when i call xxx->Foo(5) from minGW App to MSVC DLL ??

I really hope this two compilers cooperate with each other little well.....








hzf(int)
Foo called....8
hzf call: 64

wysota
20th September 2010, 09:55
The two compilers have completely different name mangliing schemes. It is possible to use a library compiled with one for the other but it requires some other tool that will translate the imports to the dialect understood by the other compiler.

Can't you use the same compiler for both the library and the application?

radory
20th September 2010, 10:37
The two compilers have completely different name mangliing schemes. It is possible to use a library compiled with one for the other but it requires some other tool that will translate the imports to the dialect understood by the other compiler.

Can't you use the same compiler for both the library and the application?

thanks a lot for your reply, wysota, you are one of the best man here....
Sorry , because i need to include ws2bth.h header file and call bluetooth functions in ws2_32.lib,
i can't find this two file in mingw's include/lib folder, so i include them from window's SDK,
it only get compiled with MSVC, i tried to use minGW do the same thing , i got so many errors.....
i have no idea, although, i want to use minGW do all the things.....
could you please tell me some alternative ways to call bluetooth functions on windows with minGW??

Thank you very much.....

wysota
20th September 2010, 10:43
So why don't you compile your whole app with MSVC?

radory
20th September 2010, 11:25
yes ,this is a good option....i will consider it....
i just prefer Qt creator much then ms visual studio, i have the set up qt develop environment with minGW, and
don't want to set up msvc-qt again, msvc is a big thing , i don't like it, and don't like msvc's code style, it uses
lot of #define/typedef directives , it is hard to understand.....although there are plenty of people use msvc in
China....of course, most of them , are copies....
thanks you again, Mr wysota....

wysota
20th September 2010, 11:30
i just prefer Qt creator much then ms visual studio,
You can use Qt Creator with MSVC. It's the compiler that you are going to change, not the IDE.