About Unicode 
Author Message
 About Unicode

Hi, All!
I try to understand programming with Unicode so I have 2 questions.
1. In a sample I saw that for conversion from BSTR to ANSI was used W2A ATL
macros.
But I think that if OLE2ANSI flag is defined  BSTR will points to a char.
Does the macros (W2A ) work right in that case? Or using of OLE2A macros is
more correct.
2. Is it correct to pass BSTR string to functions which are needed LPOLESTR
parameters?

Best Regards,
Basis



Fri, 10 Oct 2003 04:35:22 GMT  
 About Unicode
Hello,

Quote:
> 1. In a sample I saw that for conversion from BSTR to ANSI was used W2A
ATL
> macros.
> But I think that if OLE2ANSI flag is defined  BSTR will points to a char.
> Does the macros (W2A ) work right in that case? Or using of OLE2A macros
is
> more correct.

Use OLE2A.

Quote:
> 2. Is it correct to pass BSTR string to functions which are needed
LPOLESTR
> parameters?

No, it's incorrect. Usually it works in case of [in] LPOLESTR parameter and
if BSTR string is null terminated one. e.g:

HRESULT Foo1([in] LPOLESTR pwsz);
BSTR bsData = SysAllocString(OLESTR("Test"));
Foo1(bsData) ; // it will work.

HRESULT Foo2([in, out] LPOLESTR* ppwsz);
BSTR bsData = SysAllocString(OLESTR("Test"));
Foo2(&bsData) ; // it will crash, in case of memory reallocation by Foo2, as
BSTR and OLESTR use different memory allocation routines.

HRESULT Foo3([in] LPOLESTR pwsz);
// bsData contains binary data, it's not null terminated string
BSTR bsData = SysAllocStringByteLen(pData, nDataLen);
Foo3(bsData) ; // bsData will be partially marshaled, the BSTR's data after
first NULL char will be lost. It's also possible that bsData doesn't contain
NULL character at all, then OLESTR string will have unpredictable size. In
the example above it always has NULL character as SysAllocStringByteLen adds
it.

--
Best regards,
Vadim Melnik,
{*filter*} Consultant.
_____________________________
http://www.*-*-*.com/



Sat, 11 Oct 2003 00:26:20 GMT  
 About Unicode
OLECHAR is char under Win16 and Mac, wchar_t under Win32. If you only
develop for Win32 (as most of us do), you can assume OLECHAR == WCHAR.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

Quote:

> Hi, All!
> I try to understand programming with Unicode so I have 2 questions.
> 1. In a sample I saw that for conversion from BSTR to ANSI was used W2A
ATL
> macros.
> But I think that if OLE2ANSI flag is defined  BSTR will points to a char.
> Does the macros (W2A ) work right in that case? Or using of OLE2A macros
is
> more correct.
> 2. Is it correct to pass BSTR string to functions which are needed
LPOLESTR
> parameters?

> Best Regards,
> Basis



Sat, 11 Oct 2003 05:01:32 GMT  
 About Unicode


Quote:
> OLECHAR is char under Win16 and Mac, wchar_t under Win32. If you only
> develop for Win32 (as most of us do), you can assume OLECHAR == WCHAR.

In general it is not right. See the declaration of OLECHAR

#if defined(_WIN32) && !defined(OLE2ANSI)
typedef WCHAR OLECHAR;
#else
typedef char      OLECHAR;



Sat, 11 Oct 2003 22:41:22 GMT  
 About Unicode
Thank you for examples. But this subject is not clear for me yet.
How can I find out when I should allocate memory for LPOLESTR parameter and
when not? For BSTR there is a rule: if it is [in] parameter you should
allocate memory. But for LPOLESTR I think it doesn't work. Because there is
a lot of samples like that Foo(OLESTR("A string"));
For example There is HRESULT ICreateErrorInfo::SetDescription(LPCOLESTR
szDescription) method in Automation. But there is no information in MSDN
about should I allocate memory for szDescription or not.

And the second question. What memory allocation routines exist for LPOLESTR.

Best regards,
Basis


Hello,

Quote:

> > 1. In a sample I saw that for conversion from BSTR to ANSI was used W2A
> ATL
> > macros.
> > But I think that if OLE2ANSI flag is defined  BSTR will points to a
char.
> > Does the macros (W2A ) work right in that case? Or using of OLE2A macros
> is
> > more correct.

> Use OLE2A.

> > 2. Is it correct to pass BSTR string to functions which are needed
> LPOLESTR
> > parameters?

> No, it's incorrect. Usually it works in case of [in] LPOLESTR parameter
and
> if BSTR string is null terminated one. e.g:

> HRESULT Foo1([in] LPOLESTR pwsz);
> BSTR bsData = SysAllocString(OLESTR("Test"));
> Foo1(bsData) ; // it will work.

> HRESULT Foo2([in, out] LPOLESTR* ppwsz);
> BSTR bsData = SysAllocString(OLESTR("Test"));
> Foo2(&bsData) ; // it will crash, in case of memory reallocation by Foo2,
as
> BSTR and OLESTR use different memory allocation routines.

