Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DLL Talk / [STYX] COM .NET DLL Tutorial

Author
Message
Net Commander
21
Years of Service
User Offline
Joined: 12th Sep 2002
Location: United Kingdom
Posted: 11th Oct 2006 11:14
(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:



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
-------------

Attachments

Login to view attachments
empty
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 12th Oct 2006 01:11 Edited at: 12th Oct 2006 01:11
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 27th Oct 2006 11:19
Quote: "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:

+ Code Snippet
regasm DBProTest.dll /tlbBProTest.tlb
"


How do I do this? I can't find the VS command prompt. I'm using VB Express.



BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 27th Oct 2006 15:35
[setting mailback]



Net Commander
21
Years of Service
User Offline
Joined: 12th Sep 2002
Location: United Kingdom
Posted: 3rd Nov 2006 21:37
OK maybe it's only VS Pro that has the extra command shell. It's basically just like a normal command shell, except it sets extra paths in your Windows environment variables to .NET tools.

Anyhow, by default you can find regasm.exe in C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727

If you can't find that path make sure you've got the .NET v2.0 redistributable installed:
http://www.microsoft.com/downloads/details.aspx?FamilyID=0856eacb-4362-4b0d-8edd-aab15c5e04f5&displaylang=en

You should add the above path and any other useful ones to your Windows PATH environment variable for next time:
- Right click My Computer and select Properties
- Flick to the Advanced tab
- Click the Environment Variables button
- In the System Variables box find and highlight 'Path'
- Click the Edit button
- Add ; then the above path
- Click OK

Hope this helps.

Login to post a reply

Server time is: 2024-04-19 11:39:38
Your offset time is: 2024-04-19 11:39:38