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

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?
XtreemAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
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'.
0
 
Dariusz DziaraConnect With a Mentor ProgrammerCommented:
"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().

Example:
  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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.