Solved

Converting data types...

Posted on 1999-01-19
10
835 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 134
why "." vs "->" 23 119
How to copy an image file into clipboard C/C++? 1 154
Embarcadero WebBroker REST server endpoint parameters 2 54
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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

22 Experts available now in Live!

Get 1:1 Help Now