> HRESULT Foo3([in] LPOLESTR pwsz);
> // bsData contains binary data, it's not null terminated string
> BSTR bsData = SysAllocStringByteLen(pData, nDataLen);
> Foo3(bsData) ; // bsData will be partially marshaled, the BSTR's data
after
> first NULL char will be lost. It's also possible that bsData doesn't
contain
> NULL character at all, then OLESTR string will have unpredictable size. In
> the example above it always has NULL character as SysAllocStringByteLen
adds
> it.

> --
> Best regards,
> Vadim Melnik,
> {*filter*} Consultant.
> _____________________________
> http://www.*-*-*.com/



Sat, 11 Oct 2003 23:18:03 GMT  
 About Unicode
Do you think your program will work if you manually define OLE2ANSI
under WIN32??? Access Violation is about guaranteed...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

Quote:


> > OLECHAR is char under Win16 and Mac, wchar_t under Win32. If you only
> > develop for Win32 (as most of us do), you can assume OLECHAR == WCHAR.

> In general it is not right. See the declaration of OLECHAR

> #if defined(_WIN32) && !defined(OLE2ANSI)
> typedef WCHAR OLECHAR;
> #else
> typedef char      OLECHAR;



Sun, 12 Oct 2003 02:56:01 GMT  
 About Unicode

Quote:
> Thank you for examples. But this subject is not clear for me yet.
> How can I find out when I should allocate memory for LPOLESTR parameter
and
> when not? For BSTR there is a rule: if it is [in] parameter you should
> allocate memory. But for LPOLESTR I think it doesn't work. Because there
is
> a lot of samples like that Foo(OLESTR("A string"));

For LPOLESTR there is the same rule - you should allocate it for [in]
parameter.  It'll work independently of allocation type - new, malloc,
_alloca, CoTaskMemAlloc, SysAllocString etc..

Quote:
> For example There is HRESULT ICreateErrorInfo::SetDescription(LPCOLESTR
> szDescription) method in Automation. But there is no information in MSDN
> about should I allocate memory for szDescription or not.

What is the problem?

OLECHAR wszDesc[] = OLESTR("My Description");
pei->SetDescription(wszDesc);

Quote:
> And the second question. What memory allocation routines exist for

LPOLESTR.
There is default task memory allocator in OLE, CoGetMalloc returns it, see
IMalloc for more details. Also look at convenient wrappers for it such as
CoTaskMemAlloc/CoTaskMemFree/CoTaskMemRealloc.  You should use it for [out]
parameters.

Cheers,
Vadim.

Quote:

> Best regards,
> Basis


> Hello,

> > > 1. In a sample I saw that for conversion from BSTR to ANSI was used
W2A
> > ATL
> > > macros.
> > > But I think that if OLE2ANSI flag is defined  BSTR will points to a
> char.
> > > Does the macros (W2A ) work right in that case? Or using of OLE2A
macros
> > is
> > > more correct.

> > Use OLE2A.

> > > 2. Is it correct to pass BSTR string to functions which are needed
> > LPOLESTR
> > > parameters?

> > No, it's incorrect. Usually it works in case of [in] LPOLESTR parameter
> and
> > if BSTR string is null terminated one. e.g:

> > HRESULT Foo1([in] LPOLESTR pwsz);
> > BSTR bsData = SysAllocString(OLESTR("Test"));
> > Foo1(bsData) ; // it will work.

> > HRESULT Foo2([in, out] LPOLESTR* ppwsz);
> > BSTR bsData = SysAllocString(OLESTR("Test"));
> > Foo2(&bsData) ; // it will crash, in case of memory reallocation by
Foo2,
> as
> > BSTR and OLESTR use different memory allocation routines.

> > HRESULT Foo3([in] LPOLESTR pwsz);
> > // bsData contains binary data, it's not null terminated string
> > BSTR bsData = SysAllocStringByteLen(pData, nDataLen);
> > Foo3(bsData) ; // bsData will be partially marshaled, the BSTR's data
> after
> > first NULL char will be lost. It's also possible that bsData doesn't
> contain
> > NULL character at all, then OLESTR string will have unpredictable size.
In
> > the example above it always has NULL character as SysAllocStringByteLen
> adds
> > it.

> > --
> > Best regards,
> > Vadim Melnik,
> > {*filter*} Consultant.
> > _____________________________
> > http://www.*-*-*.com/



Sun, 12 Oct 2003 04:09:19 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. UNICODE, programs using unicode

2. how to get unicode string from database in an none-unicode project

3. Unicode Edit box in non-unicode application?

4. Using Unicode DLL in Non-Unicode Exe

5. Bug in SQL Server 2000 ODBC driver when inserting unicode data in a non-unicode long varchar (text) column

6. Help ! Unicode to Multi byte and back to Unicode ?

7. LDAP: ldap_get_option for Hostname fails - UNICODE, non UNICODE both!

8. Unicode problems(Sending a Unicode String from VB to a dll)...

9. Urgent: need Unicode basics! Any Unicode Guru?

10. About Unicode Text Read

11. Unicode Problems with Win98

12. Unicode-EBCDIC

 

 
Powered by phpBB® Forum Software