|
-
December 18th, 2003, 07:24 AM
#1
casting from BSTR* to stl string
Hello,
I know I asked about the other way...
Anyone can help me cast from BSTR* to std::string
Thanks
-
December 18th, 2003, 09:22 AM
#2
I think you mean convert, not cast. A BSTR* is a pointer, a std::string is an object. You can only cast a pointer type to another pointer type.
Anyway, shouldn't it be a BSTR, not BSTR*? A BSTR is a representation of an entire string, and a std::string represents an entire string.
Maybe you should clarify what you're trying to do.
Regards,
Paul McKenzie
-
December 18th, 2003, 09:31 AM
#3
What I'm trying to do
I have COM interface that is written in C++, but need to server VB & ASP clients
I want VB to use a specific function in the interface, after trying a few parameters type I saw VB must use BSTR* and not char* as my in parameters (Do you think I can use BSTR instead BSTR*?, then I need this conversion as well...)
My C++ interface after getting the params for VB is calling a c++ function that its parameters are string* (not string my mistake)
so I need to convert those VB BSTR* (or BSTR if it's possible) to string* so I could use my function
thanks
avi
-
December 18th, 2003, 10:18 AM
#4
Check the FAQ on converting from Unicode to ANSI and modify the code a little so that instead of char *'s you use std::strings. The thing is that BSTRs are Unicode (UTF16) and std::strings are ANSI, so it's more than just casting.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
December 18th, 2003, 12:28 PM
#5
Re: casting from BSTR* to stl string
Originally posted by avi123
Hello,
I know I asked about the other way...
Anyone can help me cast from BSTR* to std::string
Thanks
As mentioned, it's more of a conversion than casting.
You said you're using COM; you can use the USES_CONVERSION macros...
(rough, uncompiled, untested, but the general idea should be there...)
Code:
USES_CONVERSION;
char* psz = BSTR2A( yourbstr );
std::string thestring( psz );
or
Code:
USES_CONVERSION;
std::string thestring( BSTR2A( yourbstr ) );
Of course, use of the macros implies your BSTR doesn't contain embedded NULLs...
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
December 19th, 2003, 08:36 AM
#6
The W2A macros just call WideCharToMultiByte internally anyways and another thing to keep in mind is that they allocate the temporary ANSI string on the stack, so if the strings get large, don't use these macros and more important even, do not use these macros inside loops, otherwise you'll run out of stack space very quickly.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
December 19th, 2003, 03:37 PM
#7
Originally posted by Yves M
The W2A macros just call WideCharToMultiByte internally anyways and another thing to keep in mind is that they allocate the temporary ANSI string on the stack, so if the strings get large, don't use these macros and more important even, do not use these macros inside loops, otherwise you'll run out of stack space very quickly.
Right, thanks for mentioning that, Yves. If loops are necessary, one could potentially use a separate function to do the conversion. Nonetheless, a large string could still cause a 0xc00000fd.
If one is using VS.NET, CW2A and related classes are an interesting alternative to the USES_CONVERSION macros... With that family of classes, a stack overflow isn't a concern...
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
December 19th, 2003, 03:47 PM
#8
I just checked those macros out (part of ATL 7) and they suffer from one flaw. They assume that the resulting ANSI string has at most twice as many characters as the original Unicode string. This is fine in many cases, but when e.g. converting to UTF8 this can very easily break things. But well, for most ACPs it should be fine.
Code:
From ATL7
int nLengthW = lstrlenW( psz )+1;
int nLengthA = nLengthW*2;
// ...
::WideCharToMultiByte( nCodePage, 0, psz, nLengthW, m_psz, nLengthA, NULL, NULL )
// ...
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
December 19th, 2003, 10:47 PM
#9
Originally posted by Yves M
I just checked those macros out (part of ATL 7)
Which macros, Yves? The CW*, CT*, and CA* are (template) classes in ATL 7.x, are they not?
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
December 21st, 2003, 01:57 AM
#10
where is BSTR2A is decleraed, I mean what .h file do I need to include
I get the compilation error: d:\MyClass\MyClass.cpp(17): error C3861: 'BSTR2A': identifier not found, even with argument-dependent lookup
-
December 21st, 2003, 01:57 PM
#11
Originally posted by avi123
where is BSTR2A is decleraed, I mean what .h file do I need to include
I get the compilation error: d:\MyClass\MyClass.cpp(17): error C3861: 'BSTR2A': identifier not found, even with argument-dependent lookup
Atlconv.h, but you may wish to include atlbase.h.
Keeping in mind the aforementioned warning about embedded NULLs, perhaps W2CA would work better for you, since BSTR2A doesn't seem to exist. (I checked Google and got hits for it so I assumed it existed, rather than checking atlconv.h, which does not define a BSTR2A...)
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|