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. calling String ^ * from C#

calling String ^ * from C#

Scheduled Pinned Locked Moved Managed C++/CLI
c++questioncsharpcomhelp
8 Posts 3 Posters 40 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.
  • B Offline
    B Offline
    ByStorm Software
    wrote on last edited by
    #1

    I'm sure that this is a stupid question and that I am just missing some obvious but here it goes. I'm adding a remoting interface to an MFC service that I have. I've gotten it to compile and run with the CLI and now I'm porting the COM interface to a remoting interface. Then I hit a problem. I have some methods that return values as parameters..you know the old "BOOL *" and the ever popular "LPTSTR *"....anyway... in C++/CLI these get translated to Boolean * and String^ *. the problem is...HOW in the world do I call these from C#? I know it's a reference, but c++/cli doesn't support the keyword REF and c# doesn't like things like "&". I guess I could try to jump thru a lot of hoops and get it to work via unmanaged code, but this SEEMS like a simple syntax problem. sorry again to bother people with this, but for the life of me...I can't find the "right" keywords to get google to show me what I need. thanks, Gene

    L 1 Reply Last reply
    0
    • B ByStorm Software

      I'm sure that this is a stupid question and that I am just missing some obvious but here it goes. I'm adding a remoting interface to an MFC service that I have. I've gotten it to compile and run with the CLI and now I'm porting the COM interface to a remoting interface. Then I hit a problem. I have some methods that return values as parameters..you know the old "BOOL *" and the ever popular "LPTSTR *"....anyway... in C++/CLI these get translated to Boolean * and String^ *. the problem is...HOW in the world do I call these from C#? I know it's a reference, but c++/cli doesn't support the keyword REF and c# doesn't like things like "&". I guess I could try to jump thru a lot of hoops and get it to work via unmanaged code, but this SEEMS like a simple syntax problem. sorry again to bother people with this, but for the life of me...I can't find the "right" keywords to get google to show me what I need. thanks, Gene

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

      Do you want to call String from c# to MFC or to c++/cli If you chose c++/cli, yust add a project to your solution, and add a reference.

      B 1 Reply Last reply
      0
      • L Lost User

        Do you want to call String from c# to MFC or to c++/cli If you chose c++/cli, yust add a project to your solution, and add a reference.

        B Offline
        B Offline
        ByStorm Software
        wrote on last edited by
        #3

        What I'm trying to do is call the C++/cli from C#. The method I'm calling is trying to return an out parameter like String^ *. It would be a "ref" in c# and a & in normal C++. I did figure it out though. the magic symbol is %. it looks like % means ref in C++/CLI.

        L 1 Reply Last reply
        0
        • B ByStorm Software

          What I'm trying to do is call the C++/cli from C#. The method I'm calling is trying to return an out parameter like String^ *. It would be a "ref" in c# and a & in normal C++. I did figure it out though. the magic symbol is %. it looks like % means ref in C++/CLI.

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

          Let's say you have a class in C#. And both project are under your solution. Go to c++/cli project setting, and add a reference by projects, and select a project c#. When in c++/cli: using namespace projectCSharNamespace; projectCSharpClass ^cl = gcnew projectCSharpClass(); cl->String. But why do you want as String^ *, instead yust String^

          B 1 Reply Last reply
          0
          • L Lost User

            Let's say you have a class in C#. And both project are under your solution. Go to c++/cli project setting, and add a reference by projects, and select a project c#. When in c++/cli: using namespace projectCSharNamespace; projectCSharpClass ^cl = gcnew projectCSharpClass(); cl->String. But why do you want as String^ *, instead yust String^

            B Offline
            B Offline
            ByStorm Software
            wrote on last edited by
            #5

            the C++/CLI function will be returning a value in the string passed in. sort of like in c# void GetName(ref string nameStr) { nameStr = "sam"; } in c++ it would be void GetName(string^ * nameStr) { *nameStr = "sam"; } The problem happened when I tried to call c++ from c# since c# doesn't understand * unless it's unsafe.

            L 1 Reply Last reply
            0
            • B ByStorm Software

              the C++/CLI function will be returning a value in the string passed in. sort of like in c# void GetName(ref string nameStr) { nameStr = "sam"; } in c++ it would be void GetName(string^ * nameStr) { *nameStr = "sam"; } The problem happened when I tried to call c++ from c# since c# doesn't understand * unless it's unsafe.

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

              As i coud see with docmetation, (Nothing at all) And my test, i coud see that it isn't possible to do as reference. Why don't you use as return. If you wan't a multiple variables, you coud use struct. This coud be a bug, missing features or left out Intentionaly.

              B 1 Reply Last reply
              0
              • L Lost User

                As i coud see with docmetation, (Nothing at all) And my test, i coud see that it isn't possible to do as reference. Why don't you use as return. If you wan't a multiple variables, you coud use struct. This coud be a bug, missing features or left out Intentionaly.

                B Offline
                B Offline
                ByStorm Software
                wrote on last edited by
                #7

                I figured it out. if you want to pass variables around as reference in C++/CLI you have to mark them with a %. so instead of void GetName(String ^ * name) { *name = "sam"; } you do this: void GetName(String^% name) { name = "Sam"; } then you call it from c# like this: String nameStr; GetName(ref nameStr);

                R 1 Reply Last reply
                0
                • B ByStorm Software

                  I figured it out. if you want to pass variables around as reference in C++/CLI you have to mark them with a %. so instead of void GetName(String ^ * name) { *name = "sam"; } you do this: void GetName(String^% name) { name = "Sam"; } then you call it from c# like this: String nameStr; GetName(ref nameStr);

                  R Offline
                  R Offline
                  RockyMu
                  wrote on last edited by
                  #8

                  Thanks ByStorm. You saved my life. Been searching for this for weeks.

                  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