# Compiling and Registering a Type Library

In subsequent examples you will need to be able to compile type libraries and reference them from VB projects. In this example you will compile an IDL file using the Microsoft IDL compiler and register the resulting type library file with Windows, thereby allowing you to reference the type library in VB just like an ActiveX component. But first, you may require some essential background information. The MIDL compiler is a DOS based application. Because of this, you must make sure MIDL is running in a DOS shell with the proper environment variables set. MIDL must be able to locate additional files it requires to compile your IDL code. If your experience with DOS is limited this can be troublesome depending which Windows operating system your using and how you installed Visual Studio. So, the first few steps in this example are the steps I used to get my Windows 98 system ready to compile IDL files. If youre running Windows NT and you have properly installed the C++ portion of Visual Studio you will probably be able to go straight to step 5.

1. Locate a file with the name VCVARS32.BAT, normally located in C:\Program Files\Microsoft Visual Studio\VC98\Bin\. Visual Studio will have already included this file during installation. The contents of the batch file should look like this:

@echo off

remrem Root of Visual Developer Studio Common files.set VSCommonDir=C:\PROGRA~1\MICROS~4\COMMON




remrem Root of Visual Developer Studio installed files.remset MSDevDir=C:\PROGRA~1\MICROS~4\COMMON\msdev98remrem Root of Visual C++ installed files.remset MSVCDir=C:\PROGRA~1\MICROS~4\VC98remrem VcOsDir is used to help create either a Windows 95 or Windows NT specific path.remset VcOsDir=WIN95if "%OS%" == "Windows_NT" set VcOsDir=WINNTremecho Setting environment for using Microsoft Visual C++ tools.remif "%OS%" == "Windows_NT" set PATH=%MSDevDir%\BIN;%MSVCDir%\BIN;%VSCommonDir%\TOOLS\%VcOsDir%;%VSCommonDir%\TOOLS;%PATH%if "%OS%" == "" set PATH="%MSDevDir%\BIN";"%MSVCDir%\BIN";"%VSCommonDir%\TOOLS\%VcOsDir%";"%VSCommonDir%\TOOLS";"%windir%\SYSTEM";"%PATH%"set INCLUDE=%MSVCDir%\ATL\INCLUDE;%MSVCDir%\INCLUDE;%MSVCDir%\MFC\INCLUDE;%INCLUDE%set LIB=%MSVCDir%\LIB;%MSVCDir%\MFC\LIB;%LIB%set VcOsDir=set VSCommonDir=

1. Next create a shortcut on your desktop to COMMAND.COM. Name the shortcut Go MIDL.
2. Right mouse click over the shortcut and select to view the shortcuts properties. Select the Program tab and set the properties as follows: Cmd line: C:\WINDOWS\COMMAND.COM Working: "C:\Program Files\Microsoft Visual Studio\VC98" Batch file: BIN\VCVARS32.BAT
3. Now select the Memory tab and make sure the Initial environment: property is set to at least 2048.

When you click on this shortcut, you will be in a DOS shell that is properly set for compiling IDL files. The shortcut will run the VCVARS32.BAT file for you. This properly initializes the DOS environment variables for the MIDL compiler.

1. Next, copy the following IDL code, paste it into Notepad, and save the file as EX2.IDL:
// TypeLib : EX2.IDL (EX2)// TargetFile : EX2.TLB//// Version 1.0// - Author : Philip G. Fucich// - Completed: 01/01/2000// - Notes : Example 2 type library.// UUID Usage:// {C4408B80-EFC0-11D3-A88C-81F2B32E4671} : library EX2// {C4408B82-EFC0-11D3-A88C-81F2B32E4671} : typedef exSampleEnum// {C4408B84-EFC0-11D3-A88C-81F2B32E4671} : interface Box[uuid(C4408B80-EFC0-11D3-A88C-81F2B32E4671),version(1.0),helpstring("Example 2 Library")]library EX2{importlib("STDOLE2.TLB");typedef[uuid(C4408B82-EFC0-11D3-A88C-81F2B32E4671),helpstring("Sample enum declaration."),v1_enum,version(1.0)]enum exSampleEnum {[helpstring("Helpstring for exDefault.")]exDefault = 0,exOne = 1,exTwo = 2,} exSampleEnum ;[uuid(C4408B84-EFC0-11D3-A88C-81F2B32E4671),helpstring("A Box."),nonextensible,oleautomation,]interface Box : IUnknown {[helpstring("Box height.")][propget] HRESULT Height ([out, retval] long* PropData );[helpstring("Box width.")][propget] HRESULT Width ([out, retval] long* PropData );[helpstring("Box depth.")][propget] HRESULT Depth ([out, retval] long* PropData );};}
1. Copy or move EX2.IDL to the following directory:
C:\Program Files\Microsoft Visual Studio\VC98\
1. Now click on your Go MIDL shortcut.
2. In the DOS Window, type the following command:
bin\midl EX2.IDL

If all has gone well up to this point, you will have an EX2.TLB file. This file cannot be referenced in a VB project until it has been registered with Windows. The next steps will cover how to register your type library using some simple VB code.

1. Move the EX2.IDL and EX2.TLB files to a directory of your choice. For this example, Ill say its in C:\Example2\.
2. Open VB and create a new Standard EXE project.
3. Go the project references dialog and add a reference to TypeLib Information as shown here:

1. Click OK to return the to Form1 and add a command button to the form.
2. Double click on the command button and add the following code to the Command1_Click() event procedure:
Dim o_TypeLib As TypeLibInfoSet o_TypeLib = TLIApplication.TypeLibInfoFromFile("C:\Example2\EX2.TLB")o_TypeLib.RegisterMsgBox "The TypeLibrary is registered."
1. Now, run the project and click the Command1 button.

The EX2.TLB will now be registered with Windows and thereby ready to reference in a VB project. To check out the EX2.TLB, open a new Standard EXE project, go to the project references dialog, and add it as a reference. You will find it listed as Example 2 Library. When youve added the reference, open the VB Object Browser and select EX2 in the Project/Library combo box. Your Object Browser will look something like this:

posted on 2010-12-22 16:39  懒蜜蜂  阅读(281)  评论(0编辑  收藏