Solved

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

Posted on 2002-03-28
2
512 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now