Solved

Cast from LPSTR to BSTR

Posted on 2003-10-30
10
2,003 Views
Last Modified: 2011-09-20
Hi all,

   I want to assing a string of LPSTR type to BSTR.
As im new to VC++ plz give me all possible ways to do this.
I have tried with x= (BSTR) y;
But its not assinging it.

Thx in advance
satish jupalli
0
Comment
Question by:satishjupalli
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 3

Expert Comment

by:freewell
ID: 9655984
#include "afxpriv.h"

BOOL CAboutDlg::OnInitDialog()
{
      CDialog::OnInitDialog();

      USES_CONVERSION;
      CString str1("Test Conversion");
      BSTR bstr = T2OLE(str1);
      AfxMessageBox(OLE2T(bstr));

      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}
0
 

Author Comment

by:satishjupalli
ID: 9656200
Thx freewell,

     But im trying to do this conversion in DLL.
    In that getProperty method im trying to conert
   msgTo which is of type LPSTR. And i want to convert
msgTo to *pVal which is a BSTR type.
           
            USES_CONVERSION;
      *pVal= T2OLE(msgTo);  
Moreover i dount want to use any MFC related dll's for support.

Hope u got the propblem...

Thx in advance
0
 
LVL 3

Expert Comment

by:freewell
ID: 9656480

SysFreeString(pVal);   // Free previous string, if any.
pVal = SysAllocString(L"msgTo");

// Caller should check to see if pVal is null after calling this method

0
 

Author Comment

by:satishjupalli
ID: 9656535
Thx ,

  It seems u didnt get my problem ....

  Here is my code in brief...

  LPSTR msgTo= "catandRat";
  Now how do u want me to assign this msgTo to pVal.
 
  pVal =SysAllocString(???)

  how should i assign it...
 plz help me

 
0
 
LVL 9

Expert Comment

by:_ys_
ID: 9656740
>> i dount want to use any MFC related dll's for support.

What about the COM utility class _bstr_t. (#include <comutil.h>)

LPCTSTR szString = _T("string");
_bstr_t bstrWrapper (szString);

If you need the actual BSTR independent of the wrapper:
BSTR bstrString = bstrWrapper.Detach ( );

The resultant BSTR will have been SysAlloc'ed, and will need to be SysFree'ed. if you leave it in the wrapper, cleanup is automatic.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 9

Expert Comment

by:_ys_
ID: 9656743
My use of LPCTSTR illustrates that both char* and wchar_t* are accepted as parameters for _bstr_t construction.
0
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 9656780
The problem with SysAllocString() is that is takes a UNICODE (i.e. a WIDE-STRING) as input rather than an ANSI string, which is what you are starting with.  So you need a TWO step process to convert:

1) Turn your ANSI string into a UNICODE string.
2) Turn your UNICODE string into a BSTR.

Do it like this:

      LPSTR msgTo = "catandRat";  // Starting ANSI String

      // Convert to UNICODE using MultiByteToWideChar()

      // Get the needed size for the conversion buffer
      int iSizeNeeded = MultiByteToWideChar(CP_ACP, 0, msgTo, -1, NULL, 0);

      // Allocate a buffer of the required size
      WCHAR *wszUnicode = new WCHAR[iSizeNeeded];

      // Convert the ANSI string to UNICODE
      MultiByteToWideChar(CP_ACP, 0, msgTo, -1, wszUnicode, iSizeNeeded);

      // wszUnicode now contains a UNICODE string

      BSTR myBstr;
      myBstr = SysAllocString(wszUnicode);

      // Free the UNICODE string buffer
      delete [] wszUnicode;


      // When finished with it, don't forget to free the BSTR
      SysFreeString(myBstr);
0
 

Expert Comment

by:GiedriusS
ID: 9656794
the best way:

#include <atlstr.h>

...

BSTR bstr;
CString temp;
LPSTR lpstr;
temp = lpstr;
bstr = temp.AllocSysString();
....
0
 
LVL 32

Expert Comment

by:jhance
ID: 9656873
Of course the fact that the user here said:

"Moreover i dount want to use any MFC related dll's for support."

pretty well eliminates your suggestion.  Last time I checked, CString was an MFC class.

I personally DO use the MFC and/or ATL conversion macros.  But in those cases where they are not available or not desired, it's helpful to know the "hard" way to do it.  Hence, my suggestion to use MultiByteToWideChar() followed by SysAllocString().
0
 

Expert Comment

by:jbuote1
ID: 9663989
For what it's worth,
I too am writing a dll that needs to convert ANSI to Unicode, and Can't use MFC.....

JHance is correct....
There ARE several ways to do it, but the point is there....
I know of another way, but JHance summed it up REAL good.

Use that you should be fine.

BTW:
CString IS an MFC Class...
Therefore you can't use it.
Nice work JHance....
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

16 Experts available now in Live!

Get 1:1 Help Now