Help with std::string

Status
Not open for further replies.
Hey,

This feels like a silly question... I've not had much experience with using the STL's with functions like SendMessage(..., WM_GETTEXT,...). I know when I want access to the characters in the std::string for use in other functions to use the c_str method. My Question is how do I supply the pointer to calls such as SendMessage when the function will allocate characters to my std::string?


Like I said, sounds like an easy one... I just don't know the answer, and the documentation I've found so far isn't very clear.

Anyone help me on this one?

Jher
 
Ugh, that was not what I was wanting to hear.....

Unfortunately, I have to use the API functions. I'm working on a dll with a keyboard hook in another application and a bit of subclassing mixed in, and the hook of this type requires a Win32 dll - and no MFC. (I tried using CString/afx.h, and the hook call fails when it's included).

Oh well, I'll keep doing my half STL half char[] stuff.

Thanks euzeka

Jher
 
Ugh, that was not what I was wanting to hear.....
Well, sorry ;) If you do a lot of those calls, you might consider wrapping them in a function that:
a) retrieves the required buffer size (most win32 APIs have some way of doing this, usually by saying you're passing an empty buffer)
b) allocates a char[] of the required size
c) calls the function
d) creates a std::string from the char[]
e) deletes the char[]

If could save you a lot of work, and potential errors. Only worth it if you're going to make the same API call a lot of times though. This also hurts performance somewhat (2 dynamic allocations instead of 1).
 
Well, I'm not doing enough of the calls to justifiy the slowdown, and most of those calls I'm doing in a WH_KWYBOARD Hook, and a subclass of another application's control... so I have to be in and out quickly.

What I ended up doing is scraping by with standard c lib str functions (strncpy, strncat, etc.) when I can, and dumping the char strings into a STL string when I need to do serious parsing.

Not very pretty, but it does get the job done.

Anyway, thanks again!

Jher
 
Status
Not open for further replies.