Creating Component Object Model (COM) Object

Tips >> Visual Basic

 

MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE

The Component Object Model (COM) is a Microsoft framework for managing component objects, allowing them to communicate by calling methods of other components in their object workspace. Each component in the framework is a self-contained program. Traditional programs cannot communicate with programs outside their workspace when running, without using other services such as DDE, TCP/IP, etc.

ActiveX is the name given to the services based around the COM framework. The components may be visual (embedded into other documents using Object Linking and Embedding), or a set of services (compiled as a Dynamic Link Library (DLL)).

DISTRIBUTED COM:

The Distributed Component Object Model (DCOM) extends the COM framework so that components may communicate across different computers. It achieves this by executing a Remote Procedure Call (RPC) to a server, which executes the method and returns the results to the client. Creating DCOM components allows you to extend the functionality of ASP. As the components are compiled, they tend to run much faster than calling script routines through the ASP interpreter.

 

REGISTERING COMPONENTS:

When you develop a COM object, the component is automatically registered on the computer when it's compiled. If you are developing a component for another computer, then you have to copy the compiled DLL to the server and register it explicitly using regsvr32.

 

BUILDING A COM OBJECT WIITH VISUAL BASIC:

The discussion has thought to create a COM object using Visual Basic. The object will be used to determine if a string is a palindrome (a string that reads the same forwards as it does backwards, ignoring case and punctuation).

A DLL project in Visual Basic is implemented as a class module. The class module allows you to define attributes (data of the class), and methods (the functionality of the class). One of the principle concepts of object-oriented programming is that the data of the class is encapsulated in the object. This means that variables of the class are not accessible directly without using some method. Methods of the class tend to be declared as Public as they must be accessible from outside the class. If the class has functions that you don't want to be accessed outside of the class, then you declare them as Private. The Palindrome component we will create has no attributes (variables), one public method to check if a string is a palindrome, and a private function to format the string to determine if it's a palindrome.

 

STARTING THE PROJECT:

Start an ActiveX DLL project in Visual Basic. Set the name of the project to CheckPalindrome, and the name the Class Module Palindrome. The following is the code for the Class module

 

Palindrome.cls
Option Explicit
Public Function isPalindrome(ByVal strPal As String) As Boolean
     Dim strTemp As String
     strPal = stripPunctuation(strPal)
     strTemp = StrReverse(strPal)
     isPalindrome = (strTemp = strPal)
End Function
Private Function stripPunctuation(ByVal strOriginal As String) As String
     Dim strStripped As String
     Dim iAscii As Integer, counter As Integer
     strOriginal = LCase(strOriginal)
     For counter = 1 To Len(strOriginal)
          iAscii = Asc(Mid(strOriginal, counter, 1))
          ' The lowercase ASCII range is 97 to 122
          If iAscii > 96 And iAscii < 123 Then
               strStripped = strStripped & Chr(iAscii)
          End If
     Next counter
     stripPunctuation = strStripped
End Function

 

Create the DLL by selecting "Make CheckPalindrome.dll" from the File menu. The component will be automatically registered on the machine.

 

TESTING THE COMPONENT:

You can test the new object by creating a new Standard Exe project in Visual Basic. In the new project, select the "Project" menu item, then select "References". Scroll down to our new component "CheckPalindrome", and place a tick against it. You can now use the object directly in the project.
The following example has one TextBox called txtPal, and one CommandButton called cmdTest.

 

usepal.frm
Option Explicit
Private Sub cmdTest_Click()
     Dim objPal As New Palindrome
     If objPal.isPalindrome(txtPal.Text) = True Then
          MsgBox txtPal.Text & " is a palindrome"
     Else
          MsgBox txtPal.Text & " is not a palindrome"
     End If
End Sub

 

 

REGISTERING THE COMPONENT ON A SERVER

To use the component with ASP, we need to register it on the server.

Copy the file to the server, and then register it using regsvr32

regsvr32 c:\development\components\CheckPalindrome.dll

 

Once the components registered, you can access it in ASP as follows:

Using the component with ASP

pal.asp
<% @ language="vbscript" %>
<% Option Explicit %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"https://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Palindrome Tester</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>Palindrome Tester</h1>
<%
Dim objPal, strPal
strPal = Request.Form("pal")
If strPal <> "" Then
     Response.Write "<p>"
     Set objPal = Server.CreateObject("CheckPalindrome.Palindrome")
     If objPal.isPalindrome(strPal) = True Then
          Response.Write strPal & " is a palindrome"
     Else
          Response.Write strPal & " is not a palindrome"
     End If
     Response.Write "</p>"
End If
%>
<form id="palindrome" name="palindrome" method="post" action="pal.asp">
<p>
Text: <br>
<input type="text" size="40" value="" name="pal"><br>
<input type="submit" value="Test" name="palButton">
</p>
</form>
</body>
</html>

 

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641