Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

IDL For VB Tutorial : Page 2

The Interface Definition Language, or IDL, is a language for describing collections of data types, constants, methods (functions, procedures, etc.), and groups of methods known as interfaces. VB creates type information for you automatically when you create ActiveX components, but you can also create independent type information using IDL and the Microsoft IDL compiler. Using IDL in this way can enhance your programming skills and allow you to solve some pretty complex problems. The best way to get an understanding of how to write IDL, compile it to a type library, and use the type library in VB is see some real examples in a tutorial format, as this article demonstrates.


advertisement

Example 1: Looking Under the Hood of VB Components

Type information is the single most important element in component development, and VB hides almost every technical detail about it from the programmer. This example is designed to get you under the hood with respect to VB generated type libraries. Here are the steps:

  1. Start by opening VB and creating a new ActiveX DLL.
  2. Change the name of the project from Project1 to EUTH.
  3. Get into the project properties and for the project description enter Example, Under The Hood.
  4. Open up the Class1 class module and change the name from Class1 to Person.
  5. Make sure the Person class instancing is set to 5 MultiUse and insert the following code:
Public Enum PersonTypeEnum
euPersonUnknown = 0
euPersonButcher = 1
euPersonBaker = 2
euPersonCandleStickMaker = 3
End Enum

Public Property Get Name() As String
End Property

Public Property Let Name(ByVal RHS As String)
End Property

Public Property Get PersonType() As PersonTypeEnum
End Property

Public Property Let PersonType(ByVal RHS As PersonTypeEnum)
End Property

Public Sub MakeFriends(ByRef WithPerson As Person)
End Sub

Public Function IsHealthy() As Boolean
End Function



Of course, there is no logic in this code. Thats because we dont care about the actual logic that makes this class work, just the definition of the public members (properties, functions, etc.) of the class.

  1. Now, make the EUTH.DLL and save the project files so you can experiment with them later.

When you compile your ActiveX component, VB creates a binary file of type information and includes this file in the component as a resource. This type library is what you see when you reference a component in another project and view it with the Object Browser.

  1. Next, close VB and open Visual C++. Ill provide a little more guidance here, in case you're not familiar with VC++.
  2. Chose File.Open on the pull down menus, select the directory where EUTH.dll resides, change the Files of type: selection to Executable Files , and change the Open as: selection to Resources. Your dialog should look like this:

  1. Press Open to view the EUTH.dll resources. You may get a message box warning you that you cannot save resource back to the executable, just click OK. VC++ will open a window for you to view the resources compiled into the DLL.
  2. Expand the TYPELIB folder. Your window should look similar to this:

Notice the icon under the TYPELIB folder. This icon tells you that the resource is a binary file. You can use the VB Resource Editor add-in to include files like icons, cursors, wave file, etc., to your components.

  1. Now right mouse click on the 1 [Neutral] resource and select Export.
  2. Name the file EUTH.TLB and save it in the folder containing the EUTH.dll.

Now youve extracted a copy of the type library from the component. So what? You didnt have to extract the binary file at all. Instead you could have let VB create an independent type library for you by checking Remote Server Files in the Project Properties dialog. Well, now you know two methods of getting an independent type library from a VB component. There is also a third way:

  1. Under Visual Studio tools in the Windows task bar, find and execute OLE View.
  2. Select the File pull down menu and select View TypeLib.
  3. Next you will see the common Windows file dialog. Locate and Open EUTH.dll.

With a few mouse click you can explore what seems to be the contents of the type library. This is really just another representation of the type library. The real content is binary. The right hand pane contains decompiled IDL code representing the type information of the type library resource inside EUTH.dll. This text can be saved to an ASCII file and compiled into a type library using MIDL.EXE.

You will notice, if you spend some time exploring the contents using OLE View, that the information presented contains some similarities to the VB project from which it came. Type libraries supply information about components to the world of COM for two primary reasons: to help developers reuse components and to help compilers create client applications that properly address components. Unfortunately, I have heard too many VB programmers talk about creating COM components yet they dont know what a vtable is, cannot adequately describe COM mechanisms, and dont know what operating system components provide the essential services of COM. The best way to keep from looking like a techno-poser is to study hard and solidify your knowledge with lots of experimentation. Here are some references to start with:

  • MSDN Library(+)Platform SDK(+)COM and ActiveX Object Services(+)COM MSDN
  • Library(+)Platform SDK(+)COM and ActiveX Object Services(+)MIDL
  • Programming Distributed Applications with COM and Microsoft Visual Basic 6.0, Ted Pattison Microsoft Press


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date