Solved

Cast from LPSTR to BSTR

Posted on 2003-10-30
10
2,056 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 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

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!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

717 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