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);
#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);
To copy to clipboard, switch view to plain text mode
// 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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
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;
}
#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;
}
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
Bookmarks