Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem in _bstr_t

problem in _bstr_t

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rjkg
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • R rjkg

      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

      P Offline
      P Offline
      peterchen
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups