• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 529
  • Last Modified:

How to get the BSTR value set through property in ATL COM Dll

Hi,

I have created a property in my COM Dll and the property type is BSTR.I have retrieved a char * and
want to get it thru property .How to do that ????

e.g:
STDMETHODIMP XYZ::get_Path(BSTR AppName, BSTR *pVal)
{
 //Retieved a chsr * value lets say "Hello".
 How can I get it as return value in pVal????
Can I Type cast it.If yes,how???
}
0
goodami
Asked:
goodami
1 Solution
 
RobalitoruCommented:
Try this:
        USES_CONVERSION;
        *pVal=A2BSTR( yourChar );

0
 
SteveGTRCommented:
Here's a little help class that helps with BSTR's:

#define BSTR_MAX_STRING   1024

// Helper class for BSTR processing
class CBStr
{
public:
  CBStr(LPCTSTR lpszValue=NULL);
  virtual ~CBStr();
  operator BSTR() { return m_bstr; };
  operator BSTR*() { return &m_bstr; };
  operator CString();
  BSTR m_bstr;
};

CBStr::CBStr(LPCTSTR lpszValue)
{
  USES_CONVERSION;

  m_bstr = lpszValue ? ::SysAllocString(T2COLE(lpszValue)) : NULL;
}

CBStr::~CBStr()
{
  if (m_bstr)
    ::SysFreeString(m_bstr);
}

CBStr::operator CString()
{
  CString str;

  WideCharToMultiByte(CP_ACP, 0, (LPCWSTR) m_bstr, -1,
    str.GetBuffer(SS_MAX_STRING),
    SS_MAX_STRING, NULL, NULL);

  str.ReleaseBuffer();

  return str;
}

// Here's how to you it

CBStr bstr(_T("Test test"));
CString str;

str = bstr;

Good Luck,
Steve


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now