Solved

Converting data types...

Posted on 1999-01-19
10
832 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 clear a vector as well as how to detect empty vectors in C++.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now