?
Solved

converting string (BSTR) to char [ ], VB to C++

Posted on 2003-03-26
9
Medium Priority
?
1,802 Views
Last Modified: 2007-12-19
Hello experts,

I am new to COM programming, and I have encountered a problem which I am having troubles to resolve.

So I have a dll which has an external function that can be called from vb, and it expects
a string parameter (BSTR),

STDMETHODIMP MyClass::myMethod(BSTR input)
{
 //code
}

After I get the BSTR input from VB, I want to convert this string into unsigned char array[]
type to use for a private function.
Can anyone gimme a hand with this? an Example to do this would be prefect! thanks
 


0
Comment
Question by:WacKy
[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
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8211086
You could

#include <comdef.h>

STDMETHODIMP MyClass::myMethod(BSTR input)
{
 __bstr_t _bstr ( input);
 char* psz = ( char*) _bstr; // will call 'operator char*( ) const' and convert to char*
}


0
 
LVL 3

Expert Comment

by:ckaneta
ID: 8211326
Here's how I did it when I had to figure a way
probably not the most efficient method in the world, but it worked

for (DWORD c=0;c<SysStringLen(bstrResponse);c++){
 strResponse[c] = (char)bstrResponse[c];
 strResponse[c+1]='\0';
}
bstrResponse is a BSTR variable
strResponse is defined as char strResponse[256]
0
 

Author Comment

by:WacKy
ID: 8212052
Thanks to both jkr and ckaneta,
both examples worked fine, except I needed a char[] not a char*.

I changed my external function however, to:

STDMETHODIMP MyClass::myMethod(BSTR input, BSTR *output)
{
//get BSTR input, convert to unsinged char[], call
myFunction(unsigned char[]), convert from array[] to BSTR*
}

unsigned char[] MyClass::myFunction(unsigned char[])
{
//myFunction gets this Array, do some manipulation, and returns array[]
}

So can you guys show me how to convert from unsigned char[] to BSTR* so I can output this to VB? Thanks much
 
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 86

Accepted Solution

by:
jkr earned 200 total points
ID: 8212101
Sure. Just use

char* psz = "test";
__bstr_t _bstr ( psz );
BSTR bstr = _bstr.copy();
0
 

Expert Comment

by:Marius_Samoila
ID: 8212430
It seems that you are using ATL or MFC. If this is true, the shortest way is:


STDMETHODIMP MyClass::myMethod(BSTR input)
{
    USES_CONVERSION;
    unsigned char *array;
    array = (unsigned char*)OLE2A(input);

}


Your array is allocated on stack. You do not need to free anything.
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 8212685
Except the OLE2A isn't supposed to be used that way.  Microsoft had a long INFO: thing on it which I can't seem to locate.  Anyway, if I remember correctly the code with OLE2A does work, but only because the converted string doesn't get overwritten (which can happen in some circumstances).

The reccomended way to do it is with CW2A as described at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_atl_String_Conversion_Macros.asp

0
 

Author Comment

by:WacKy
ID: 8212884
thanks to all who's trying to help me.
jkr, I tried your last example, and I get a warning saying
"forcing value to bool true or false"



unsigned char array[256];
for (DWORD c=0; c<SysStringLen(input); c++)
{
     array[c] = (unsigned char)input[c];
     array[c+1]='\0';
}

unsigned char* psz = myFunction(array); //myFunction
//returns unsigned char *
_bstr_t _bstr(psz);
BSTR bstr = _bstr.copy();
*output= bstr;

//and my output string becomes -1 as a result of this, I lost all my collection of data inside the array?
 
0
 
LVL 1

Expert Comment

by:Hermetic
ID: 8214059
Try using wcstombs().  As long as it's not expected to be an an out parameter.
0
 

Author Comment

by:WacKy
ID: 8214742
thanks to everyone, but I think I should give points to jkr. =)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

770 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