Solved

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

Posted on 2002-03-28
2
518 Views
Last Modified: 2013-11-25
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
Comment
Question by:goodami
2 Comments
 
LVL 1

Accepted Solution

by:
Robalitoru earned 20 total points
ID: 6903092
Try this:
        USES_CONVERSION;
        *pVal=A2BSTR( yourChar );

0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 6905049
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question