


















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.
@echo off
rem
rem Root of Visual Developer Studio Common files.
set VSCommonDir=C:\PROGRA~1\MICROS~4\COMMON
rem
rem Root of Visual Developer Studio installed files.
rem
set MSDevDir=C:\PROGRA~1\MICROS~4\COMMON\msdev98
rem
rem Root of Visual C++ installed files.
rem
set MSVCDir=C:\PROGRA~1\MICROS~4\VC98
rem
rem VcOsDir is used to help create either a Windows 95 or Windows NT specific path.
rem
set VcOsDir=WIN95
if "%OS%" == "Windows_NT" set VcOsDir=WINNT
rem
echo Setting environment for using Microsoft Visual C++ tools.
rem
if "%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=
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.
// 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 );
};
}
C:\Program Files\Microsoft Visual Studio\VC98\
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.
Dim o_TypeLib As TypeLibInfo
Set o_TypeLib = TLIApplication.TypeLibInfoFromFile("C:\Example2\EX2.TLB")
o_TypeLib.Register
MsgBox "The TypeLibrary is registered."
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:
注:可以使用regtlib.exe来对tlb文件进行注册。
在遇到.net中有枚举类型,同时COM是原有系统不能更改代码时,因为regasm或者tlbexp生成的tlb文件会修改枚举的名称,可以将tlb转换成idl文件,通过修改名称之后将idl编译成tlb,然后再使用regtlib来注册使用。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。