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

Posted on 2006-05-04
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

    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'.
    LVL 8

    Assisted Solution

    "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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now