Should a BSTR From a CString::AllocSysString() be freed? - c++

/* Code Snippet */
CString pString;
pString.Format("LoginWhite : %s",IP.GetString());
BSTR szRuleFind = pString.AllocSysString();
// Check for Existing Rule
HRESULT hr = pRules->Item(szRuleFind,&pFind);
SysFreeString(szRuleFind);
Would the "Freeing" of the BSTR be done like this or would the COM Object[INetFwRules* in this case] do so when you ::Release() the Object?

Related

how to convert com instance to variant, to pass it in idispach invoke

i want to pass a com object instance as a variant parameter to another active x object function, for that i need to convert the idispatch pointer to a variant? i am not sure.
hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
{
return;
}
hr = CLSIDFromProgID(objectName.c_str(), &clsid);
if (FAILED(hr))
{
return;
}
hr = CoCreateInstance(clsid, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pApp));
if (FAILED(hr) || pApp == nullptr) {
return;
}
this is the instance creating code, after that i am using this :
VARIANT v;
VariantInit(&v);
v.pdispVal = pApp;
v.ppdispVal = &pApp;
v.vt = VT_DISPATCH;
return v;
and passing it to an active x method, but it is giving access violation after invoke.
what i am doing wrong?
If you want to use the VARIANT raw structure, you can code it like this:
VARIANT v;
VariantInit(&v);
pApp->AddRef();
v.pdispVal = pApp;
v.vt = VT_DISPATCH;
...
// later on, some code (this code or another code) will/should call this
VariantClear(&v); // implicitely calls pdispVal->Release();
Or, if you're using the Visual Studio development environment, then you can just use the _variant_t or CComVariant (ATL) smart wrappers which I recommend. In this case, you can just call it like this:
IDispatch *pApp = ...
// both wrappers will call appropriate methods
// and will release what must be, when destroyed
CComVariant cv = pApp;
// or
_variant_t vt = pApp;
PS: don't use both wrapper classes, make your choice. If a project uses ATL, I uses CComVariant, otherwise _variant_t, for example.

Get C# out string paramater value to C++ BSTR*?

I have a method in C# DLL having one input and two output parameters. All the three are String.
I want to call that function from firebreath C++. How do I have to pass the BSTR* value from C++?. And how do I get that out values from C#?
I have already tried to get using
BSTR* userKey =NULL;
*userKey = ::SysAllocString(L"Hello Managed World");
But its not working.
C# function:
public void GetPublicKeyforEncryption(String ToUserEmailAddress, out String userKey , out String mySharedKey)
C++ Calling function:
std::wstring& ToUserEmailaddress;
BYTE **bEncryptData = NULL;
BSTR bsData = SysAllocStringLen(ToUserEmailaddress.data(), ToUserEmailaddress.size());
BSTR* userKey =NULL;
BSTR* mySharedKey =NULL;
CSharpInterface->GetPublicKeyforEncryption(bsData,userKey ,mySharedKey );
In the above c++ GetPublicKeyforEncryption bsData value is passed correctly,How will i allocate memory and Pass
userKey ,mySharedKey in the BSTR* type
Anyone can solve this issue?
Help me.
Thanks
Sanju
That's not correct code, BSTR is already a pointer type. You need to pass a pointer to your BSTR variable so the called method can assign it. Generic code that assumes just one out string argument:
BSTR retval = NULL;
HRESULT hr = CSharpInterface->SomeMethod(&retval);
if (SUCCEEDED(hr)) {
// Use retval
//...
SysFreeString(retval);
}

Shall we treat BSTR type in COM as value or reference?

