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. Marshal native string to managed string for wrapper DLL

Marshal native string to managed string for wrapper DLL

Scheduled Pinned Locked Moved Managed C++/CLI
c++question
19 Posts 4 Posters 0 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.
  • A alleyes 0

    I have the following native function call:

    wchar_t* SXNative::ReturnString()
    {
    wchar_t retString[128];
    wchar_t Test[] = L"This is a dummy";

    wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);

    return retString;
    }

    The return value works fine from the native function. In a C++/CLI wrapper class I have a managed type that marshalls the return type:

    String^ ManagedWrapper::Managed_ReturnString()
    {
    wchar_t* test = nat_ptr->ReturnString();
    return gcnew String(test);
    }

    The return type from the manaaged function returns garbage. Any idea what is wrong with this? Thanks in advance.

    Jer 29:11

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    Al_S wrote:

    wchar_t* SXNative::ReturnString() { wchar_t retString[128]; return retString; }

    IMO this isn't valid C or C++ code, it returns a pointer to a stack-based array, which is going to diappear in thin air as soon as the function returns. It may look like a good pointer on immediate inspection, but it will not work reliably. :)

    Luc Pattyn


    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


    Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


    A 1 Reply Last reply
    0
    • L Luc Pattyn

      Al_S wrote:

      wchar_t* SXNative::ReturnString() { wchar_t retString[128]; return retString; }

      IMO this isn't valid C or C++ code, it returns a pointer to a stack-based array, which is going to diappear in thin air as soon as the function returns. It may look like a good pointer on immediate inspection, but it will not work reliably. :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


      A Offline
      A Offline
      alleyes 0
      wrote on last edited by
      #3

      OK. It goes out of scope after it returns. How do I mend that?

      Jer 29:11

      modified on Tuesday, October 20, 2009 9:18 PM

      L 1 Reply Last reply
      0
      • A alleyes 0

        OK. It goes out of scope after it returns. How do I mend that?

        Jer 29:11

        modified on Tuesday, October 20, 2009 9:18 PM

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #4

        by making sure it does not sit on the stack (the alternatives being static allocation and heap allocation). In C, it would be a global array, or a malloc'ed buffer. in C++ it could also be (a member of) an object, anything that gets gcnew'ed or is static. And for anything dynamic, you would eventually have to deallocate it (e.g. call free on the thing that was malloc'ed). :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


        A 1 Reply Last reply
        0
        • L Luc Pattyn

          by making sure it does not sit on the stack (the alternatives being static allocation and heap allocation). In C, it would be a global array, or a malloc'ed buffer. in C++ it could also be (a member of) an object, anything that gets gcnew'ed or is static. And for anything dynamic, you would eventually have to deallocate it (e.g. call free on the thing that was malloc'ed). :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


          A Offline
          A Offline
          alleyes 0
          wrote on last edited by
          #5

          Can you suggest something? I've struggled with mixed mode a bit

          Jer 29:11

          L 1 Reply Last reply
          0
          • A alleyes 0

            Can you suggest something? I've struggled with mixed mode a bit

            Jer 29:11

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            I don't know what it is you want to achieve. I guess this would be the simplest C++ code that should do it:

            wchar_t* SXNative::ReturnString() {
            return L"This is a dummy";
            }

            as now the string sits statically in memory. If you need more C++ advice, better someone else kicks in. I answered the OP because it started as a P/Invoke issue, which it wasn't after all. :)

            Luc Pattyn


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


            A 1 Reply Last reply
            0
            • L Luc Pattyn

              I don't know what it is you want to achieve. I guess this would be the simplest C++ code that should do it:

              wchar_t* SXNative::ReturnString() {
              return L"This is a dummy";
              }

              as now the string sits statically in memory. If you need more C++ advice, better someone else kicks in. I answered the OP because it started as a P/Invoke issue, which it wasn't after all. :)

              Luc Pattyn


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


              A Offline
              A Offline
              alleyes 0
              wrote on last edited by
              #7

              Thanks. Not sure where the post was intended as P/Invoke. I was sure I was pursuing an interop question. What you posted is a portion of what I had initially. The native code worked as posted. It just goes out of scope when called my the managed function. As stated in the OP, I want to return a native string to a managed function that marshalls it to a System::String. Illustrated in the top of the thread :confused:

              Jer 29:11

              L M 2 Replies Last reply
              0
              • A alleyes 0

                Thanks. Not sure where the post was intended as P/Invoke. I was sure I was pursuing an interop question. What you posted is a portion of what I had initially. The native code worked as posted. It just goes out of scope when called my the managed function. As stated in the OP, I want to return a native string to a managed function that marshalls it to a System::String. Illustrated in the top of the thread :confused:

                Jer 29:11

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                "Marshal" in the subject line got my attention... :)

                Luc Pattyn


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


                A 1 Reply Last reply
                0
                • L Luc Pattyn

                  "Marshal" in the subject line got my attention... :)

                  Luc Pattyn


                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                  Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


                  A Offline
                  A Offline
                  Andreoli Carlo
                  wrote on last edited by
                  #9

                  Look at his thread ... maybe it can help, you its going from System::String (utf8 charset) do std::string link

                  A 1 Reply Last reply
                  0
                  • A alleyes 0

                    Thanks. Not sure where the post was intended as P/Invoke. I was sure I was pursuing an interop question. What you posted is a portion of what I had initially. The native code worked as posted. It just goes out of scope when called my the managed function. As stated in the OP, I want to return a native string to a managed function that marshalls it to a System::String. Illustrated in the top of the thread :confused:

                    Jer 29:11

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #10

                    As Luc mentioned, one way to do this is to allocate the native string and free it when you're done with it:

                    wchar_t* SXNative::ReturnString()
                    {
                    wchar_t* retString = new wchar_t[128];
                    wchar_t Test[] = L"This is a dummy";

                    wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);

                    return retString;
                    }

                    String^ ManagedWrapper::Managed_ReturnString()
                    {
                    wchar_t* test = nat_ptr->ReturnString();
                    String^ retstr = gcnew String(test);
                    delete test;
                    return retstr;
                    }

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    A 1 Reply Last reply
                    0
                    • M Mark Salsbery

                      As Luc mentioned, one way to do this is to allocate the native string and free it when you're done with it:

                      wchar_t* SXNative::ReturnString()
                      {
                      wchar_t* retString = new wchar_t[128];
                      wchar_t Test[] = L"This is a dummy";

                      wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);

                      return retString;
                      }

                      String^ ManagedWrapper::Managed_ReturnString()
                      {
                      wchar_t* test = nat_ptr->ReturnString();
                      String^ retstr = gcnew String(test);
                      delete test;
                      return retstr;
                      }

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      A Offline
                      A Offline
                      alleyes 0
                      wrote on last edited by
                      #11

                      Thanks Mark, An illustration goes a long way than a theoretical discussion.   I thought about the return type from the native code, what's been discussed and realized that rather than marshalling or converting it in the managed function, why not have the managed string object returned in native function.   That's one of the bigger bonuses of C++/CLI right?   So I decided to do the following: <pre> String^ SXNative::ReturnString() {    wchar_t Test[] = L"This is a dummy";    msclr::auto_gcroot<String^> ret;    wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);    ret = marshal_as<String^>(retString);    return ret->ToString(); } </pre> <pre> String^ ManagedWrapper::Managed_ReturnString() {    return nat_ptr->ReturnString(); } </pre> I want to say thanks again for posting the example because that may help someone else like it did for me.   I also want to post my solution as well as I feel that it shows an alternative solution. Thanks again Mark   :-D Al

                      Jer 29:11

                      M 1 Reply Last reply
                      0
                      • A alleyes 0

                        Thanks Mark, An illustration goes a long way than a theoretical discussion.   I thought about the return type from the native code, what's been discussed and realized that rather than marshalling or converting it in the managed function, why not have the managed string object returned in native function.   That's one of the bigger bonuses of C++/CLI right?   So I decided to do the following: <pre> String^ SXNative::ReturnString() {    wchar_t Test[] = L"This is a dummy";    msclr::auto_gcroot<String^> ret;    wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);    ret = marshal_as<String^>(retString);    return ret->ToString(); } </pre> <pre> String^ ManagedWrapper::Managed_ReturnString() {    return nat_ptr->ReturnString(); } </pre> I want to say thanks again for posting the example because that may help someone else like it did for me.   I also want to post my solution as well as I feel that it shows an alternative solution. Thanks again Mark   :-D Al

                        Jer 29:11

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #12

                        Al_S wrote:

                        rather than marshalling or converting it in the managed function, why not have the managed string object returned in native function

                        I don't know what others read into your OP, but I personally figured the native function needed to be pure native. Had I known it could be mixed then it would have been much simpler :) Cheers, MArk

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        A 1 Reply Last reply
                        0
                        • M Mark Salsbery

                          Al_S wrote:

                          rather than marshalling or converting it in the managed function, why not have the managed string object returned in native function

                          I don't know what others read into your OP, but I personally figured the native function needed to be pure native. Had I known it could be mixed then it would have been much simpler :) Cheers, MArk

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          A Offline
                          A Offline
                          alleyes 0
                          wrote on last edited by
                          #13

                          Just the same, I would appreciate your opinion on what I posted.   Both version work but I would value your input as to what I showed. The real native code is left out for proprietary reasons, but what was illustrated in context was not necessarily written in stone as to having to be completely native code.   I should have stressed that the wrapper was a CLR DLL and that mixed mode was viable.

                          Jer 29:11

                          M 1 Reply Last reply
                          0
                          • A alleyes 0

                            Just the same, I would appreciate your opinion on what I posted.   Both version work but I would value your input as to what I showed. The real native code is left out for proprietary reasons, but what was illustrated in context was not necessarily written in stone as to having to be completely native code.   I should have stressed that the wrapper was a CLR DLL and that mixed mode was viable.

                            Jer 29:11

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #14

                            Al_S wrote:

                            I would appreciate your opinion on what I posted

                            It won't compile as shown? :) I suppose it could be simplified...

                            String^ SXNative::ReturnString()
                            {
                            wchar_t Test[] = L"This is a dummy";

                            return gcnew String(Test);
                            }

                            or

                            String^ SXNative::ReturnString()
                            {
                            return gcnew String(L"This is a dummy");
                            }

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            A 1 Reply Last reply
                            0
                            • M Mark Salsbery

                              Al_S wrote:

                              I would appreciate your opinion on what I posted

                              It won't compile as shown? :) I suppose it could be simplified...

                              String^ SXNative::ReturnString()
                              {
                              wchar_t Test[] = L"This is a dummy";

                              return gcnew String(Test);
                              }

                              or

                              String^ SXNative::ReturnString()
                              {
                              return gcnew String(L"This is a dummy");
                              }

                              Mark Salsbery Microsoft MVP - Visual C++ :java:

                              A Offline
                              A Offline
                              alleyes 0
                              wrote on last edited by
                              #15

                              Mark Salsbery wrote: It won't compile as shown? Smile I suppose it could be simplified... What YOU posted DOES compile and work for me.   Perhaps I don't understand what you mean.

                              Jer 29:11

                              M 1 Reply Last reply
                              0
                              • A alleyes 0

                                Mark Salsbery wrote: It won't compile as shown? Smile I suppose it could be simplified... What YOU posted DOES compile and work for me.   Perhaps I don't understand what you mean.

                                Jer 29:11

                                M Offline
                                M Offline
                                Mark Salsbery
                                wrote on last edited by
                                #16

                                Al_S wrote:

                                Perhaps I don't understand what you mean.

                                That was my opinion (joking) on the code you posted, because you asked. There's so many ways to code - I try hard not to give opinions (I'm hardly qualified to decide what code is good or bad), but instead I prefer to provide alternatives. Someone else may come along and provide an even better alternative...etc.

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                A 1 Reply Last reply
                                0
                                • M Mark Salsbery

                                  Al_S wrote:

                                  Perhaps I don't understand what you mean.

                                  That was my opinion (joking) on the code you posted, because you asked. There's so many ways to code - I try hard not to give opinions (I'm hardly qualified to decide what code is good or bad), but instead I prefer to provide alternatives. Someone else may come along and provide an even better alternative...etc.

                                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                                  A Offline
                                  A Offline
                                  alleyes 0
                                  wrote on last edited by
                                  #17

                                  Mark Salsbery wrote: Someone else may come along and provide an even better alternative...etc. Fair enough! My intent was just to put helpful information to add to what I received as help.

                                  Jer 29:11

                                  M 1 Reply Last reply
                                  0
                                  • A Andreoli Carlo

                                    Look at his thread ... maybe it can help, you its going from System::String (utf8 charset) do std::string link

                                    A Offline
                                    A Offline
                                    alleyes 0
                                    wrote on last edited by
                                    #18

                                    Thanks for the offer.   I think I got it (see later in thread).

                                    Jer 29:11

                                    1 Reply Last reply
                                    0
                                    • A alleyes 0

                                      Mark Salsbery wrote: Someone else may come along and provide an even better alternative...etc. Fair enough! My intent was just to put helpful information to add to what I received as help.

                                      Jer 29:11

                                      M Offline
                                      M Offline
                                      Mark Salsbery
                                      wrote on last edited by
                                      #19

                                      Al_S wrote:

                                      My intent was just to put helpful information to add to what I received as help

                                      I know. But you also asked my opinion - I quoted you on that ;P (ok, you didn't "ask", but you stated you'd appreciate it heh)

                                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                                      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