Pass string data to C# app
-
I have searched all over the web and tried lots and lots of suggestions/solutions, but I can't get this to work.
I am building a library (dll) in Qt (Windows) to be consumed by a C# .NET application (not my app, someone else's). I need to pass string parameters back to the calling C# app (parameter - not return type, which is boolean). In most tests I use char * on the Qt side, though I have tried a few others.
I have tried all the P/Invoke, Marshalling, IntPtr, StringBuilder, etc. suggestions on the C# side and none of them work. I have, though, been able to successfully pass a string when the function returns a string, by setting the return type in C# to an IntPtr and marshalling that to a string. But trying to pass the string as one (or more) of the parameters does not work.
What is the magic to make this work?
-
Maybe this on so helps.
-
@Christian-Ehrlicher
This is one of the resources I already found, but it didn't help. -
@Jay-Madren said in Pass string data to C# app:
This is one of the resources I already found, but it didn't help.
And did you also already try this?
==> https://stackoverflow.com/a/54563197/13933646 -
You can use these 2 methods to convert between
QString
andString^
(tested working):QString String2QString(String^ a) { QString Result; for (int i = 0; i<a->Length; i++) Result += (*a)[i]; return Result; } String^ QString2String(const QString& a) { StringBuilder^ Result = gcnew StringBuilder(); Char Tmp; for (int i = 0; i<a.size(); i++) { Tmp = a.at(i).unicode(); Result->Append(Tmp); } return Result->ToString(); }
Now assigning to a parameter or a return type is trivial.
P.S.
Pretty sure those methods can be optimised for performance, I just copy-pasted them from a legacy project I hadEdit:
Forgot to mentionString^
comes from C++/CLI (/clr
compiler option)