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. Convert from __GC String to const WCHAR

Convert from __GC String to const WCHAR

Scheduled Pinned Locked Moved Managed C++/CLI
question
6 Posts 2 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.
  • D Offline
    D Offline
    DR Clevenger
    wrote on last edited by
    #1

    Hello All, I trying to call an unmanged code function from managed code and the unmanaged code function requires a type of WCHAR. How can I convert a __GC String to type of WCHAR so it be passed to the function. If I type out the value, such as const WCHAR *pdn = L"Some value"; It works; however I need the "Some value" to be passed in as a variable. By the way this unmanged code is in unicode, if that helps. Thanks, DC

    T 1 Reply Last reply
    0
    • D DR Clevenger

      Hello All, I trying to call an unmanged code function from managed code and the unmanaged code function requires a type of WCHAR. How can I convert a __GC String to type of WCHAR so it be passed to the function. If I type out the value, such as const WCHAR *pdn = L"Some value"; It works; however I need the "Some value" to be passed in as a variable. By the way this unmanged code is in unicode, if that helps. Thanks, DC

      T Offline
      T Offline
      Tom Archer
      wrote on last edited by
      #2

      // From Extending MFC Applications with the .NET Framework

      #include //...
      String* s = S"Hello";
      const __wchar_t __pin * str = PtrToStringChars(s);

      Cheers, Tom Archer * Inside C# -Second Edition * Visual C++.NET Bible * Extending MFC Applications with the .NET Framework

      D 1 Reply Last reply
      0
      • T Tom Archer

        // From Extending MFC Applications with the .NET Framework

        #include //...
        String* s = S"Hello";
        const __wchar_t __pin * str = PtrToStringChars(s);

        Cheers, Tom Archer * Inside C# -Second Edition * Visual C++.NET Bible * Extending MFC Applications with the .NET Framework

        D Offline
        D Offline
        DR Clevenger
        wrote on last edited by
        #3

        Thanks, When I try this I get the following: cannot convert parameter 1 from 'const wchar_t __pin *volatile ' to 'WCHAR *' Conversion loses qualifiers: Here is the code Unmanaged Code UserStatus* WaitForChanges(WCHAR* dn) { UserStatus* status = new UserStatus(); MonitorLDAPChanges(pdn); return status; } Managed Code const __wchar_t __pin * str = PtrToStringChars(dn); WaitForChanges(str);

        D 1 Reply Last reply
        0
        • D DR Clevenger

          Thanks, When I try this I get the following: cannot convert parameter 1 from 'const wchar_t __pin *volatile ' to 'WCHAR *' Conversion loses qualifiers: Here is the code Unmanaged Code UserStatus* WaitForChanges(WCHAR* dn) { UserStatus* status = new UserStatus(); MonitorLDAPChanges(pdn); return status; } Managed Code const __wchar_t __pin * str = PtrToStringChars(dn); WaitForChanges(str);

          D Offline
          D Offline
          DR Clevenger
          wrote on last edited by
          #4

          I got it I forgot to put the 'const' in the method.

          T 1 Reply Last reply
          0
          • D DR Clevenger

            I got it I forgot to put the 'const' in the method.

            T Offline
            T Offline
            Tom Archer
            wrote on last edited by
            #5

            DR Clevenger wrote: I got it I forgot to put the 'const' in the method Yep :) Here's the thing. The __pin keyword is used to indicate that the value should not be moved in memory. This is done because a .NET String is subject to being moved about by the GC and when you convert that data into a WCHAR* for purposes of using that pointer over any length of time, you need to ensure that the data is not moved. The WCHAR* is also defined as const because String objects are immutable. Anyway, you probably know this, but I wanted to give you a complete answer on what is going on here just in case. Cheers, Tom Archer * Inside C# -Second Edition * Visual C++.NET Bible * Extending MFC Applications with the .NET Framework

            D 1 Reply Last reply
            0
            • T Tom Archer

              DR Clevenger wrote: I got it I forgot to put the 'const' in the method Yep :) Here's the thing. The __pin keyword is used to indicate that the value should not be moved in memory. This is done because a .NET String is subject to being moved about by the GC and when you convert that data into a WCHAR* for purposes of using that pointer over any length of time, you need to ensure that the data is not moved. The WCHAR* is also defined as const because String objects are immutable. Anyway, you probably know this, but I wanted to give you a complete answer on what is going on here just in case. Cheers, Tom Archer * Inside C# -Second Edition * Visual C++.NET Bible * Extending MFC Applications with the .NET Framework

              D Offline
              D Offline
              DR Clevenger
              wrote on last edited by
              #6

              Thanks for your help and the information.

              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