Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Converting data types...

Posted on 1999-01-19
10
Medium Priority
?
846 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
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!

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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

610 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