FIX: Tlbimp.exe Generates Interface Wrappers that Cannot Be Used (318608)
The information in this article applies to:
- Microsoft Windows .NET Framework 1.1
- Microsoft .NET Framework 1.0
This article was previously published under Q318608 SYMPTOMS In a .NET application, if you call a COM function that
accepts a pointer to a COM interface as its parameter, and you then pass a .NET
class that implements a wrapper interface that is created around the COM
interface to the function, the function call is unsuccessful and you receive
the following error message: An unhandled exception of
type 'System.NullReferenceException' occurred in YourApp.exe
Additional information: Object reference not set to an instance of an object.
CAUSE The wrapper class that the Type Library Importer utility
(Tlbimp.exe) generates for the COM interface changes the COM function
definition so that the function accepts a .NET wrapper interface instead of the
actual COM interface pointer. Note that Tlbimp.exe only does this for the
default interface for coclasses. If you use an interface that is not the
default interface for a coclass, the application works as expected.
RESOLUTION To work around the problem, change the wrapper assembly so
that the COM function takes a pointer to the actual COM interface instead of to
a wrapper interface. To modify the wrapper, follow these steps:
- Follow these steps to use the MSIL Disassembler
(Ildasm.exe) to disassemble the wrapper dynamic-link library (DLL):
- From a .NET command prompt, run ILDASM.
- On the File menu, click Open. Open the wrapper DLL that is created by using
Tlbimp.exe.
- On the File menu, click Dump. Click OK in the dialog box that appears. Specify a file name, and then
click OK.
- In a text editor, modify the contents of the intermediate
language that is generated.
- Search for the function that you want, and then change the
parameter list so that it takes the actual COM interface. In the intermediate
language code, underscore characters (_) typically precede the name of the COM
interfaces. Therefore, you must type an underscore (_) before the interface
name in the function definition. For example, change the following
function([in][out] class IBug.myinterface& marshal( interface) func) runtime managed internalcall
to:
function([in][out] class IBug._myinterface& marshal( interface) func) runtime managed internalcall
- Save the changes to the file.
- Run the following command from a .NET command prompt to use
ILASM to reassemble the intermediate language as a DLL:
ilasm /dll <.il file> /out=<output .dll file>
- In your .NET application, remove the reference to the old
DLL and add a reference to the new wrapper DLL that you just
created.
STATUS This bug
was corrected in .NET Framework (2003|1.1).
REFERENCESFor additional information, click the
article numbers below to view the articles in the Microsoft Knowledge Base: 310674 HOW TO: Add References to a Managed Visual C++ Project
318466 Tlbimp.exe Cannot Handle Coclasses That List Source Interfaces That Are Defined in a Different Type Library
Modification Type: | Minor | Last Reviewed: | 5/28/2003 |
---|
Keywords: | kbfix kbbug KB318608 |
---|
|