Results 1 to 7 of 7

Thread: Strings and the Win API

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    Oregon
    Posts
    64

    Strings and the Win API

    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

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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 refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    Oregon
    Posts
    64
    I've been using LPSTRs, HeapAlloc(), and HeapFree(). Is that the "right" way to do it, or just a way to do it?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You could also use new[] and delete[], but there actually is no difference.

    Unless I start talking about UNICODE
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    Oregon
    Posts
    64
    Why is it different in UNICODE?

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width