(Also posted in Dark Basic Pro Discussion Forum)
STYX COM .NET DLL Tutorial
OK folks, so you want to create a DLL using Visual Studio 2005 (VS) and the .NET Framework that will load into DBPro?
Firstly, grab a copy of the source code and VS solution attached to this post and unzip it to a suitable location on your computer - for examples sake let's assume you've unzipped to c:/vs2005-projects/DBProTest. It uses c# as the language, but the principles will also apply to VB.NET users. You'll also find in here a DBPro project and source file which we'll run later on.
Next, start VS and load in the .sln file. Or if you prefer, double-click on the .sln file to fire up VS.
Also now is a good time to open a Visual Studio 2005 Command Prompt in Start > All Programs > Microsoft Visual Studio 2005 > Visual Studio Tools. We'll need it in a short while. While you're here change directory to our project by typing 'cd c:/vs2005-projects/DBProTest'.
Open DBProTest.cs from Solution Explorer and take a look at it. It's pretty simple in construction: The DBProCOMExample namespace encompases a single public interface and a public class (that implements the interface) defining a public MyString string that can be set and read, plus a single public MyMessage() method that returns a string type. Creating a public interface exposes your DLL's internal types to COM in a controlled explicit manner and
ensures communication between the outside world and your DLL's internals occurs only in this manner.
Also of vital importance here is the ystem.Runtime.InteropServices namespace which provides a wide variety of members that support COM interop. The attributes in square brackets that decorate the class are members of this namespace.
Next, open the Properties/AssemblyInfo.cs file from Solution Explorer. You can populate the various [assembly: *] attributes in here to personalise your resulting DLL. The vital attribute in here is [assembly: ComVisible(true)] which will allow you to access members of your DLL from COM. It's like the master switch, if you like.
Another flag that needs to be enabled is 'Register for COM interop' which is located in Project > DBProTest Properties > Build tab. This flag indicates that your managed application will expose a COM object (a COM callable wrapper) that allows a COM object to interact with your managed application.
So, let's see if the whole shebang will compile and work with DBPro on your development computer. Under the Build menu select 'Build DBProTest'. All going well you should end up with a new DBProTest.dll file in the bin/release sub-folder. VS also creates some other files during compilation which we won't worry about for the moment.
Take a copy of the new DBProTest.dll and paste it into the c:/vs2005-projects/DBProTest folder.
Switch back to the VS Command Prompt where we will now use the .NET Framework Assembly Registration Utility called 'regasm'. Type 'regasm <AssemblyName> /tlb:<FileName>' where <AssemblyName> is the name of our new DLL and <FileName> is the name of a Type Library that will be exported and the types registered in the Windows Registry:
regasm DBProTest.dll /tlb:DBProTest.tlb
OK still with me? Good! Now fire up your copy of DBPro and load in the DBProTest.dbpro project. The few lines of source will create an instance of the class from our DLL, set MyString to a value, then call the MyMessage() method. Notice that the format of the class name in line 1 is "DBProCOMExample.DBProTest" which is the .net namespace we chose followed by the actual class name.
Compile the code into an exe in c:/vs2005-projects/DBProTest by pressing F5 and with any luck you will see a message on the screen!
In order to deploy your DLL onto other computers you should add a Setup project to the VS solution and specify the the source to be the output from our VS DBProTest project (i.e. the compiled DLL). The resulting .msi Windows Installer file should automatically register the DLL and types on the destination computer. Subject matter for another tutorial methinks...
-------------
Net Commander
-------------