I have an issue using function from a DLL written in C#. The functions are exported using DLLExport using stdcall. Functions without variables work fine. However, a function with a variable corrupts memory and I can't figure out why.
The DLL code is
[DllExport("Test", CallingConvention = CallingConvention.StdCall)]
public static string Test()
{
string sMessage = "The quick brown fox jumps over the lazy dog.";
return (sMessage);
}
[DllExport("SayHello", CallingConvention = CallingConvention.StdCall)]
public static string SayHello([MarshalAs(UnmanagedType.LPWStr)] string pMessage)
{
return (pMessage);
}
[DllExport("Test", CallingConvention = CallingConvention.StdCall)]
public static string Test()
{
string sMessage = "The quick brown fox jumps over the lazy dog.";
return (sMessage);
}
[DllExport("SayHello", CallingConvention = CallingConvention.StdCall)]
public static string SayHello([MarshalAs(UnmanagedType.LPWStr)] string pMessage)
{
return (pMessage);
}
To copy to clipboard, switch view to plain text mode
My QT code is
int Testptr=0;
int SayHelloptr=0;
typedef int (__stdcall *MyPrototype0)();
MyPrototype0 myFunction0 =
(MyPrototype0
) QLibrary::resolve("NFX.Trading.Panel.Bridge.dll",
"Test");
if (myFunction0)
Testptr = myFunction0();
std::string TestStr = globalclass.mql4_ansi2unicode(Testptr);
static std::string sHello = "Hi There";
typedef long int (__stdcall *MyPrototype1)(std::string pMessage);
MyPrototype1 myFunction1 =
(MyPrototype1
) QLibrary::resolve("NFX.Trading.Panel.Bridge.dll",
"SayHello");
if (myFunction1)
SayHelloptr = myFunction1((std::string)sHello);
std::string SayHelloStr = globalclass.mql4_ansi2unicode(SayHelloptr);
int Testptr=0;
int SayHelloptr=0;
typedef int (__stdcall *MyPrototype0)();
MyPrototype0 myFunction0 =
(MyPrototype0) QLibrary::resolve("NFX.Trading.Panel.Bridge.dll", "Test");
if (myFunction0)
Testptr = myFunction0();
std::string TestStr = globalclass.mql4_ansi2unicode(Testptr);
static std::string sHello = "Hi There";
typedef long int (__stdcall *MyPrototype1)(std::string pMessage);
MyPrototype1 myFunction1 =
(MyPrototype1) QLibrary::resolve("NFX.Trading.Panel.Bridge.dll", "SayHello");
if (myFunction1)
SayHelloptr = myFunction1((std::string)sHello);
std::string SayHelloStr = globalclass.mql4_ansi2unicode(SayHelloptr);
To copy to clipboard, switch view to plain text mode
When I call Test(), it returns "The quick brown fox jumps over the lazy dog."
However, when I call SayHello(sHello), the returned string which should be "Hi There" is garbled.
Please note the globalclass.mql4_ansi2unicode simply converts the return string pointer into a local string and I know this function works due to the fact that Test() works and the correct string is returned.
Can anyone point me to my problem?
Thanks
Based on Qt 5.7.0 (MSVC 2013, 32 bit)
Bookmarks