From: Paul G. <pjg...@ie...> - 2003-04-15 18:53:49
|
Hi > Again I've looked through the archives, but not found anything. > > How do I convert a BSTR to a std::string? > > If I was using MSVC, I'd use USES_CONVERSION and W2A or CComBSTR. However, > non of these appear to be available in the Mingw Win32 library. I've played a bit more and I've come up with a solution that works, but I'm hoping someone can suggest a better cleaner way: class CharArray { public: explicit CharArray(unsigned long size) : m_pArray(0) { m_pArray = new char[size]; } ~CharArray() { delete []m_pArray; } public: char* Contents() { return m_pArray; } private: char *m_pArray; private: CharArray(const CharArray&); CharArray& operator=(const CharArray&); }; std::string BSTRToString(BSTR pBstr) { const unsigned long length = wcslen(pBstr); CharArray buffer(length); wcstombs(buffer.Contents(),pBstr,length); return buffer.Contents(); } Regards Paul Paul Grenyer Email: pjg...@ie... Web: www.paulgrenyer.co.uk What do you mean you've never heard of Karnataka? http://www.karnataka.org.uk |