By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,382 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Using PInvoke with string pointers

P: n/a
I need to use functions through PInvoke which accept and return pointers to
strings. What is the best way to get string pointers (IntPtr I am
guessing?) to pass into these functions, and the best way to take the
pointers (again, IntPtr I assume?) into strings?

Thanks,

Bob
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
This is one area where there are so many ways to get the job done that it
gets confusing. In short, you can use the MarshalAsAttribute or
Marshal.StringToHGlobalAnsi method or the related or its related
StringToHGlobalUni and StringToHGlobalAuto methods.

To get the string back from the pointer, there are corresponding
PtrToStringAnsi, PtrToStringUni, and PtrToStringAuto methods.

To pass a string buffer that will be changed by the PInvoke call and
returned as a parameter to the called function, pass a Text.StringBuilder.

Different methods of passing the pointer have different memory management
requirements for freeing up the memory after the string, and the pointer to
it, is no longer needed. Make sure you check the memory management sections
in any MSDN articles on these methods.

HTH

DalePres
MCAD, MCDBA, MCSE
"Bob Dankert" <bo*@nospamnvsn-it.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...
I need to use functions through PInvoke which accept and return pointers to
strings. What is the best way to get string pointers (IntPtr I am
guessing?) to pass into these functions, and the best way to take the
pointers (again, IntPtr I assume?) into strings?

Thanks,

Bob

Nov 16 '05 #2

P: n/a
Thanks a lot - this worked perfectly!

Bob

"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
news:ur**************@TK2MSFTNGP14.phx.gbl...
This is one area where there are so many ways to get the job done that it
gets confusing. In short, you can use the MarshalAsAttribute or
Marshal.StringToHGlobalAnsi method or the related or its related
StringToHGlobalUni and StringToHGlobalAuto methods.

To get the string back from the pointer, there are corresponding
PtrToStringAnsi, PtrToStringUni, and PtrToStringAuto methods.

To pass a string buffer that will be changed by the PInvoke call and
returned as a parameter to the called function, pass a Text.StringBuilder.

Different methods of passing the pointer have different memory management
requirements for freeing up the memory after the string, and the pointer
to it, is no longer needed. Make sure you check the memory management
sections in any MSDN articles on these methods.

HTH

DalePres
MCAD, MCDBA, MCSE
"Bob Dankert" <bo*@nospamnvsn-it.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...
I need to use functions through PInvoke which accept and return pointers
to strings. What is the best way to get string pointers (IntPtr I am
guessing?) to pass into these functions, and the best way to take the
pointers (again, IntPtr I assume?) into strings?

Thanks,

Bob


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.