Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-26
9
Medium Priority
?
1,870 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

572 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