?
Solved

Converting data types...

Posted on 1999-01-19
10
Medium Priority
?
844 Views
Last Modified: 2008-02-20
To convert a BSTR to char type, I was told to use:

// bstrTEXT comes in through function (e.g. function( BSTR bstrTEXT ))
char *charTEXT;
*charTEXT = ConvertBSTRToString(bstrTEXT);

But the compiler tells me this (am I using the right function to convert BSTR to char???):

error C2065: 'ConvertBSTRToString' : undeclared identifier

Also, when coding:

BSTR bstrSomething;
bstrSomething = "Some text...";

I get these errors during compile-time:

error C2015: too many characters in constant
error C2440: '=' : cannot convert from 'char [5]' to 'unsigned short *'

Thanks...
0
Comment
Question by:loyaliser
[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
  • 5
  • 5
10 Comments
 
LVL 3

Accepted Solution

by:
stefanr earned 200 total points
ID: 1184538
I think the function should be
void AFXAPI AfxBSTR2CString(CString* pStr, BSTR bstr);
in afxpriv2.h (VC++ 6.0 SP1)
To convert manually, use

BSTR bstr = SysAllocString("Some text");

The online help for SysAllocString says

BSTR SysAllocString(OLECHAR FAR* sz);

Allocates a new string and copies the passed string into it. Returns Null if there is insufficient memory, and if Null, Null is passed in.

Parameter
sz
A zero-terminated string to copy. The sz parameter must be a Unicode string in 32-bit applications, and an ANSI string in 16-bit applications.

Return Value
If successful, points to a BSTR containing the string. If insufficient memory exists or sz was Null, returns Null.

Comments
You can free strings created with SysAllocString using SysFreeString.

If you are using Unicode, then according to the help cited above you should write

BSTR bstr = SysAllocString(L"Some text");
0
 
LVL 3

Expert Comment

by:stefanr
ID: 1184539
Sorry, I mean of course if you are using a 32-bit application, you should write L"Some text" as the SysAllocString parameter.
0
 
LVL 1

Author Comment

by:loyaliser
ID: 1184540
thanx...
0
Independent Software Vendors: 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!

 
LVL 1

Author Comment

by:loyaliser
ID: 1184541
What about converting from BSTR to char?? I am using:

*charTEXT = ConvertBSTRToString(bstrTEXT);

but the compiler says:

error C2065: 'ConvertBSTRToString' : undeclared identifier
0
 
LVL 3

Expert Comment

by:stefanr
ID: 1184542
I think that the WideCharToMultiByte function is the one to use when converting from BSTR to char...
0
 
LVL 1

Author Comment

by:loyaliser
ID: 1184543
WideCharToMultiByte is not really working out for me... how do I concatenate a BSTR to a char [] data type:

char charData[50];
BSTR bstrData;

for example, strcat(charData, bstrData) - is there a similar function to strcat?

Thanks again...
0
 
LVL 3

Expert Comment

by:stefanr
ID: 1184544
Wouldn't this work?

char szBstr[256] = { 0 };

if (0 == WideCharToMultiByte(CP_ACP, 0, bstrData, -1, szBstr, sizeof(szBstr), NULL, NULL))
{
   cerr << "Translation failed. Error Code "<< GetLastError() << endl;
}
else
{
   char szData[50] = { 0 };
   . . .
   strcat(szData, szBstr);
}

0
 
LVL 1

Author Comment

by:loyaliser
ID: 1184545
Yeah, that worked... thanks again!
0
 
LVL 1

Author Comment

by:loyaliser
ID: 1184546
I hope you don't mind, but what would be the code for doing it the other way around... char [] to BSTR?

Thanks...
0
 
LVL 3

Expert Comment

by:stefanr
ID: 1184547
char szData[50] = { 0 };
   . . .
wchar_t lszData[50] = { 0 };
if (0 == MultiByteToWideChar(CP_ACP, 0, szData, -1, lszData, sizeof(lszData)/sizeof(wchar_t)))
{
   cerr << "Conversion to wide char string failed. Error Code " << GetLastError() << endl;
}
else
{
   BSTR bstrData = SysAllocString(lszData);
   if (NULL == bstrData)
   {
      cerr << "Conversion to BSTR string failed. Error Code " << GetLastError() << endl;

   }
   else
   {
      . . .
      SysFreeString(bstrData); // Don't forget to free the string when done with it.
      bstrData = NULL;
   }
}
0

Featured Post

Industry Leaders: 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

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

801 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