Click to See Complete Forum and Search --> : Strings and the Win API
Arawn
Nov 9th, 2002, 02:46 AM
Alright, I can send strings to api functions using the .c_str() deal, but that only works for functions like MessageBox that don't change it. How can I send strings so that these functions can modify them. An example of RegEnumKey would be great :)
parksie
Nov 9th, 2002, 05:07 AM
You can't. You need to allocate a buffer using new[] of the correct size (all Win32 API functions that return a string into a buffer also provide "safe" ways to get the length of said string) then construct a string from that buffer, and delete[] the buffer.
The Win32 API doesn't understand C++ strings.
Arawn
Nov 9th, 2002, 05:35 AM
I've been using LPSTRs, HeapAlloc(), and HeapFree(). Is that the "right" way to do it, or just a way to do it?
CornedBee
Nov 9th, 2002, 06:12 AM
You could also use new[] and delete[], but there actually is no difference.
Unless I start talking about UNICODE :)
parksie
Nov 9th, 2002, 06:55 AM
When it comes down to it, new[] and delete[] will indirectly call HeapAlloc(), although it's not guaranteed (they may allocate an arena from the OS, then manage that internally, extending it where necessary).
So yeah, your method is as good as any, since LPSTR is just a typedef for char*. Only thing that could make it a "wrong" way is that it's unportable, but if you're making a program for Windows that's a given :)
Arawn
Nov 9th, 2002, 07:40 AM
Why is it different in UNICODE?
CornedBee
Nov 9th, 2002, 08:18 AM
Quite a few little differences. Like, you can't use string the way it is, LPSTR, or any allocation functions except new without changes.
I'm sure there's a lot of material to be found on the web.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.