Wiki Home

VFPStringto BSTRConversion


Namespace: VFP
Good Evening Everybody!

I need to pass a parameter to a method of the COM object written in C++.
It expects a BSTR data type.
How could I convert a regular VFP string into BSTR?

Any suggestions would be greatly appreciated.

Thanks.

Rosty
I have had the same issue passing strings from VFP into the WIN API, or COM objects not written in VFP. Take a look at the STRCONV function and see if it helps. It will convert VFP strings to unicode. - Shawn White

It seems you'll need to use some Win 32 Api functions to accomplish this. Here's some references that should help:
MSDN: String Manipulation Functions
MSDN: Unicode Conversion
MSDN: MultiByteToWideChar
(Most of the text of the above articles is copied below. Anyone want to massage it into a working VFP example? I don't have time now.)

String Manipulation Functions
To handle strings that are allocated by one component and freed by another, Automation defines a special set of functions. These functions use the following data type:

typedef OLECHAR * BSTR;
These strings are zero-terminated, and in most cases they can be treated just like OLECHAR* strings. However, you can query a BSTR for its length rather than scan it, so it can contain embedded null characters. The length is stored as a 32-bit integer at the memory location preceding the data in the string. Instead of reading this location directly, applications should use the string manipulation functions to access the length of a BSTR.

In 32-bit OLE, BSTRs use Unicode like all other strings in 32-bit OLE. In 16-bit OLE, BSTRs use ANSI. Win32 provides Multi Byte To Wide CharOffsite link to http://www.news2news.com/vfp/?function=557
and Wide Char To Multi ByteOffsite link to http://www.news2news.com/vfp/?function=488
to convert ANSI strings to Unicode, and Unicode strings to ANSI.

Multi Byte To Wide CharOffsite link to http://www.news2news.com/vfp/?function=557
The Multi Byte To Wide CharOffsite link to http://www.news2news.com/vfp/?function=557
function maps a character string to a wide-character (Unicode) string. The character string mapped by this function is not necessarily from a multibyte character set.

int Multi Byte To Wide CharOffsite link to http://www.news2news.com/vfp/?function=557
(
UINT CodePage, // code page
DWORD dwFlags, // character-type options
LPCSTR lpMultiByteStr, // string to map
int cbMultiByte, // number of bytes in string
LPWSTR lpWideCharStr, // wide-character buffer
int cchWideChar // size of buffer
);
Parameters
CodePage
[in] Specifies the code page to be used to perform the conversion. This parameter can be given the value of any code page that is installed or available in the system. You can also specify one of the values shown in the following table. Value Meaning
CP_ACP ANSI code page
CP_MACCP Macintosh code page
CP_OEMCP OEM code page
CP_SYMBOL Windows 2000: Symbol code page (42)
CP_THREAD_ACP Windows 2000: The current thread's ANSI code page
CP_UTF7 Windows NT 4.0 and Windows 2000: Translate using UTF-7
CP_UTF8 Windows NT 4.0 and Windows 2000: Translate using UTF-8. When this is set, dwFlags must be zero.


dwFlags
[in] Indicates whether to translate to precomposed or composite-wide characters (if a composite form exists), whether to use glyph characters in place of control characters, and how to deal with invalid characters. You can specify a combination of the following flag constants. Value Meaning
MB_PRECOMPOSED Always use precomposed characters—that is, characters in which a base character and a nonspacing character have a single character value. This is the default translation option. Cannot be used with MB_COMPOSITE.
MB_COMPOSITE Always use composite characters—that is, characters in which a base character and a nonspacing character have different character values. Cannot be used with MB_PRECOMPOSED.
MB_ERR_INVALID_CHARS If the function encounters an invalid input character, it fails and Get Last ErrorOffsite link to http://www.news2news.com/vfp/?function=84
returns ERROR_NO_UNICODE_TRANSLATION.
MB_USEGLYPHCHARS Use glyph characters instead of control characters.


A composite character consists of a base character and a nonspacing character, each having different character values. A precomposed character has a single character value for a base/nonspacing character combination. In the character č, the e is the base character and the accent grave mark is the nonspacing character.

The function's default behavior is to translate to the precomposed form. If a precomposed form does not exist, the function attempts to translate to a composite form.

The flags MB_PRECOMPOSED and MB_COMPOSITE are mutually exclusive. The MB_USEGLYPHCHARS flag and the MB_ERR_INVALID_CHARS can be set regardless of the state of the other flags.

lpMultiByteStr
[in] Points to the character string to be converted.
cbMultiByte
[in] Specifies the size in bytes of the string pointed to by the lpMultiByteStr parameter, or it can be -1 if the string is null terminated.
If this parameter is -1, the function processes the entire input string including the null terminator. The resulting wide character string therefore has a null terminator, and the returned length includes the null terminator.

If this parameter is a positive integer, the function processes exactly the specified number of bytes. If the given length does not include a null terminator then the resulting wide character string will not be null terminated, and the returned length does not include a null terminator.

lpWideCharStr
[out] Points to a buffer that receives the translated string.
cchWideChar
[in] Specifies the size, in wide characters, of the buffer pointed to by the lpWideCharStr parameter. If this value is zero, the function returns the required buffer size, in wide characters, and makes no use of the lpWideCharStr buffer.
Return Values
If the function succeeds, and cchWideChar is nonzero, the return value is the number of wide characters written to the buffer pointed to by lpWideCharStr.

If the function succeeds, and cchWideChar is zero, the return value is the required size, in wide characters, for a buffer that can receive the translated string.

If the function fails, the return value is zero. To get extended error information, call Get Last ErrorOffsite link to http://www.news2news.com/vfp/?function=84
. Get Last ErrorOffsite link to http://www.news2news.com/vfp/?function=84
may return one of the following error codes:

ERROR_INSUFFICIENT_BUFFER
ERROR_INVALID_FLAGS
ERROR_INVALID_PARAMETER
ERROR_NO_UNICODE_TRANSLATION

Remarks
The lpMultiByteStr and lpWideCharStr pointers must not be the same. If they are the same, the function fails, and Get Last ErrorOffsite link to http://www.news2news.com/vfp/?function=84
returns the value ERROR_INVALID_PARAMETER.

The function fails if MB_ERR_INVALID_CHARS is set and encounters an invalid character in the source string. An invalid character is either, a) a character that is not the default character in the source string but translates to the default character when MB_ERR_INVALID_CHARS is not set, or b) for DBCS strings, a character which has a lead byte but no valid trailing byte. When an invalid character is found, and MB_ERR_INVALID_CHARS is set, the function returns 0 and sets Get Last ErrorOffsite link to http://www.news2news.com/vfp/?function=84
with the error ERROR_NO_UNICODE_TRANSLATION.

