problem in _bstr_t
-
I am using two functions ------------------------- STDMETHODIMP get_Path(BSTR *pbstrPath); STDMETHODIMP put_Path(BSTR pbstrPath); my piece of code is ---------------------- _bstr_t m_bstrNewPath; CHAR szFileName[MAX_PATH] = {0}; ZeroMemory(szFileName, sizeof(szFileName)); Edit_GetText(GetDlgItem(m_Dlg, IDC_PathEditCtrl),szFileName, MAX_PATH); m_bstrNewPath = szFileName; when i am calling the function it is ok put_Path(m_bstrNewPath); but when I am calling the function get_Path(m_bstrNewPath); it fires error Cannot convert '_bstr_t' to 'wchar_t * *' so please help me how to fix that bug
Rajesh
-
I am using two functions ------------------------- STDMETHODIMP get_Path(BSTR *pbstrPath); STDMETHODIMP put_Path(BSTR pbstrPath); my piece of code is ---------------------- _bstr_t m_bstrNewPath; CHAR szFileName[MAX_PATH] = {0}; ZeroMemory(szFileName, sizeof(szFileName)); Edit_GetText(GetDlgItem(m_Dlg, IDC_PathEditCtrl),szFileName, MAX_PATH); m_bstrNewPath = szFileName; when i am calling the function it is ok put_Path(m_bstrNewPath); but when I am calling the function get_Path(m_bstrNewPath); it fires error Cannot convert '_bstr_t' to 'wchar_t * *' so please help me how to fix that bug
Rajesh
The safe way is to:
BSTR temp = 0;
HRESULT hr = get_Path(&temp);
// TODO: handle errors here
m_bstrNewPath.Attach(temp);The reason is that get_Path allocates the string and needs to return you a pointer, so the parameter uses double indirection of
(chartype) **
. _bstr_t, for various reasons, cannot safely wrap this operation. There are a few circumstances where you can avoid the temporary (but you have to know exactly when - and when not). So rather play it safe and use above construct. There is no performance hit involved. in get_Path, path is an [out] only parameter (i.e. the original valeu is ignored). If you have a method with an [in/out] parameter, use:_bstr_t path = ...; // from whereever
BSTR temp = path.Detach();
HRESULT hr = comObject->ModifyPath(&temp);
path.Attach(temp);
if (FAILED(hr))
{
// handle error here
}note the different order of attaching vs. error handling.
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!| FoldWithUs! | sighist