Windows apps
Collapse the table of content
Expand the table of content

SysAllocStringByteLen Function

Takes an ANSI string as input, and returns a BSTR that contains an ANSI string. Does not perform any ANSI-to-Unicode translation.

BSTR SysAllocStringByteLen(  
  LPCSTR  psz,    
  unsigned int  len  


A zero-terminated string to copy, or NULL to keep the string uninitialized.


Number of bytes to copy from psz. A null character is placed afterwards, allocating a total of lenplus the size of OLECHAR bytes.

Allocates a new string of lenbytes, copies lenbytes from the passed string into it, and then appends a null character.

Points to a copy of the string, or NULL if insufficient memory exists.

This function is provided to create BSTRs that contain binary data. You can use this type of BSTR only in situations where it will not be translated from ANSI to Unicode, or vice versa.

For example, do not use these BSTRs between a 16-bit and a 32-bit application running on a 32-bit Windows system. The OLE 16-bit to 32-bit (and 32-bit to 16-bit) interoperability layer will translate the BSTR and corrupt the binary data. The preferred method of passing binary data is to use a SAFEARRAY of VT_UI1, which will not be translated by OLE.

If psz is Null, a string of the requested length is allocated, but not initialized. The string psz can contain embedded null characters, and does not need to end with a Null. Free the returned string later with SysFreeString.

© 2017 Microsoft