?
Solved

Cast from LPSTR to BSTR

Posted on 2003-10-30
10
Medium Priority
?
2,082 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

752 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