Reputation: 6082
I am sending a series of char arrays
from C++ to my C# program.
The c++ functions look something like this:
ReturnChar.cpp
extern "C" RETURNCHAR_API char* testString()
{
return test;
}
ReturnChar.h
extern "C" RETURNCHAR_API char* __cdecl testString();
ReturnChar Import C#
public static class ImportTest
{
[DllImport("ReturnChar.dll", EntryPoint = "testString", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern char* testString();
}
public partial class MainWindow : Window
{
public unsafe MainWindow()
{
InitializeComponent();
try
{
StringBuilder sb = new StringBuilder(new string(ImportTest.testString()));
textBox1.Text = sb.ToString();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
I get the result actually, but looks like there is a problem of encoding between C++ and C#. How can I resolve this?
I don't intend to use wchar_t
in my C++ program, because I have the operations being done on char arrays. Can I somehow encode this in C#?
Upvotes: 0
Views: 173
Reputation: 29539
You should be using wchar_t
throughout in your C++ program, and perform your operations on wchar_t
arrays instead.
That said, you need to add CharSet = CharSet.Ansi
to the DllImport
parameters.
Upvotes: 1