Thread: Deleting BSTR help!

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    34

    Deleting BSTR help!

    Hi guys,

    So is it legal to do the following to free the allocated string?

    Code:
    LPWSTR szString = SysAllocString("Some String");
    ...
    SysFreeString((BSTR)(szString));
    If I remember correctly, I think a BSTR string has its size recorded within it, so I wasn't sure casting WCHAR* to BSTR was valid or not.

    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The cast should not be needed. Does it generate a compile error without the cast?

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    No I haven't tried that yet. So LPWSTR string to BSTR string cast is okay when SysFreeString(), huh? Thanks!

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    SysAllocString returns a BSTR, which is entirely different from a wide character string. Attempting to cast between the two is not an option.

    If you want a wide character string, then I think this will work:
    Code:
    LPWSTR szString = new WSTR[lstrlen(L"Some String") + sizeof(*szString))];
    lstrcpy(szString, L"Some String");

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Attempting to cast between the two is not an option.
    It can be an option. If the BSTR is a NULL terminated string, you can treat it just like a "const wchar_t*" string.

    gg

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    I may be wrong, but I thought that BSTR->WCHAR* cast is okay but WCHAR*->BSTR cast is not okay (since BSTR contains more info)? Isn't BSTR basically a WCHAR* string with the string size information prefixed? And how can ther be a BSTR string without the NULL terminator (I mean unless we intentially erase the terminator character)?
    Last edited by chiefmonkey; 04-29-2009 at 10:19 AM.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    BSTR's don't *have* to be NULL terminated - since their length is prefixed.

    Correct - If an API expects a BSTR then you must pass a BSTR. If an API takes a const WCHAR*, then you can pass in a NULL terminated BSTR. For non-const WCHAR* API's, you need make sure it doesn't do anything to the buffer that a BSTR wouldn't appreciate.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and deleting directories and deleting files
    By fguy817817 in forum C Programming
    Replies: 1
    Last Post: 04-08-2009, 07:26 AM
  2. convert std string to bstr without ATL
    By mungy in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2006, 05:28 AM
  3. deleting nodes in a linear list
    By sudhanshu_nsit in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2006, 02:00 AM
  4. Deleting Records from a structure
    By Simon in forum C Programming
    Replies: 5
    Last Post: 09-11-2002, 11:28 PM
  5. need help deleting a deleting a struct from file
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-20-2002, 05:38 AM