• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2140
  • Last Modified:

Cast from LPSTR to BSTR

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
satishjupalli
Asked:
satishjupalli
  • 2
  • 2
  • 2
  • +3
1 Solution
 
freewellCommented:
#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
 
satishjupalliAuthor Commented:
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
 
freewellCommented:

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
satishjupalliAuthor Commented:
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
 
_ys_Commented:
>> 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
 
_ys_Commented:
My use of LPCTSTR illustrates that both char* and wchar_t* are accepted as parameters for _bstr_t construction.
0
 
jhanceCommented:
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
 
GiedriusSCommented:
the best way:

#include <atlstr.h>

...

BSTR bstr;
CString temp;
LPSTR lpstr;
temp = lpstr;
bstr = temp.AllocSysString();
....
0
 
jhanceCommented:
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
 
jbuote1Commented:
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now