shingpui
July 13th, 2010, 09:28 AM
Hi,
I have a C++ dll that needs to send a string to C#. I know that C++ uses ASCII for storing characters and C# uses UNICODE instead. I like to know what would be the best approach to receive the string on the c# end and how should it be implemented. This is what i have now but i'm getting garbage on the c# end because of the different charcter coding.
I'm getting "Dąesult = 1.234" from the C# end while i send "My Result = 1.234" from c++ dll.
Thanks
////////////////////////////////////////
//c++
////////////////////////////////////////
//Test.h
extern "C" __declspec( dllexport ) const char* TestStringReturn();
#include "Test.h"
extern "C" __declspec( dllexport ) const char* TestStringReturn()
{
std::stringstream ReturnServiceResult;
ReturnServiceResult << "My Result = 1.234";
return ReturnServiceResult.str().c_str();
}
////////////////////////////////////////
//c#
////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
namespace Test
{
static class Program
{
[DllImport(@"D:\TestStringReturn.dll")]
static extern string TestStringReturn();
[StructLayout(LayoutKind.Sequential)]
struct Args
{
public string ip;
public string argparams;
}
static void Main()
{
string ReturnString;
ReturnString = TestStringReturn();
}
}
}
I have a C++ dll that needs to send a string to C#. I know that C++ uses ASCII for storing characters and C# uses UNICODE instead. I like to know what would be the best approach to receive the string on the c# end and how should it be implemented. This is what i have now but i'm getting garbage on the c# end because of the different charcter coding.
I'm getting "Dąesult = 1.234" from the C# end while i send "My Result = 1.234" from c++ dll.
Thanks
////////////////////////////////////////
//c++
////////////////////////////////////////
//Test.h
extern "C" __declspec( dllexport ) const char* TestStringReturn();
#include "Test.h"
extern "C" __declspec( dllexport ) const char* TestStringReturn()
{
std::stringstream ReturnServiceResult;
ReturnServiceResult << "My Result = 1.234";
return ReturnServiceResult.str().c_str();
}
////////////////////////////////////////
//c#
////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
namespace Test
{
static class Program
{
[DllImport(@"D:\TestStringReturn.dll")]
static extern string TestStringReturn();
[StructLayout(LayoutKind.Sequential)]
struct Args
{
public string ip;
public string argparams;
}
static void Main()
{
string ReturnString;
ReturnString = TestStringReturn();
}
}
}