Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Creating Sub Main

At this point, the add-in has a server class that can add items to the Add-In menu with the ConnectAddIn subroutine. It can remove those items with the DisconnectAddIn subroutine. The project also has one or more event handler classes with AfterClick subroutines. If the add-in supports a single command, as is the case with the PropertySetter add-in, the same class can handle all three functions.

The developer still cannot use the add-in because the Add-In Manager and the operating system do not know that the add-in exists. The Add-In Manager learns about add-ins by examining an entry in an initialization file. In Visual Basic 4, this file is VB.INI. In Visual Basic 5, it is VBADDIN.INI. In Windows 95, these files are located in the computer's Windows directory. In Windows NT, the files are in the WinNT directory. If the server is a 16-bit application, an entry should be made in the "Add-Ins16" section of the initialization file. If the server is a 32-bit application, the entry belongs in the "Add-Ins32" section.

Entries in the initialization file give the Add-In Manager the names of the add-in applications and the classes that contain the ConnectAddIn and DisconnectAddIn subroutines. For example, suppose a project named MyProject contains an add-in class named Connector that provides the ConnectAddIn and DisconnectAddIn subroutines. Then the initialization file should contain an entry for a value named MyProject.Connector. The initialization file entry should specify a value of either zero or one. The value one means new instances of the Visual Basic programming environment should automatically load the server into the Add-Ins menu when they start. Initially an add-in should set this value to zero.

Visual Basic will change the value to one when the developer loads the add-in using the Add-In Manager. This will make the add-in automatically load the next time the developer starts the programming environment. The following lines show how the 32-bit version of the PropertySetter add-in is declared in the 32-bit section of the initialization file;

[Add-Ins32]

PropertySetter.PropSet=0

This entry could be created by editing the initialization file using a text editor. This would be rather cumbersome, however. Traditionally, an add-in provides a subroutine that creates the appropriate entries in the initialization file. In Visual Basic 4, an add-in project begins execution with the Main subroutine. The add-in can create its initialization file entry in the Main subroutine. Then the developer can make the add-in create its entry by running the application in the development environment. The following code uses the GetPrivateProfileString and WritePrivateProfileString API functions to create an entry for the PropertySetter add-in for the Visual Basic 4 development environment;

#If Win16 Then

Declare Function GetPrivateProfileString Lib "Kernel" _

(ByVal lpApplicationName As String, _

ByVal lpKeyName As Any, ByVal lpDefault As String, _

ByVal lpReturnedString As String, _

ByVal nSize As Integer, ByVal lpFileName As String) _

As Integer

Declare Function WritePrivateProfileString Lib "Kernel" _

(ByVal lpApplicationName As String, _

ByVal lpKeyName As Any, ByVal lpString As Any, _

ByVal lplFileName As String) As Integer

#Else

Declare Function GetPrivateProfileString Lib "Kernel32" _

Alias "GetPrivateProfileStringA" _

(ByVal lpApplicationName As String, _

ByVal lpKeyName As Any, ByVal lpDefault As String, _

ByVal lpReturnedString As String, ByVal nSize As Long, _

ByVal lpFileName As String) As Long

Declare Function WritePrivateProfileString Lib "Kernel32" _

Alias "WritePrivateProfileStringA" _

(ByVal lpApplicationName As String, _

ByVal lpKeyName As Any, ByVal lpString As Any, _

ByVal lpFileName As String) As Long

#End If

Sub Main()

Dim section As String

Dim val As String * 256

Dim length As Long

' Check the correct section of VB.INI.

#If Win16 Then

section = "Add-Ins16"

#Else

section = "Add-Ins32"

#End If

' See if PropertySetter.PropSet is already in VB.INI.

length = GetPrivateProfileString(section, _

"PropertySetter.PropSet", "", val, _

256, "VB.INI")

' If not, add it.

If length = 0 Then _

WritePrivateProfileString section, _

"PropertySetter.PropSet ", "0", _

"VB.INI"

End Sub

When an add-in application is run in the Visual Basic 5 development environment, the Main subroutine is not executed. The application can still provide a subroutine to install the add-in, however. In this case, the developer will need to explicitly invoke the subroutine to create the necessary entry. The developer can do this by executing the subroutine in Visual Basic's Immediate window.


[Home Page] [Tutorial Page] [Main Add-In Page] [Next Lesson] [Prev Lesson]





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.