MAPI: For more information, see Syntax and Limitations for Win32 Functions Useful in MAPI Development.

Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Winnls.h; include Windows.h.
Library: Use Kernel32.lib.


SysAllocString
Allocates a new string and copies the passed string into it.

BSTR SysAllocString(
const OLECHAR * sz
);
Parameter
sz
A zero-terminated string to copy. The sz parameter must be a Unicode string in 32-bit applications, and an ANSI string in 16-bit applications. The argument sz may be NULL.
Return Value
If successful, points to a BSTR containing the string. Returns NULL if insufficient memory exists or if sz is NULL or zero-length.

Comments
You can free strings created with SysAllocString using SysFreeString.

Example
inline void CStatBar::SetText(OLECHAR * sz)
{
SysFreeString(m_bstrMsg); // Free previous string, if any.
m_bstrMsg = SysAllocString(sz);
}
Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in oleauto.h.
Library: Use oleaut32.lib.

SysFreeString
Deallocates a string allocated previously by SysAllocString, SysAllocStringByteLen, SysReAllocString, SysAllocStringLen, or SysReAllocStringLen.

VOID SysFreeString(
BSTR bstr
);
Parameter
bstr
Previously allocated BSTR. If bstr is NULL, the function simply returns.
Example
CStatBar::~CStatBar()
{
SysFreeString(m_bstrMsg);
}
Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in oleauto.h.
Library: Use oleaut32.lib.

These are some constants that are needed (From WINNLS.H):
//
// Code Page Default Values.
//
#define CP_ACP 0 // default to ANSI code page
#define CP_OEMCP 1 // default to OEM code page
#define CP_MACCP 2 // default to MAC code page
#define CP_THREAD_ACP 3 // current thread's ANSI code page
#define CP_SYMBOL 42 // SYMBOL translations

#define CP_UTF7 65000 // UTF-7 translation
#define CP_UTF8 65001 // UTF-8 translation

//
// MBCS and Unicode Translation Flags.
//
#define MB_PRECOMPOSED 0x00000001 // use precomposed chars
#define MB_COMPOSITE 0x00000002 // use composite chars
#define MB_USEGLYPHCHARS 0x00000004 // use glyph chars, not ctrl chars
#define MB_ERR_INVALID_CHARS 0x00000008 // error for invalid chars
Constants from WinError.h:
#define ERROR_NO_UNICODE_TRANSLATION 1113L
#define ERROR_INSUFFICIENT_BUFFER 122L // dderror
#define ERROR_INVALID_FLAGS 1004L
#define ERROR_INVALID_PARAMETER 87L // dderror
Category Code Samples Category C _ O _ M
( Topic last updated: 2006.07.11 02:37:29 PM )