How To Use BSTR, VARIANT, SAFEARRAY Types in Custom Interface

Last reviewed: November 6, 1996
Article ID: Q158723
The information in this article applies to:
  • Microsoft COM libraries included with: Microsoft Windows NT 4.0 Microsoft Windows NT 3.51
  • Windows 95 without DCOM
  • Windows 95 with DCOM

SUMMARY

On platforms that do not support DCOM, such as Windows 95 without DCOM and versions of NT before NT 4.0, MIDL cannot generate marshaling code for custom interfaces that use the VARIANT, BSTR, or SAFEARRAY types. MIDL can generate marshaling code for such custom interfaces on platforms that support DCOM, such as Windows 95 with DCOM, NT 4.0 and later versions. This marshaling code can only be used on DCOM platforms; it cannot be used on platforms that do not support DCOM.

MORE INFORMATION

Marshaling code for the VARIANT, BSTR, and SAFEARRAY types was available on platforms that do not support DCOM for use by the OLE Automation marshaler. However, this code was not exposed to other users, such as MIDL. Consequently, MIDL cannot generate marshaling code on these platforms for custom interfaces that use these types unless you provide your own marshaling code for these types. On platforms that support DCOM, the system providing marshaling code for these types is made available to MIDL.

If your custom interface uses BSTR, VARIANT, or SAFEARRAY, and if you want to use your custom interface marshaling code only on a DCOM platform, you can use MIDL generated marshaling code. You will need to use -D_WIN32_DCOM as a C compiler flag in the build process of the MIDL generated code. Otherwise, a compiler error will be generated that indicates that your code will compile only if you mark it as runable only on a DCOM platform.

If your custom interface uses BSTR, VARIANT, or SAFEARRA, and if you want to use your custom interface marshaling code on platforms that do not support DCOM, consider the following workarounds:.

  • Use LPSTR or LPWSTR instead of BSTR to pass strings. MIDL can
generate code to marshal LPSTR and LPWSTR on all platforms. Use MIDL arrays instead of SAFEARRAY. Replace VARIANT with simple types or your own structure or union.
  • Use the OLE Automation marshaler to marshal your custom interface
instead of using MIDL-generated marshaling code. The custom interface must be constrained to the OLE Automation-compatible types for this. The OLE Automation marshaler can marshal VARIANT and BSTR on all platforms.

REFERENCES

For more information, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q139074
   TITLE     : SAMPLE: VTBLBIND: Marshaling Using an OLE Automation
               Marshaler

   ARTICLE-ID: Q139072
   TITLE     : Using OLE Automation Marshaler for 16/16 & 16/32 COM
               Interface


Additional reference words: 2.00 4.00 OLE2 OLE COM DCOM kbdss
KBCategory: kbole kbhowto
KBSubcategory: LeTwoArc



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: November 6, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.