Answer the question
In order to leave comments, you need to log in
How to implement string passing from C++ dll to .Net C# project?
There is a project in C #, a dll goes to it on the pluses. It is necessary to transfer a string from a function in a file to a sharp program.
Google used. Smoked foreign resources, but nothing helped.
As I understand it, there it is necessary to do marshaling on the Sharp side. So it didn’t work out for me, I didn’t understand what I needed and in what form to write on both sides (there were examples with CoTaskMemAlloc on the plus side (as I understand it, this is something related to COM), but I don’t know how to screw it up at all).
Tried a bunch of options. It turned out only on the plus side to overtake the enchantment into an array of longs and on the sharp side to get an int and overtake it into an enchantment and into a string, but these are fierce crutches.
How it is normal to implement it? Thanks in advance!
Answer the question
In order to leave comments, you need to log in
Filed an example
#include <stdio.h>
extern "C" __declspec(dllexport)
void __stdcall TestString(char str[]) {
char source[] = "Привет!";
sprintf_s(str, sizeof(source), source);
}
class Program {
[DllImport(@"Project1.dll", CharSet=CharSet.Ansi)]
public static extern void TestString(StringBuilder str);
static void Main(string[] args) {
var sb = new StringBuilder(4096);
TestString(sb);
Console.Write(sb.ToString());
Console.ReadLine();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question