From book ATL Internals, I knew BSTR is different from OLECHAR*, and there are CComBSTR and CString for BSTR.
According MSDN Allocating and Releasing Memory for a BSTR, I knew memory management responsibility for caller/callee.
Take this line from MSDN,
HRESULT CMyWebBrowser::put_StatusText(BSTR bstr)
I still do not know how to handle bstr properly in my implementation. Since I still have a basic question for BSTR -- should we treat bstr as a value (like int) or as a reference (like int*), at least on COM interface boundary.
I want to convert BSTR as soon as possible to CString/CComBSTR in my implementation. Value or Reference semantic will be totally different case for the conversion. I've digged into CComBSTR.Attach, CComBSTR.AssignBSTR, etc. But the code cannot solve my doubts.
MSDN CComBSTR.Attach has some code snip, I feel it is wrong since it is not obey Allocating and Releasing Memory for a BSTR. ATL Internals said SetSysString will 'free the original BSTR passed in', if I used it, it will violate BSTR argument convention, just like CComBSTR.Attach.
All in all, I want to using CString to handle raw BSTR in implementation, but do not know the correct way...I've written some just work code in my projects, but I always feel nervous since I don't know whether I am correct.
Let me talk coding language
HRESULT CMyWebBrowser::put_StatusText(BSTR bstr)
{
// What I do NOT know
CString str1; // 1. copy bstr (with embeded NUL)
CString str2; // 2. ref bstr
// What I know
CComBSTR cbstr1;
cbstr1.AssignBSTR(bstr); // 3. copy bstr
CComBSTR cbstr2;
cbstr2.Attach(bstr); // 4. ref bstr, do not copy
// What I do NOT know
// Should we copy or ref bstr ???
}
CComBSTR is just a RAII wrapper around raw BSTR. So feel free to use CComBSTR instead of raw BSTR to help writing code that is exception-safe, that makes it harder to leak resources (i.e. the raw BSTR), etc.
If the BSTR is an input parameter, it's just like a const wchar_t* (with length prefixed, and potentially some NULs L'\0' characters inside). If the BSTR doesn't have NULs embedded inside, you can simply pass it to a CString constructor, that will make a deep-copy of it, and you can locally work with your CString. Modifications to that CString won't be visible on the original BSTR. You can use std::wstring as well (and note that std::wstring can handle embedded NULs as well).
void DoSomething(BSTR bstrInput)
{
std::wstring myString(bstrInput);
// ... work with std::wstring (or CString...) inside here
}
Instead, if the BSTR is an output parameter, then it is passed using another level of indirection, i.e. BSTR*. In this case, you can use CComBSTR::Detach() inside your method to release the BSTR safely wrapped into the CComBSTR, and transfer its ownership to the caller:
HRESULT DoSomething( BSTR* pbstrOut )
{
// Check parameter pointer
if (pbstrOut == nullptr)
return E_POINTER;
// Guard code with try-catch, since exceptions can't cross COM module boundaries.
try
{
std::wstring someString;
// ... work with std::wstring (or CString...) inside here
// Build a BSTR from the ordinary string
CComBSTR bstr(someString.c_str());
// Return to caller ("move semantics", i.e. transfer ownership
// from current CComBSTR to the caller)
*pbstrOut = bstr.Detach();
// All right
return S_OK;
}
catch(const std::exception& e)
{
// Log exception message...
return E_FAIL;
}
catch(const CAtlException& e)
{
return e; // implicit cast to HRESULT
}
}
Basically, the idea is to use BSTR (wrapped in a RAII class like CComBSTR) only at the boundary, and do the local work using std::wstring or CString.
As a "bouns reading", consider Eric Lippert's guide to BSTR semantics.
Having BSTR on input, you are not responsible to release it. Converting to CString is easy:
CString sValue(bstr);
or, if you prefer to keep Unicode characters on MBCS build:
CStringW sValue(bstr);
If you need to convert back when you have [out] parameter, you do (simple version):
VOID Foo(/*[out]*/ BSTR* psValue)
{
CString sValue;
*psValue = CComBSTR(sValue).Detach();
}
Full version is:
STDMETHODIMP Foo(/*[out]*/ BSTR* psValue)
{
_ATLTRY
{
ATLENSURE_THROW(psValue, E_POINTER); // Parameter validation
*psValue = NULL; // We're responsible to initialize this no matter what
CString sValue;
// Doing our stuff to get the string value into variable
*psValue = CComBSTR(sValue).Detach();
}
_ATLCATCH(Exception)
{
return Exception;
}
return S_OK;
}

Using _bstr_t to pass parameter of type BSTR* in function

