![]() |
Platforms, Frameworks & Libraries »
COM / COM+ »
COM Interop
Advanced
License: The Code Project Open License (CPOL)
Creating a COM DLL with VS 2005: Advanced WalkthroughBy VB RocksThis article demonstrates how to manually create a COM DLL with VS 2005. |
VB6, VB (VB7.x, VB8.0, VB9.0), COM, COM+, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This article demonstrates how to create a COM DLL in Visual Studio 2005 the manual way. It also demonstrates how to install it into the GAC with gacutil.exe, and how to register the library using the Assembly Registration Tool (regasm.exe).
To learn how to create a COM DLL the easy way, see my article "Creating a COM DLL with VS 2005: A Walkthrough"
To learn how to register a COM DLL the easy way, see my article "Registering COM DLL with VS 2005 Walk-Through"
After almost a year from posting my super simple article "Creating a COM DLL with VS 2005: A Walkthrough", I was surprised to find quite a few ratings lower than a 5. I figured there are probably a lot of developers that want to know the answer to the question: "How can I do this the hard way?" Well, for those sadistic sophisticated inquiring minds, here you go…
Just as a note, I'm thinking about writing an article about "Creating a COM DLL with the Visual Basic Command-line Compiler", which will use Notepad and Vbc.exe, so if you're really hard-core you may look for that.
*** Just as a note, please follow these instructions
closely.
*** If your COM DLL doesn't work
when you're done, then you may have missed something.
To begin with, let's create a new Visual Basic.NET
Class Library
First of all, notice that when Visual Studio created the project, it automatically added a class named "Class1". Let's start our programming by changing the name of "Class1" to something more meaningful, like "MyCalculator".
Let's add a method to our class that we can later call from VB6:
Now that we have our class programmed, we're ready to make our class COM Interoperable.
The first thing we need to do is Import the System.Runtime.InteropServices namespace. This is required to obtain necessary interop attributes.
Add the following to the top of the class:
Imports System.Runtime.InteropServices
The next thing we need to do is define an interface for our class. This is necessary, because this is what a COM client uses to communicate with a COM object.
To define an interface for our class, we will add the <ClassInterface()> attribute to our class, which will automatically generate an interface for us. This will ensure that each public member of our class will be exposed to COM.
Our class should now look like this:
After our Interface has been added, we will need to add a specific GUID, which is a 128 bit number that gets added to the registry, and is used to identify a COM type. (GUID is a Globally Unique Identifier)
To generate a GUID:
Now that we have our GUID, let's add the Guid() attribute between the ClassInterface() attribute and our class declaration. Then, paste in the GUID. * Be sure to remove the curly braces {}.
Your class should now look like this:
Define a Strong Name
All Com Interop assemblies should be Signed, and installed into the GAC (Global Assembly Cache). This is not technically required, if the assembly is not installed into the GAC, then it needs to be copied into the same folder as the COM program that is using it.
To sign the assembly:
Register for Com Interop
Since we have the Properties window open, click on the Compile Tab, then check "Register for COM interop" at the bottom of the window.
Finally, Save and close the Properties window.
Now that we have everything setup, change the solution configuration from Debug to Release, then save and compile the project.
Next, open the Visual Studio Command Prompt:
GAC: Installing our library into the Global Assembly Cache using the "gacutil.exe" utility makes our .dll globally available to all .Net assemblies, and prevents them from being copied into the application directory of the referencing assembly.
gacutil –i MyComDll.dll
Registry: Registering our library into the Windows Registry using the "Regasm.exe" utility makes our COM type library .tlb (.dll) globally available to all VB6 assemblies.
regasm MyComDll.dll /tlb:MyComDll.tlb
Note: "TlbExp.exe" is another utility that can be used to generate a COM type library from a .NET assembly, however it does not register it with the Windows Registry.
We now have a functioning COM DLL ready to be used in VB6. Let's go into VB6 and see how to use it.
Create a new VB6 Project:
Add a reference to our new COM DLL:
Finally, in the Click() event of our Command Button, add the following code to utilize our new library:
Private Sub Command1_Click()
Dim calc As New MyCalculator
Dim result As Integer
result = calc.Add(5, 5)
MsgBox result
End Sub
To test our new COM DLL:
Here's the result of our test:
When you are finished working through this article, you may want to remove the library we created from the registry. Here's how:
gacutil –u MyComDll.dll

As you can see, it's quite a bit more work to create a COM DLL the manual way, including installing it into the GAC and registering it into the registry.
One thing to keep in mind is that if you try to install your COM DLL into the GAC, and register it in the registry on a computer other than your own development PC, you may find that the gacutil and regasm are not installed. If that is the case, you will need to install the .NET Framework Tools. Here is a link that has more information about them:
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 7 Jul 2009 Editor: |
Copyright 2009 by VB Rocks Everything else Copyright © CodeProject, 1999-2010 Web11 | Advertise on the Code Project |