[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


LPTSTR (or LPCTSTR) vs. _bstr_t as a parameter of a function

Posted on 2006-05-04
Medium Priority
Last Modified: 2008-01-16
Is there anything fundamentally or philosophically wrong with using _bstr_t as a parameter of a function rather than LPTSTR (or LPCTSTR)?  I notice all Unicode Platform SDK functions take LPTSTR (or LPCTSTR) as parameters, never _bstr_t, but it tends to make little difference to the caller of the function. The only difference it makes is how the functions deals with the passed-in string.

I guess with _bstr_t, the caller can pass in a TCHAR*, LPTSTR, LPCTSTR, char* or wchar_t* and it will still work fine.  But with LPTSTR (or LPCTSTR), the caller must pass in a TCHAR* type (e.g., LPTSTR, TCHAR*, _T("")).

Any ideas on what is the status quo (if any) or why one way is better than the other?
Question by:Xtreem
LVL 86

Accepted Solution

jkr earned 1000 total points
ID: 16607435
No, but a _bstr_t is a COM helper class that encapsulates BSTRs. If you need to pass BSTRs, either pass them 'as is' and use the conversions provided with that helper class. If you need to pass UNICODE strings, just pass UNICODE strings. There's nothing wrong with passing a '_bstr_t', but when you want to interoperate with modules that are written with compilers/languages that don't use or know '_bstr_t', you'll be in trouble. In general, if you nedd it, use it, but don't if it introduces only overhead. You wouldn't pass a single character as a '__int64' either, so it's just a matter of 'choosing your weapons wisely'.

Assisted Solution

mrblue earned 400 total points
ID: 16609912
"Is there anything fundamentally or philosophically wrong with using _bstr_t as a parameter of a function rather than LPTSTR (or LPCTSTR)?" - yes, in general case it is incorrect.

LPTSTR it macro that can evaluate to LPWSTR if _UNICODE is defined or to LPSTR if it is not defined.
If you want versatile version you should use for example string conversion macros like W2T() or W2CT().

  function(.., W2T((BSTR)_bstr_t("text")), ...)

When _UNICODE is defined no conversion will be performed.

"I notice all Unicode Platform SDK functions take LPTSTR" - note that multibyte samples take LPTSTR parameters also.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question