What is the correct way of doing this:
_bstr_t description;
errorInfo->GetDescription( &description.GetBSTR() );
or:
_bstr_t description;
errorInfo->GetDescription( description.GetAddress() );
Where IError:GetDescription is defined as:
HRESULT GetDescription (BSTR *pbstrDescription);
I know I could easily do this:
BSTR description= SysAllocString (L"Whateva"));
errorInfo->GetDescription (&description);
SysFreeString (description);
Thanks
The BSTR is reference counted, I seriously doubt that will work right if you use GetAddress(). Sadly the source code isn't available to double-check that. I've always done it like this:
BSTR temp = 0;
HRESULT hr = p->GetDescription(&temp);
if (SUCCEEDED(hr)) {
_bstr_t wrap(temp, FALSE);
// etc..
}
To follow up on #Hans's answer - the appropriate way to construct the _bstr_t depends on whether GetDescription returns you a BSTR that you own, or one that references memory you don't have to free.
The goal here is to minimize the number of copies, but also avoid any manual calls to SysFreeString on the returned data. I would modify the code as shown to clarify this:
BSTR temp = 0;
HRESULT hr = p->GetDescription(&temp);
if (SUCCEEDED(hr)) {
_bstr_t wrap(temp, false); // do not copy returned BSTR, which
// will be freed when wrap goes out of scope.
// Use true if you want a copy.
// etc..
}
A late answer that may not apply to earlier (or later) versions of Visual Studio; however,
VS 12.0 has the _bstr_t implementation inline, and evidently an internal Data_t instance is created with a m_RefCount of 1 when calling GetBSTR() on a virgin _bstr_t. So the _bstr_t lifecycle in your first example looks to be okay:
_bstr_t description;
errorInfo->GetDescription( &description.GetBSTR() );
But if _bstr_t is dirty, the existing internal m_wstr pointer will be overwritten, leaking the previous memory it referenced.
By using the following operator&, a dirty _bstr_t can be used given that it's first cleared via Assign(nullptr). The overload also provides the convenience of utilizing the address operator instead of GetBSTR();
BSTR *operator&(_bstr_t &b) {
b.Assign(nullptr);
return &b.GetBSTR();
}
So, your first example could instead look like the following:
_bstr_t description(L"naughty");
errorInfo->GetDescription(&description);
This evaluation was based on comutil.h from VS 12.0.
_bstr_t (and its ATL sibling CComBSTR) are resource owners of BSTR. Spying from the code it seems that 'GetAddress' is specifically designed for the use case of working with BSTR output parameters where it is expected that client frees the BSTR.
Using 'GetAddress()' is not equivalent to using '&GetBSTR()' in case the _bstr_t already owns a BSTR. MSDN states: 'Frees any existing string and returns the address of a newly allocated string.'.
_bstr_t bstrTemp;
HRESULT hr = p->GetDescription(bstrTemp.GetAddress());
Caveat: this specific use case of 'GetAddress' is not stated in the documentation; it is my deduction from looking at the source code and experience with its ATL counter part CComBSTR.
Since user 'caoanan' questioned this solution, I paste here the source code Microsoft:
inline BSTR* _bstr_t::GetAddress()
{
Attach(0);
return &m_Data->GetWString();
}
inline wchar_t*& _bstr_t::Data_t::GetWString() throw()
{
return m_wstr;
}
inline void _bstr_t::Attach(BSTR s)
{
_Free();
m_Data = new Data_t(s, FALSE);
if (m_Data == NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
inline _bstr_t::Data_t::Data_t(BSTR bstr, bool fCopy)
: m_str(NULL), m_RefCount(1)
{
if (fCopy && bstr != NULL) {
m_wstr = ::SysAllocStringByteLen(reinterpret_cast<char*>(bstr),
::SysStringByteLen(bstr));
if (m_wstr == NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
else {
m_wstr = bstr;
}
}
My answer is also late. Suppose you have the signature HRESULT PutDescription (BSTR NewDescription);. In that case do the following
_bstr_t NewAdvice = L"Great advice!";
HRESULT hr1 = PutDescription(NewAdvice.GetBSTR());
By the rules of COM, the function PutDescription is not allowed to change or even destroy the passed BSTR.
For the opposite HRESULT GetDescription (BSTR *pActualDescription); pass a virgin _bstr_t by means of the function GetAddress():
_bstr_t GetAdvice;
HRESULT hr2 = GetDescription(GetAdvice.GetAddress());
The function GetAddress() frees any existing string and returns the address of a newly allocated string. So, if you pass a _bstr_t that has some content, this content will be freed and is therefore lost. The same happens to all _bstr_ts that share the same BSTR. But I think this is a stupid thing to do. Why passing an argument with content to a function that is supposed to change that content?
_bstr_t GetAdvice = L"This content will not survive the next function call!";
HRESULT hr = GetDescription(GetAdvice.GetAddress());
A real moron might even pass a _bstr_t that is assigned to a raw BSTR:
BSTR Bst = ::SysAllocString(L"Who would do that?");
_bstr_t GetDescr;
GetDescr.Attach(Bst);//GetDescr wraps Bst, no copying!
HRESULT hr = GetDescription(GetDescr.GetAddress());
In that case GetDescr gets the expected value but the content of Bst is unpredictable.
int GetDataStr(_bstr_t & str) override {
BSTR data = str.Detach();
int res = m_connection->GetDataStr( &data );
str.Attach(data);
return res;
}

COM method call returns Catastrophic Failure

Note:
Pass BSTR variable to COM method, HRESULT return is 8000FFFF
Previous calls with interface pointer, was successful: HRESULT is 0
Execution, inside Visual Studio succeeds, outside fails - release and debug
Illustration:
const char *simFile;
simFile = new char;
//omitted
_bstr_t simFileToOpen(simFile);
BSTR raw_sim_Open = simFileToOpen.copy();
SysFreeString(simFileToOpen);
delete simFile;
hresult = pis8->raw_Open (raw_sim_Open); //0x8000FFFF returned
simFile looks to be a single character stored inside a const char*.
It is not a NULL terminated string, unless it is an empty string and it's contents are 0.
Are you sure you didn't mean to do something like:
const char *simFile = new char[1024];
strcpy(simFile, "path");
Even better yet you can just use SysAllocString to get a BSTR directly.
BSTR str = SysAllocString(_T("path"));