PRB: Visual Basic Does Not Understand IUnknown** Type

ID: Q194913


The information in this article applies to:


SYMPTOMS

When writing a COM component in C++, it may be necessary to declare a function parameter as being the type IUnknown**. However, if the interface is defined in IDL and compiled with MIDL, Visual Basic will not understand the declaration and will display the type as "Unknown" in the Object Browser.


RESOLUTION

Visual Basic can accept parameters as type IUnknown**, but they must be qualified with the stdole library that defines the IUnknown interface (i.e., stdole.IUnknown**), and the type library should be compiled with MkTypLib instead of MIDL.

NOTE: Adding stdole.IUnknown** to your interface means that it no longer adheres to the strict standards of being OLE Automation compatible. For your library to compile correctly, you may need to remove any interface attributes that cause MkTypeLib to perform strong type checking, including the oleautomation, dual, and non-extensible keywords.


STATUS

This behavior is by design.


MORE INFORMATION

Steps to Reproduce Behavior

  1. Open Visual C++ and click New on the File menu. On the Projects tab select "Win32 Dynamic-Link Library" and name the project "MyUnknown." Keep the defaults in any dialogs that may appear.


  2. Once again, click New on the File menu and, on the Files tab, select "Text File." Name the file "MyUnknown.odl" and click OK.


  3. Add the following code to "MyUnknown.odl":
    
          [
             uuid(1A4C3A6A-50AE-11D1-BB71-00C04FAD8B08),
             version(1.1)
          ]
          library MyUnknown
          {
             importlib("StdOle2.tlb");
    
             [
                odl,
                uuid(1A4C3A68-50AE-11D1-BB71-00C04FAD8B08),
                hidden
             ]
             interface _CTUnknown : IUnknown {
                HRESULT GetIUnknown([out, retval] IUnknown** iu);
                HRESULT SetIUnknown([in, out] IUnknown** iu);
             };
    
             [
                uuid(1A4C3A69-50AE-11D1-BB71-00C04FAD8B08)
             ]
             coclass CTUnknown {
                [default] interface _CTUnknown;
             };
    
          };
     


  4. Press CTRL+F7 or select "Compile MyUnknown.odl" from the Build menu to build the type library.


  5. Open Visual Basic and select a new Standard EXE project. Choose References from the Project menu and click the Browse button. Find your compiled type library (MyUnknown.tlb) in the debug directory of your VC project and click OK. Make sure the library is checked in the References dialog and then click OK to close the References dialog.


  6. Open Visual Basic's Object Browser by pressing F2. From the drop-down on the upper-left portion of the Object Browser, select MyUnknown from the list. View the SetIUnknown and GetIUnknown function of the MyUnknown.CTUnknown class. You will notice that Visual Basic identifies the IUnknown** parameters as type "Unknown," and cannot read them.


  7. Close your Visual Basic project. Do not save changes.


  8. To correct the problem, return to Visual C++ and modify the MyUnknown.odl file by changing the GetIUnknown and SetIUnknown functions to read:
    
          HRESULT GetIUnknown([out, retval] stdole.IUnknown** iu);
          HRESULT SetIUnknown([in, out] stdole.IUnknown** iu);
     


  9. In the project workspace, click the "File View" tab. Double-click "MyUnknown Files" to expand the file tree. Right-click the "MyUnknown.odl" file and select Settings from the context menu.


  10. From the Project Settings dialog, choose the General Tab and check the option "Always use custom build step." Choose the Custom Build Tab and add the following to the Build command(s) line:
    mktyplib MyUnknown.odl /tlb "Debug\MyUnknown.tlb"
    Then add the following to the Output file(s) line and click OK:
    Debug\MyUnknown.tlb


  11. Now, rebuild your type library by pressing CTRL+F7 or selecting "Compile MyUnknown.odl" from the Build menu.


  12. Repeat steps 5 and 6 to verify that Visual Basic can now recognize the IUnknown** data types.



Keywords          : kbCOMt kbVBp kbVBp500 kbVBp600 kbVC500 kbVC600 kbCodeSam 
Version           : WINDOWS:5.0,6.0; WINNT:5.0,6.0
Platform          : WINDOWS winnt 
Issue type        : kbprb 

Last Reviewed: May 27, 1999