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 :)
Printable View
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 :)
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.
I've been using LPSTRs, HeapAlloc(), and HeapFree(). Is that the "right" way to do it, or just a way to do it?
You could also use new[] and delete[], but there actually is no difference.
Unless I start talking about UNICODE :)
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 :)
Why is it different in UNICODE?
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.