Solved

Cast from LPSTR to BSTR

Posted on 2003-10-30
10
2,026 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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
 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 199
GUI: DIalog Stacking and Popping in MS C++ 4 76
How to print into std::string like sprintf using C++11? 12 60
How do i run a c++ file? 15 29
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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 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