Convert Native Pointer to IntPtr
-
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.
-
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.
-
The documentation at Marshal.StringToHGlobalAnsi(String) Method (System.Runtime.InteropServices) | Microsoft Docs[^] says you can just free the native pointer.
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.