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.
- Locate a file with the name vcvars32.bat, normally located in c: \ Program Files \ Microsoft Visual Studio \ vc98 \ bin \. visual Studio will have already stored ded this file during installation. the contents of the batch file shocould look like this:
@ 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 % \ MFC \ include; % include %
Set Lib = % msvcdir % \ Lib; % msvcdir % \ MFC \ Lib; % lib %
Set vcosdir =
Set vscommondir =
- Next create a shortcut on your desktop to command. com. Name the shortcut go midl.
- 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
- Now select the memory tab and make sure the initial environment: property is set to at least 2048.
When you click on this sort cut, you will be in a DOS shell that is properly set for compiling IDL files. the specified cut will run the vcvars32.bat file for you. this properly initializes the DOS environment variables for the midl compiler.
- 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 );
};
}
- Copy or move ex2.idl to the following directory:
C: \ Program Files \ Microsoft Visual Studio \ vc98 \
- Now click on your go midl shortcut.
- 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.
- Move the ex2.idl and ex2.tlb files to a directory of your choice. For this example, Ill say its in c: \ example2 \.
- Open VB and create a new standard EXE project.
- Go the project references dialog and add a reference to typelib information as shown here:
- Click OK to return the to form1 and add a command button to the form.
- Double click on the command button and add the following code to the Commander click () event procedure:
Dim o_typelib as typelibinfo
Set o_typelib = tliapplication. typelibinfofromfile ("C: \ example2 \ ex2.tlb ")
O_typelib.register
Msgbox "The typelibrary is registered ."
- 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:
Note: You can use regtlib.exe to register a TLB file.
encountered.. Net has an enumeration type. When COM is the original system and cannot change Code , because the TLB file generated by regasm or tlbexp modifies the enumeration name, you can convert TLB to an IDL file, compile IDL to TLB by modifying the name, and then register and use regtlib.