Convert string

How can I convert a string to LPCOLESTR?
closed account (48T7M4Gy)
Maybe self-help with Google??

https://www.experts-exchange.com/questions/10036788/LPCOLESTR.html
I've searched all over google and can't find anything. That website says to do this, but I need to convert a string variable to LPCOLESTR.
1
2
LPCOLESTR myOleStr;
myOleStr = OLESTR("This is an OLESTR");

Have you tested if myOlestr = OLESTR( myString); or myOlestr = OLESTR(myString.c_str()); works?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <iomanip>

#include <comutil.h> // #include for _bstr_t
#pragma comment (lib, "comsuppw.lib" ) // link with "comsuppw.lib" (or debug version: "comsuppwd.lib")

void test_it( LPCOLESTR olestr )
{
    std::wcout << "got an LPCOLESTR (alias for const BSTR): " << std::quoted(olestr) << L'\n' ;
    const DWORD* plen = reinterpret_cast< const DWORD* >(olestr) - 1 ;
    std::cout << "bytes allocated excluding the terminating double null: " << *plen << "\n\n" ;
}

int main()
{
      const std::string str = "Hello World!" ;
      
      // _bstr_t is a C++ wrapper over BSTR (Ole String)
      // for an overview of BSTR, see: https://msdn.microsoft.com/en-us/library/ms221069.aspx
      // constructor converts from narrow char to wide char and then calls ::SysAllocString 
      // its destructor will free the resources with ::SysFreeString
      // https://msdn.microsoft.com/en-us/library/zthfhkd6.aspx
      // https://msdn.microsoft.com/en-us/library/windows/desktop/ms221458(v=vs.85).aspx
      _bstr_t bstr = str.c_str() ; // constructor accepts const char*
    
      // implicit converstion from _bstr_t to LPCOLESTR (BSTR)
      test_it(bstr) ;
    
      // _bstr_t provides the expected string manipulation support
      bstr = "This is the new string!" ; 
      test_it(bstr) ;
    
      bstr += " (append some more characters to it)" ;
      test_it(bstr) ;
}

http://rextester.com/QTDD95158
Okay that works. Thank you!
Topic archived. No new replies allowed.