BSTR, LPSTR conversion problem [April 26, 2004, 9:21]

zhaozj2021-02-16  62

BSTR, LPSTR conversion problem

BSTR BSTR;

LPSTR LPSTR;

Direct them directly

BSTR = (BSTR) LPSTR;

LPSTR = (LPSTR) BSTR;

It seems that it seems to be very reasonable, and this is actually wrong. They are all types of data, although it looks a string, but it should have no difference in integerging integer hardness into a string. Consider, the main should be different in memory. The explanation of LPSTR in MSDN is the string pointer ending with '/ 0', it should be

CHAR PSTR [] = "Hello"; the resulting string is the same. BSTR is used to pass the value between COM, a special set of memory management methods, and the demonstration methods in the MSDN are as follows:

· BSTR BSTRSTATUS = :: sysallocstring (l "some text");

:: sysfreeestring (bstrstatus);

When calling a function returns a BSTR, you must release the string. The same meaning, when a function returns a BSTR, it is not used to release the memory.

Again the problem of BSTR, LPSTR conversion

If BSTR = (BSTR) lpstr; (of course, BSTR = LPSTR; the compiler will prompt the error), the return BSTR will not be identified, but even if it is successful, the breakpoint is set, at this time, watch the Watch window is also seen. Come out, how to see, then say it later.

If lpstr = (lpstr) BSTR; if the BSTR value, LPSTR can see is the first character of BSTR (see in the debug window).

The method correctly converted between BSTR and LPSTR is to use BSTR's package _bstr_t.

There is such an operation overload in the class _bstr_t. It's really embarrassing, soon I know that these can be overloaded, I have always thought it is , -, *, / overload.

Operator const wchar_t * () const throw ();

Operator wchar_t * () const throw ();

Operator const char * () const throw (_COM_ERROR);

Operator char * () const throw (_COM_ERROR);

Plus the "Technical function" of _BSTR_T is allowed, so it can be solved.

LPSTR LPSTR = (LPSTR) _BSTR_T (BST); // LPSTR should be an operator with char *.

BSTR BSTR = _BSTR_T (LPSTR);

// I will not solve this, how is this, conversion between different types, there will be no warnings, nor don't be overloaded, it doesn't see it. Look at _bstr_t source code, still unclear, have time to say.

Anyway, it is right.

There is no value in BSTR, you can't see it directly, you can use _bstr_t. It seems that _BSTR_T is quite useful.

In addition, I think that there is a BSTR BSTRVAL in Variant; if the function returns is BSTRVAL, I don't know if I don't know :: sysfreestring is released?

转载请注明原文地址:https://www.9cbs.com/read-17205.html

New Post(0)