Strings Passed to DLLs in Variants Aren't Converted to ANSI

Last reviewed: October 30, 1995
Article ID: Q138071
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 32-bit only, for Windows, version 4.0

SUMMARY

On Win32 Operating systems, if a variant containing a UNICODE string is passed to a DLL, Visual Basic version 4.0 (32-bit version) does not automatically convert it to ANSI, as it would a UNICODE string that is passed directly.

MORE INFORMATION

Visual Basic version 4.0 (32-bit version) stores strings as UNICODE internally. So, if you assign a string to a variant in Visual Basic version 4.0 (32-bit), it will be stored as UNICODE. Now, if you pass this to a 32-bit DLL function either by reference or by value, Visual Basic will not automatically convert the string contained in the variant to ANSI. If you are building a UNICODE DLL or using a pre-existing UNICODE DLL, this is not a problem. However, for custom ANSI DLLs, you will have to convert the string to ANSI yourself, using the WideCharToMultiByte Windows API function inside the DLL, in order to actually use the string meaningfully. For pre-built ANSI DLLs, you must convert the Visual Basic version 4.0 UNICODE string to ANSI, using the StrConv function, assign it to the Variant, and then pass it to the DLL. You could use the latter approach for your own custom ANSI DLLs too.


Additional reference words: 4.00 vb4win vb432
KBCategory: kbole
KBSubcategory: IAPOLE


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 30, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.