Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Passing a C# string to C++/CLI does not show up from a CPP program

Passing a C# string to C++/CLI does not show up from a CPP program

Scheduled Pinned Locked Moved Managed C++/CLI
c++helpcsharpcom
13 Posts 4 Posters 2 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    SujayG
    wrote on last edited by
    #1

    Hi There , I want to call a C# function from C++ , via CLI/C++. C# code

    private string _text = "";

    public void setText(string text)
    {
    // _text = text;
    _text = "HI World";
    Console.WriteLine("C# settext - {0}", _text);
    }

    public string getText()
    {
    return _text;
    }

    C++/CLI code Header :

    //virtual void setText(System::String^ text);
    //virtual System::String^ getText();

    virtual void setText(std::string text);
    virtual std::string getText();

    CPP file

    std::string CStringBridge::getText()
    {
    _managedObject = gcnew Bridge();
    return (marshal_asstd::string(_managedObject->getText()));
    }

    void CStringBridge::setText(std::string text)
    {
    _managedObject = gcnew Bridge();
    _managedObject->setText(gcnew System::String(text.c_str()));
    }

    Note : When I use the following code

    virtual void setText(System::String^ text);
    virtual System::String^ getText();

    I get the following error 3395

    __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention

    , and so I stuck to std::string When I use the library from the C++/CLI code, and call from my C++ program, "Hi World" should be printed ; instead nothing gets printed C++ console application

    IStringBridgeWrapper *pBridge = IStringBridgeWrapper::CreateInstance();
    pBridge->setText(std::string("I am here"));

    try{

    pBridge->getText();
    

    }
    catch(exception& e)
    {
    cout << e.what() << endl;
    }

    I think the string is not being properly passed . Any ideas to solve it shall be appreicated.

    My Blog

    D L J 3 Replies Last reply
    0
    • S SujayG

      Hi There , I want to call a C# function from C++ , via CLI/C++. C# code

      private string _text = "";

      public void setText(string text)
      {
      // _text = text;
      _text = "HI World";
      Console.WriteLine("C# settext - {0}", _text);
      }

      public string getText()
      {
      return _text;
      }

      C++/CLI code Header :

      //virtual void setText(System::String^ text);
      //virtual System::String^ getText();

      virtual void setText(std::string text);
      virtual std::string getText();

      CPP file

      std::string CStringBridge::getText()
      {
      _managedObject = gcnew Bridge();
      return (marshal_asstd::string(_managedObject->getText()));
      }

      void CStringBridge::setText(std::string text)
      {
      _managedObject = gcnew Bridge();
      _managedObject->setText(gcnew System::String(text.c_str()));
      }

      Note : When I use the following code

      virtual void setText(System::String^ text);
      virtual System::String^ getText();

      I get the following error 3395

      __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention

      , and so I stuck to std::string When I use the library from the C++/CLI code, and call from my C++ program, "Hi World" should be printed ; instead nothing gets printed C++ console application

      IStringBridgeWrapper *pBridge = IStringBridgeWrapper::CreateInstance();
      pBridge->setText(std::string("I am here"));

      try{

      pBridge->getText();
      

      }
      catch(exception& e)
      {
      cout << e.what() << endl;
      }

      I think the string is not being properly passed . Any ideas to solve it shall be appreicated.

      My Blog

      D Offline
      D Offline
      David Knechtges
      wrote on last edited by
      #2

      This is how I convert a managed (c#) string to an unmanaged string (C++):

      using namespace msclr::interop;
      void GetUnmanagedString(String ^ managedString, char **unmanagedString)
      {
      marshal_context ^ context = gcnew marshal_context();
      strcpy(*unmanagedString, context->marshal_as(managedString));
      delete context;
      }

      Note that the unmanaged string memory has already been allocated before I call this function.

      S 1 Reply Last reply
      0
      • D David Knechtges

        This is how I convert a managed (c#) string to an unmanaged string (C++):

        using namespace msclr::interop;
        void GetUnmanagedString(String ^ managedString, char **unmanagedString)
        {
        marshal_context ^ context = gcnew marshal_context();
        strcpy(*unmanagedString, context->marshal_as(managedString));
        delete context;
        }

        Note that the unmanaged string memory has already been allocated before I call this function.

        S Offline
        S Offline
        SujayG
        wrote on last edited by
        #3

        That's just the conversion part . I don't think that's the reason for the string not getting displayed.

        My Blog

        1 Reply Last reply
        0
        • S SujayG

          Hi There , I want to call a C# function from C++ , via CLI/C++. C# code

          private string _text = "";

          public void setText(string text)
          {
          // _text = text;
          _text = "HI World";
          Console.WriteLine("C# settext - {0}", _text);
          }

          public string getText()
          {
          return _text;
          }

          C++/CLI code Header :

          //virtual void setText(System::String^ text);
          //virtual System::String^ getText();

          virtual void setText(std::string text);
          virtual std::string getText();

          CPP file

          std::string CStringBridge::getText()
          {
          _managedObject = gcnew Bridge();
          return (marshal_asstd::string(_managedObject->getText()));
          }

          void CStringBridge::setText(std::string text)
          {
          _managedObject = gcnew Bridge();
          _managedObject->setText(gcnew System::String(text.c_str()));
          }

          Note : When I use the following code

          virtual void setText(System::String^ text);
          virtual System::String^ getText();

          I get the following error 3395

          __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention

          , and so I stuck to std::string When I use the library from the C++/CLI code, and call from my C++ program, "Hi World" should be printed ; instead nothing gets printed C++ console application

          IStringBridgeWrapper *pBridge = IStringBridgeWrapper::CreateInstance();
          pBridge->setText(std::string("I am here"));

          try{

          pBridge->getText();
          

          }
          catch(exception& e)
          {
          cout << e.what() << endl;
          }

          I think the string is not being properly passed . Any ideas to solve it shall be appreicated.

          My Blog

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          sujayg wrote:

          I get the following error 3395

          __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention

          Why are you trying to use dllexport in a CLI program? Make everything CLI and it should work together.

          Use the best guess

          S 1 Reply Last reply
          0
          • L Lost User

            sujayg wrote:

            I get the following error 3395

            __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention

            Why are you trying to use dllexport in a CLI program? Make everything CLI and it should work together.

            Use the best guess

            S Offline
            S Offline
            SujayG
            wrote on last edited by
            #5

            Hi , I was following the example at Using managed code in an unmanaged application[^] It seeemed strange that the function signature with String^ throws the error, but std::string does not.

            My Blog

            L 1 Reply Last reply
            0
            • S SujayG

              Hi , I was following the example at Using managed code in an unmanaged application[^] It seeemed strange that the function signature with String^ throws the error, but std::string does not.

              My Blog

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              That has nothing to do with it; as the message says, you cannot use dllexport on a CLI method. You are trying to mix managed and unmanaged code, which does not work well.

              Use the best guess

              S 1 Reply Last reply
              0
              • L Lost User

                That has nothing to do with it; as the message says, you cannot use dllexport on a CLI method. You are trying to mix managed and unmanaged code, which does not work well.

                Use the best guess

                S Offline
                S Offline
                SujayG
                wrote on last edited by
                #7

                What if I dont use String^ and use std::string as the preferred parameter .

                My Blog

                L 1 Reply Last reply
                0
                • S SujayG

                  What if I dont use String^ and use std::string as the preferred parameter .

                  My Blog

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You'll get in even more difficulty. You need to spend some time learning the difference between managed and unmanaged code in C++. I am not sure what you are trying to achieve here, but suffice it to say that what you are attempting is not straightforward. C++ was never designed to work in .NET and unless you have a compelling reason to mix the two languages you should not bother. Stick to either unmanaged C++ or C#, and make life easier for yourself.

                  Use the best guess

                  S 1 Reply Last reply
                  0
                  • L Lost User

                    You'll get in even more difficulty. You need to spend some time learning the difference between managed and unmanaged code in C++. I am not sure what you are trying to achieve here, but suffice it to say that what you are attempting is not straightforward. C++ was never designed to work in .NET and unless you have a compelling reason to mix the two languages you should not bother. Stick to either unmanaged C++ or C#, and make life easier for yourself.

                    Use the best guess

                    S Offline
                    S Offline
                    SujayG
                    wrote on last edited by
                    #9

                    I want to call a C# function from C++ ; and I am using C++?CLI as the bridge . I had developed a CLI wrapper - for calling C++ function from C# ,but this was my attempt to do the reverse.

                    My Blog

                    L 1 Reply Last reply
                    0
                    • S SujayG

                      I want to call a C# function from C++ ; and I am using C++?CLI as the bridge . I had developed a CLI wrapper - for calling C++ function from C# ,but this was my attempt to do the reverse.

                      My Blog

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      See this white paper[^].

                      Use the best guess

                      S 1 Reply Last reply
                      0
                      • L Lost User

                        See this white paper[^].

                        Use the best guess

                        S Offline
                        S Offline
                        SujayG
                        wrote on last edited by
                        #11

                        Thanks for the link. I have created the manged DLL , without errors. I want to call this DLL from native C++ applicaton . Please help me with a step by step process how can I do it .

                        My Blog

                        L 1 Reply Last reply
                        0
                        • S SujayG

                          Thanks for the link. I have created the manged DLL , without errors. I want to call this DLL from native C++ applicaton . Please help me with a step by step process how can I do it .

                          My Blog

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          sujayg wrote:

                          Please help me with a step by step process how can I do it .

                          Sorry, I don't have one, you will have to figure it out from the documentation and articles on the subject. I still do not understand what you are trying to achieve here, calling managed code from unmanaged is not such a simple matter.

                          Use the best guess

                          1 Reply Last reply
                          0
                          • S SujayG

                            Hi There , I want to call a C# function from C++ , via CLI/C++. C# code

                            private string _text = "";

                            public void setText(string text)
                            {
                            // _text = text;
                            _text = "HI World";
                            Console.WriteLine("C# settext - {0}", _text);
                            }

                            public string getText()
                            {
                            return _text;
                            }

                            C++/CLI code Header :

                            //virtual void setText(System::String^ text);
                            //virtual System::String^ getText();

                            virtual void setText(std::string text);
                            virtual std::string getText();

                            CPP file

                            std::string CStringBridge::getText()
                            {
                            _managedObject = gcnew Bridge();
                            return (marshal_asstd::string(_managedObject->getText()));
                            }

                            void CStringBridge::setText(std::string text)
                            {
                            _managedObject = gcnew Bridge();
                            _managedObject->setText(gcnew System::String(text.c_str()));
                            }

                            Note : When I use the following code

                            virtual void setText(System::String^ text);
                            virtual System::String^ getText();

                            I get the following error 3395

                            __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention

                            , and so I stuck to std::string When I use the library from the C++/CLI code, and call from my C++ program, "Hi World" should be printed ; instead nothing gets printed C++ console application

                            IStringBridgeWrapper *pBridge = IStringBridgeWrapper::CreateInstance();
                            pBridge->setText(std::string("I am here"));

                            try{

                            pBridge->getText();
                            

                            }
                            catch(exception& e)
                            {
                            cout << e.what() << endl;
                            }

                            I think the string is not being properly passed . Any ideas to solve it shall be appreicated.

                            My Blog

                            J Offline
                            J Offline
                            jschell
                            wrote on last edited by
                            #13

                            It isn't clear if there is a real business problem here but mixed mode programming can often be tricky. So it is easier to just not do it. Instead 1. Create a executable of the targeted functionality. 2. Provide an management API for that: sockets, files or std io. 3. Use the client application to manage a "process" to run the above executable. 4. The client application uses the management api to produce the desired functionality. The advantage to this is that both pieces, with care, can be tested independently and debugged independently as well. And the target functionality cannot take done the client application should it fail in a catastrophic way.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups