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
Solved

Converting data types...

Posted on 1999-01-19
10
840 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
  • 5
  • 5
10 Comments
 
LVL 3

Accepted Solution

by:
stefanr earned 50 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
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.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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 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.

808 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