a more efficient use of BSTR?

This is a discussion on a more efficient use of BSTR? within the C++ Programming forums, part of the General Programming Boards category; <code> TCHAR szBuf[14]; szBuf[0]=0; wsprintf(szBuf, _T("some string, %d", i=1); ComBSTR bstrName = (BSTR)szBuf; </code> It seems wrong to allocate space ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    4

    a more efficient use of BSTR?

    <code>

    TCHAR szBuf[14];
    szBuf[0]=0;
    wsprintf(szBuf, _T("some string, %d", i=1);
    ComBSTR bstrName = (BSTR)szBuf;

    </code>

    It seems wrong to allocate space for a wide-character string, get it into a buffer and then create a ComBSTR class to hold it. Is there a more optimal way to do this with just ComBSTR or, _bstr_t ?

    -su

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    Ah, a typo. Here's the actual code snip:
    <code>
    TCHAR szBuf[14];
    szBuf[0]=0;
    wsprintf(szBuf, _T("some string, %d", i+1);
    ComBSTR bstrName = (BSTR)szBuf;
    </code>

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Use SysAllocString/SysFreeString for raw bstr types. Assigning a bstr as L"String" or _T("String"), is incorrect. CComBSTR will internally convert the string you give it to a bstr format.

    Also using wsprintf with a TCHAR is incorrect. You should use the TCHAR equivalent.
    Last edited by valaris; 02-11-2010 at 08:27 AM.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Also, your wsprintf() will overflow the buffer szBuf - it generates 15 characters of data, counting the null terminator. (Also, you don't need to zero the first character - wsprintf() is just overwriting that.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,266
    Quote Originally Posted by sulu View Post
    Ah, a typo. Here's the actual code snip:
    <code>
    TCHAR szBuf[14];
    szBuf[0]=0;
    wsprintf(szBuf, _T("some string, %d", i+1);
    ComBSTR bstrName = (BSTR)szBuf;
    </code>
    Fixed:
    Code:
    TCHAR buffer[32];
    _stprintf(buffer, _T("some string, %d"), i+1);
    CComBSTR name = buffer;
    There. Now your program is not going to break if you change from unicode to MBCS. Do not worry about the fact that there is one buffer on the stack and another allocate from SysAllocString. This is normal when writing code such as we have here.
    Last edited by iMalc; 02-12-2010 at 12:29 AM. Reason: Fixed wrong function name on the print line
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    4

    Smile a more efficient use of BSTR?

    Thanks, all.

    I see no way, based on my reading, to get the parameter represented by the %d into the CComBSTR through a constructor call. In other words, it seems I must call some kind of wchar version of printf-type of function to get the %d guy returned and appended to the initializing character string.

    Thank you all for excellent responses!

    -su

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    Also, I find no information on the _tcsprintf method referenced previously. -su

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412

  10. #10
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    There was nothing wrong with the function used in the original code. wsprintf != swprintf

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by adeyblue View Post
    There was nothing wrong with the function used in the original code. wsprintf != swprintf
    Oops, sorry. Thought the wsprintf in the OP was a typo for swprintf. Didn't even know wsprintf existed, thanks for the link.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,266
    Quote Originally Posted by adeyblue View Post
    There was nothing wrong with the function used in the original code. wsprintf != swprintf
    Yes there absolutely was something wrong with using wsprintf. The code would needlessly fail to compile if the project settings were change from Unicode to Multi-Byte-Character-System.

    You don't use TCHAR (which can be either characters size) and then explicitly use the wide char version of a function. You either use the TCHAR versions for all bits, or you use the WCHAR or CHAR versions for all bits.
    It's just like using a char and then copying that into an int and expecting it to be negative if the high bit is set. Since a char can be unsigned, it might not do what you expect. You need to use the right data types, and functions taking those types, for the right job.
    Last edited by iMalc; 02-12-2010 at 12:37 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by iMalc View Post
    Yes there absolutely was something wrong with using wsprintf. The code would needlessly fail to compile if the project settings were change from Unicode to Multi-Byte-Character-System.

    You don't use TCHAR (which can be either characters size) and then explicitly use the wide char version of a function. You either use the TCHAR versions for all bits, or you use the WCHAR or CHAR versions for all bits.
    It's just like using a char and then copying that into an int and expecting it to be negative if the high bit is set. Since a char can be unsigned, it might not do what you expect. You need to use the right data types, and functions taking those types, for the right job.
    But if you look at the declaration of wsprintf you'll see that it takes the arguments LPTSTR and LPCTSTR. Long Pointer to (Const) TCHAR String. So it's not wide-char specific, it depends on if UNICODE is defined or not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Most Efficient Way to Check a Variable Is A Whole Number
    By bengreenwood in forum C++ Programming
    Replies: 29
    Last Post: 05-28-2009, 01:33 PM
  2. Deleting BSTR help!
    By chiefmonkey in forum Windows Programming
    Replies: 6
    Last Post: 04-29-2009, 10:56 AM
  3. Is std::map efficient for this problem?
    By dudeomanodude in forum C++ Programming
    Replies: 12
    Last Post: 04-10-2008, 02:15 PM
  4. Efficient Algorithm
    By purple in forum C Programming
    Replies: 10
    Last Post: 02-05-2003, 02:01 PM
  5. How do I write more efficient code?
    By minesweeper in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 11:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21