DOCUMENT:Q143258 11-JAN-2001 [vbwin] TITLE :HOWTO: Create Constants and DLL Declarations in a Type Library PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:4.0,5.0,6.0 OPER/SYS: KEYWORDS:kbVBp400 kbVBp500 kbVBp600 kbGrpDSVB ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Professional Edition for Windows, versions 5.0, 6.0 - Microsoft Visual Basic Enterprise Edition for Windows, versions 5.0, 6.0 - Microsoft Visual Basic Standard Edition, 32-bit, for Windows, version 4.0 - Microsoft Visual Basic Professional Edition, 16-bit, for Windows, version 4.0 - Microsoft Visual Basic Professional Edition, 32-bit, for Windows, version 4.0 - Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows, version 4.0 - Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows, version 4.0 ------------------------------------------------------------------------------- SUMMARY ======= It can be very useful to package constant definitions and DLL declarations in a type Library. Visual Basic allows you to access type libraries and their contents. Once you have made a reference to the type library, you can view its information in Visual Basic's own Object Browser. Type Libraries are also superior in that you can also provide help strings along with the declarations to always let the user know what a definition is for. MORE INFORMATION ================ To create a type library, you need the command line programs UUID.EXE and MKTYPLIB.EXE. These programs are provided with Visual C++. Suppose we wanted to create a type library with three integer constants, a string constant, and one Windows API declaration. The following is the Object Description Language (ODL) code needed to produce the type library. Read through the ODL and there will be comments explaining what each section is and why we need them. [ // The Universal Unique identifier (UUID), needs to be generated by // using uuid.exe program. Then, you paste the value into the type // library. uuid(006da100-110f-11cf-83b2-00aa0068851c), // The Help String comes up in the object browser as the second // piece of the library description in the "Libraries/Projects" combo // box. helpstring("Constants TypeLib"), // The Locale Identifier (LCID), identifies the language the type // library applies to. lcid(0x9), // This is the version number of the type library. version(1.0) ] // This is the name of the type library. It comes up in the object // browser as the first piece of the library description in the // "Libraries/Projects" combo box. library MyLib { // Define numeric constants. typedef enum tagConst { mylibConst1, // Make the constant mylibconst1 equal to 0. mylibConst2=5, // Make the constant mylibconst2 equal to 5. mylibConst3=7 // Make the constant mylibconst3 equal to 7. }Constants; // You need to define string constants in a module. // Modules also need to reference a dll name. In this case you don't // need to, so just give it a bogus name. [dllname("bogus")] module MoreConstants { // Define a constant mystr and assign it the value StringConstant. const LPSTR mystr="StringConstant"; }; // Now call a function located in the Windows API, specifically // User32.dll. Now you see why you need a dll name here, this is // where the API function will come from. [dllname("user32.dll")] module APIDeclare { // Give the API function a descriptive help line, this will // be seen in the Object browser. Then, you can declare the // function. The entry attribute specifies the identifier for the // entry point into the dll. // The in attribute specifies a parameter as a value going into // the function. //Note that the following two lines need to be all on one line. [helpstring("Test API function Declaration"), entry("CloseWindow")]_ boolean CloseWindow([in] long Winhndl); }; } To compile this code, copy it into a text file and call it test.odl, and then run the command line program MkTypLib as follows: mktyplib /nocpp test.odl The following message appears: Successfully generated type library 'test.tlb'. To Use the Type Library from Visual Basic ----------------------------------------- 1. Start Visual Basic with a new project. 2. From the tools menu, select references. This will bring up the Reference dialog box; click the Browse button to search for the Test.tlb file. Once you have found the file, click OK. 3. The Type Library will now be in the list of references, make sure it is checked, then exit the references dialog box. 4. Draw a command button on form1. 5. Enter the following code: Private Sub Command1_Click() Dim x As Boolean 'Call the Windows API function CloseWindow without making one 'declare at all. Note that the CloseWindow API will close the current 'window. x = CloseWindow(Me.hWnd) 'Now print out the value of the constants in the type library 'using message boxes. MsgBox mylibConst1 MsgBox mylibConst2 MsgBox mylibConst3 MsgBox mystr End Sub 6. Run the program; you will see form1 minimize and then you will see a message box for each constant we defined in our type library and the value for each constant. REFERENCES ========== OLE 2 Programmers Reference Volume 2 Additional query words: ====================================================================== Keywords : kbVBp400 kbVBp500 kbVBp600 kbGrpDSVB Technology : kbVBSearch kbAudDeveloper kbZNotKeyword6 kbZNotKeyword2 kbVB500Search kbVB600Search kbVBA500 kbVBA600 kbVB500 kbVB600 kbVB400Search kbVB400 kbVB16bitSearch Version : WINDOWS:4.0,5.0,6.0 Issue type : kbhowto ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 2001.