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 Native Pointer to IntPtr

Convert Native Pointer to IntPtr

Scheduled Pinned Locked Moved Managed C++/CLI
c++tutorialquestion
3 Posts 2 Posters 10 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    I can't figure out how to convert a native string pointer to an IntPtr. This is how I'm creating the native string pointer:

    PCWSTR pFullUrl = static_cast(static_cast(Marshal::StringToHGlobalUni(S->pFullUrl))); // S->pFullUrl is a reference to a managed structure's System::String member

    Now, in the destructor, I need to convert the PCWSTR pointer back to an IntPtr so I can call Marshal::FreeHGlobal(pFullUrl). Does anyone know how to perform the conversion from a native pointer to a managed IntPtr? NOTE: FreeHGlobal() takes IntPtr not IntPtr^, so I don't want to convert to IntPtr^.

    The difficult we do right away... ...the impossible takes slightly longer.

    L 1 Reply Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      I can't figure out how to convert a native string pointer to an IntPtr. This is how I'm creating the native string pointer:

      PCWSTR pFullUrl = static_cast(static_cast(Marshal::StringToHGlobalUni(S->pFullUrl))); // S->pFullUrl is a reference to a managed structure's System::String member

      Now, in the destructor, I need to convert the PCWSTR pointer back to an IntPtr so I can call Marshal::FreeHGlobal(pFullUrl). Does anyone know how to perform the conversion from a native pointer to a managed IntPtr? NOTE: FreeHGlobal() takes IntPtr not IntPtr^, so I don't want to convert to IntPtr^.

      The difficult we do right away... ...the impossible takes slightly longer.

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

      The documentation at Marshal.StringToHGlobalAnsi(String) Method (System.Runtime.InteropServices) | Microsoft Docs[^] says you can just free the native pointer.

      Richard Andrew x64R 1 Reply Last reply
      0
      • L Lost User

        The documentation at Marshal.StringToHGlobalAnsi(String) Method (System.Runtime.InteropServices) | Microsoft Docs[^] says you can just free the native pointer.

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Thanks Richard, I was just about to delete this post because I just realized that I could do FreeHGlobal(IntPtr(pFullUrl)) as long as pFullUrl is a LONGLONG. Thank you for your response.

        The difficult we do right away... ...the impossible takes slightly longer.

        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