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

Qt Code:
  1. #ifdef XYZLIBRARY_EXPORTS
  2. #define XYZAPI __declspec(dllexport)
  3. #else
  4. #define XYZAPI __declspec(dllimport)
  5. #endif
  6.  
  7. #ifdef __cplusplus
  8. # define EXTERN_C extern "C"
  9. # define XYZHANDLE IXyz*
  10.  
  11. struct IXyz
  12. {
  13. virtual int Foo(int n) = 0;
  14. virtual void Release() = 0;
  15. };
  16.  
  17. #endif
  18.  
  19. EXTERN_C XYZAPI int hzf (int);
  20. EXTERN_C XYZAPI XYZHANDLE hzfXyz (VOID);
To copy to clipboard, switch view to plain text mode 



// XyzLibrary.cpp

Qt Code:
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "XyzLibrary.h"
  4.  
  5.  
  6. class XyzImpl : public IXyz
  7. {
  8. public:
  9. int Foo(int n);
  10. void Release();
  11. };
  12.  
  13. int XyzImpl::Foo(int n)
  14. {
  15. printf("Foo called....%d\n",n);
  16. return n * n;
  17. }
  18.  
  19. void XyzImpl::Release()
  20. {
  21. delete this;
  22. }
  23.  
  24.  
  25. XYZHANDLE hzfXyz()
  26. {
  27. printf("\n\nhzfXyz() called ....\n");
  28. return new XyzImpl;
  29. }
  30.  
  31. int hzf(int n)
  32. {
  33. printf("\n\nhzf(int) called ....\n");
  34. XyzImpl *xyz = new XyzImpl;
  35. return xyz->Foo(n);
  36. }
To copy to clipboard, switch view to plain text mode 

I compiled the code and get a XyzLibrary.dll



/// minGW code.....

Qt Code:
  1. #include <stdio.h>
  2. #include "XyzLibrary.h"
  3.  
  4.  
  5. int main(int ,char** )
  6. {
  7. IXyz *xxx = hzfXyz();
  8. printf(" hzfXyz call: %d\n" , xxx->Foo(5));
  9.  
  10. printf(" hzf call: %d\n" , hzf(8));
  11.  
  12. return 0;
  13. }
To copy to clipboard, switch view to plain text mode 

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, ss.jpg

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