Posts Tagged ‘COM’

Getting .NET COM Interop DLLs Working

Wednesday, October 1st, 2008

I recently finished working on a macro program where the software’s interface is COM.  Everything worked great until I wanted to transfer the macro over to a test client computer.  The program I was writing the macro for requires some registry entries, so I assumed that was all I needed to do.  After about 2 hours of having issues I figured out how to get the macro working on computers other than the one I developed the macro on.

What happens automatically when developing COM innerop projects in Visual Studio is that the IDE automatically registers the DLL file behind the scenes, so when the program that is going to use the macro starts up, it can find the DLL file (assuming the “register for COM Interop” is checked in project properties).

When transferring over the macro DLL(s), the client computer does not have the correct registry entries, so when the program attempts to load the macro, it will fail because it can’t find the macro DLL(s).

Things I learned:
- regsrv does not work on .NET DLLS, you must use regasm.exe (included in the .NET framework).
- just running regasm.exe with the dll name is not enough (there are two methods to getting the DLL visible to COM)

The method I used:
This method is good if you only want one program to see your DLLs, this does not register it with the GAC.
In your project properties inside Visual Studio 2005:
- Select the “Signing” tab, check the “Sign the assembly” checkbox.
- From the drop-down box, select <new>, fill out the fields in the pop-up (eg. key name and password).
- Creating a key for your DLL means that it will disallow people to create a DLL with the same name as yours and so the .NET framework can properly manage dependencies.  If you don’t do this step you will get a warning when you try to run the regasm step below.

- Figure out where the regasm file is so we can use it.  This step isn’t necessary if you use a console window inside Visual Studio. (for .net 2.0 it is in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe), regasm can’t be called with “regasm …” inside a normal console window, unless you added your own dos “shortcut” to the exe.
- Copy all of your output files to where you want them to be.  So for my macro, I had a directory inside the application’s add-on folder.
- Create a  text/BAT file  in the same directory
- include this command in the BAT file:  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm macroname.dll /codebase /regfile:install.reg
- run the bat file in a console window, it will generate a reg file that you can use to install the necessary registry entries to make the DLL visible to your selected DLL file.  Remember that the BAT file needs to be executed in the directory that you intend to run the macro from.  When the REG file is created, it adds various direct paths to the directory, so we need the regasm utility to use the correct path.

Now you can merge the REG file from anywhere in the user’s computer.  It will register the DLL file so that your specified program will find it.  When we write the regasm command line, take note of a few things.  The codebase switch is the directory where the DLLs are located.  the regfile switch is the filename you want the registry install file.  You can run regasm without the regfile switch, but it will install the registry keys directly into the registry.  This is fine, but for me having a reg file is more convenient.

Esprit Macro With .NET

Thursday, August 21st, 2008

Edit: I’m having some issues with a second tutorial, so at this point I don’t think this process below is correct.  2nd Edit:  This code should work fine, the issue was when I started using COM objects from Esprit.  There are a few gotchas that need to be taken care of.  I think the primary reason I had issues was due to writing the macro in C# instead of VB (the language the Esprit tutorials have).  I may be writing about it in the future.  If you are interested right now, send a message, or post in the forums.

This post will be a little more special interest them most of my posts. I recently started learning how to create macros for a program called Esprit (D.P. Technology). It’s a “CAD/CAM” program.

They still use COM/VBA as their plug-in interface, so to write a plug-in in .NET there are a few things that need to be taken care of. Their tutorial that comes with the program goes over setting everything up, but It misses a few steps. Well more likely, it was written for the one the earlier versions of Visual Studio, but anyways I will go over the steps necessary to get a “Hello World” running.

- Create a new Visual Studio project (Class Library), call it DotNETAddInTutorial1 as the tutorial from Esprit called it.

This tutorial goes over a simple hello world plug-in using Visual Studio 2005
- Go into the IDE menu Project->DotNETAddInTutorial1 Properties…
– Under the “application” tab, click the “Assembly Information…” button to the right. The pop-up will have a check-box at the bottom that says “Make Assembly COM-Visible” Check that. (important!) Click OK to close that pop-up window.
– Under the “Build” tab, check the “Register for COM interop” too

- Right click on References in the Solution Explorer window in the IDE. Select “Add Reference…”
- Select the COM tab and add these 4 references (Esprit Type Library, EspritCommands Type Library, EspritConstants Type Library, Microsoft Add-In Designer), take note that the Esprit ones will have version numbers in the name.

- Double click your Class1.cs file in the Solution Explorer so you can edit it.
Here is how the file should look:

using System;
using System.Collections.Generic;
using System.Text;
using Extensibility;
using System.Windows.Forms;

namespace DotNETAddInTutorial1
{
    public class Class1 : IDTExtensibility2
    {
        void IDTExtensibility2.OnAddInsUpdate(ref Array custom)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IDTExtensibility2.OnBeginShutdown(ref Array custom)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IDTExtensibility2.OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            MessageBox.Show("Add-In Tutorial 1 Connect", "", MessageBoxButtons.OK);
        }

        void IDTExtensibility2.OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
        {
            MessageBox.Show("Add-In Tutorial 1 Disconnect", "", MessageBoxButtons.OK);
        }

        void IDTExtensibility2.OnStartupComplete(ref Array custom)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
    }
}

Here is a nice tip:

    public class Class1 : IDTExtensibility2
    {

    }

If that is all you type into the class, you will see a blue squiggly line under the word Class1. If you right click on IDTExtensibility2 and select “Implement Interface”->”Implement Interface Explicitly,” Visual Studio 2005 will automatically generate the function tabs needed. Otherwise if you read the Esprit Documentation about this, they have a list of functions that you can type in by hand (their tutorials are in VB.NET only though).

From there you can build your solution. Copy the files in from the project’s Bin/Release folder into probably the folder “C:\Program Files\D.P.Technology\ESPRIT\AddIns\DotNETAddInTutorial1″

The last thing you need to do is create a registry file so you can let Esprit know of your plugin.
Create a text file and name it install.reg.
Right click on it and select edit.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\D.P.Technology\ESPRIT\AddIns\DotNETAddInTutorial1.Class1]
"Description"="Test Add-in using .NET C#."
"FriendlyName"="Cool Testing Add-on"
"LoadBehavior"=dword:00000001
"CincomVersion"=dword:00000000
"NETInterOpDlls"="DotNETAddInTutorial1.dll"
"COMDlls"="DotNETAddInTutorial1.dll"

I’ve noticed that the last two lines are NOT needed, but I just leave them in there anyways.

Once you’ve save the file you can right click on it again to merge it into the registry. The other option would open up regedit and do it by hand. Notice that DotNETAddInTutorial1.Class1 specifically specifies the project name and class file to call.

That’s it!




The Way Of Coding



 
Scott J. Waldron Photography
Stock Photo Website
Tech Learning Site
Follow me on Twitter

Popular Article Tags

Archives

Pages