PRB: Visual Basic and Visual Basic for Applications Do Not Understand IUnknown** Data Type in MIDL Type Library
This article was previously published under Q194913 On This PageSYMPTOMS
If you build a Component Object Model (COM) component in C/C++ that takes or returns an IUnknown** data type and uses an Interface Definition Language (IDL) file and the Microsoft Interface Definition Language (MIDL) compiler to generate the type library, you find that neither Visual Basic nor Visual Basic for Applications (VBA) display the type correctly in the Visual Basic Object Browser, and indicate that the type is UNKNOWN. When you call the C/C++ component from Visual Basic, you may see the following run-time error:
Run-time error: 458
This error message can occur during cross-apartment calls to the interface. At other times, the method call succeeds even though Visual Basic is unsure of the data type.Variable uses a type not supported in Visual Basic In addition, if you use the Implements keyword to implement the C/C++ interface in a Visual Basic class, you receive the following compile-time error message:
Bad interface for Implements: method uses type that is not supported by Visual Basic.
You cannot implement an interface with an UNKNOWN type.However, a similarly written Visual Basic component that uses the hidden IUnknown data type (which equates to an IUnknown** in C/C++) displays correctly in the Object Browser and can be called by another Visual Basic class or used by Visual Basic Implements without error. CAUSE
MIDL-generated type libraries represent an IUnknown** parameter as a VT_UNKNOWN in the TYPEDESC for the ELEMDESC struct that describes the parameter. Although Visual Basic can work with VARIANTs of type VT_UNKNOWN, it does not expect this type for a parameter description in a type library. Consequently, it treats the parameter as UNKNOWN, and displays one of the error messages in the "Symptoms" section of this article. Visual Basic and VBA both use the CreateTypeLib2 API function (not MIDL) to generate the type library when they build their own COM components, and they can define a parameter as being the type IUnknown** by using a reference to an external type library (stdole2.tlb) and a ITypeInfo to describe the data type. The reference points to the actual description of IUnknown instead of relying on the VT_UNKNOWN VARTYPE. RESOLUTION
Because programmers are not allowed to use an IUnknown** data type natively in Visual Basic and VBA, components that are designed to work in these programming environments should avoid using this type when possible. Two common reasons why you might have chosen to use IUnknown in your component description (but you don't actually need to) are:
While you can qualify data types in MIDL, the MIDL compiler automatically converts any qualified reference to IUnknown back to a VT_UNKNOWN VARTYPE in the type library description. This simplifies the library for remote procedure calls (RPC) and DCOM, but it prevents you from using MIDL to generate libraries that use a Visual Basic- or Visual Basic for Applications-compatible IUnknown** type. Instead, you need to use MkTypLib and an ODL file to build the type library, or create the library programmatically with the CreateTypeLib2 API. See the "More Information" section for details. STATUS
This problem is a compatibility issue between Microsoft Visual Basic and the Microsoft MIDL compiler.
MORE INFORMATIONSteps to Reproduce Behavior
Problems with the Use of MkTypLibMicrosoft has standardized on the MIDL compiler for all new COM and COM+ applications, and encourages developers to use MIDL whenever possible.Developers who do use MkTypLib to work around this issue should be aware that MkTypLib does not allow you to compile an interface with the qualified stdole.IUnknown** type if the interface is marked "oleautomation compatible." You need to remove the oleautomation attribute to compile the type library. However, if you remove oleautomation, you are not able to use the universal marshaller (typelib marshalling) to remote the interface across contexts. So, in addition to using an MkTypLib library for your type information, you need to build a proxy DLL (or custom marshal with IMarshal) to handle any remoting of the interface. Furthermore, MkTypLib offers only a limited set of functionality in defining your interfaces. If your project requires IDL-specific definitions (such as wire_marshal, iid_is, asynch, and so forth) or the __int64 data type, you cannot use MkTypLib. You need to restructure the interface description as outlined in the "Resolution" section of this article, or build your own library with the CreateTypeLib2 API. REFERENCES
For more information on using MIDL or MkTypLib, see the "Automation" section in the MSDN library on the following Microsoft Web site at:
http://msdn.microsoft.com/en-us/library/aa366797(VS.85).aspx (http://msdn.microsoft.com/en-us/library/aa366797(VS.85).aspx) APPLIES TO
| Article Translations
|

Back to the top
