Reputation: 386
I've inherited a MFC project from another developer. Under multiple MFC methods, calls are made to a COM server, passing strings as arguments. Here is a representative example of the conversion pattern in the current code base:
COM server:
void COMServer::foo(_bstr_t strParam)
MFC method:
void Foo::foo(CString &csParam)
{
CComBSTR bstrParam(strDocName);
ptrToComServer->foo((BSTR)bstrParam);
}
After reading about string conversion rules in this article, I'm wondering if the current pattern of conversion is
With respect to the above two concerns, I'd also like to know if the following conversion would be better
void Foo::foo(CString &csParam)
{
_bstr_t bstrParam(csParam);
ptrToComServer->foo(bstrParam);
}
Upvotes: 1
Views: 7065
Reputation: 7620
The legacy MFC method should be improved, by removing the (BSTR) cast.
void Foo::foo(CString &csParam)
{
CComBSTR bstrParam(strDocName);
ptrToComServer->foo(bstrParam);
}
I see no memory corruption in your existing code, or in your new proposal. As for efficiency, the two methods seem pretty identical to me.
If you are really concerned by performances (are you really sure that maters? Do you make several thousands per second COMServer::foo calls ) you should begin by (concerning your "strings"):
CComBSTR
or _bstr_t
as soon as possible, eventually avoiding the CString
to BSTR
copies/conversions.Upvotes: 2
Reputation: 122
Please use the following conversion,
CString aTestUser(_T("TestUser"));
_bstr_t aConvertedUser(aTestUser);
Upvotes: 0