Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pass string data to C# app

Pass string data to C# app

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 432 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay Madren
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Maybe this on so helps.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      J 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Maybe this on so helps.

        J Offline
        J Offline
        Jay Madren
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        This is one of the resources I already found, but it didn't help.

        KroMignonK 1 Reply Last reply
        0
        • J Jay Madren

          @Christian-Ehrlicher
          This is one of the resources I already found, but it didn't help.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @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

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            You can use these 2 methods to convert between QString and String^ (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 had

            Edit:
            Forgot to mention String^ comes from C++/CLI (/clr compiler option)

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved