Solved

How can I register a COM enabled .net dll's type library (.tlb) file programmatically on a user's pc?

Posted on 2008-06-16
9
3,681 Views
Last Modified: 2012-05-05
I have a .net COM enabled dll MyDll.dll which I wish to call from Excel 2003 VBA on client pcs.  The setup package project I have created in Visual Studio 2008 will successfully register the dll on the client pc, but doesn't seem to be able to register the type library file, MyDll.tlb which it distributes along with the dll file  

I can generate and register a new copy of the tlb file on the client pc via running (from a command line, as admin user, and from the .net v2 directory) regasm pathtoMyDll.dll /codebase /tlb but this is tedious to do on 40-50 pcs.  
How can I automate the process? Or should I be doing things a different way?

(My clients are usually running Windows XP and Office 2003)
0
Comment
Question by:patrick_exe
9 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
Comment Utility
Did you use the Excel add-in template to create the .dll? Did you set the build properties to Register for COM Interop?
0
 

Author Comment

by:patrick_exe
Comment Utility
Technically, it is not an add in (it is unnecessary for it to be one), it is just a .net class library, with both "Register for COM Interop" and "Make COM visible" ticked.  It is also <strong>not</strong> signed (contrary to what Microsoft say, it is <strong>not</strong> necessary to put dlls in the Global Assembly Cache, nor is it necessary to make them strongly signed when using the /codebase flag with regasm).

For a very simple project, such as the one in the code snippet below, all that seems necessary for Excel VBA to be able to use the dll is that 1) the dll and tlb files generated by Visual Studio 2008 be placed in the same folder (e.g c:\test) on the client pc, 2) the dll be registered using (when running as admin from a command line) regasm "c:\test\COMEnabledClassLibrary.dll" /codebase and 3) that the VBA project have a reference to the tlb file.

My real life project has many more interfaces, classs and methods, including some with generics (which I know can't be exported to COM, but I'm not using those particular methods from COM, only from .net).  On some pcs, all that is needed is the three steps above. On some others, I get an "Object library not registered" error either at compile or run time in the Excel VBA project, and I can solve this by registering the COMEnabledClassLibrary.tlb file, either without regenerating it by 4a) running regtlibv12.exe "c:\test\COMEnabledClassLibrary.tlb", or by regenerating it by 4b) running regasm "c:\test\COMEnabledClassLibrary.dll" /codebase /tlb, in each case as administrator.

Aside from the issue of how to automate things (and thus avoid administrators at my clients having to run regasm or regtlibv12 from a command line), two things are puzzling about this: why the tlb file needs registering on some pcs but not others, and why the tlb file needs regenerating on some pcs and not others.  The original tlb file must include file paths on my development pc which do not match up with the deployment path on the client pc (in this example c:\test\), so if the tlb file needs registering at all I would have thought that it needed to be regenerated so that the correct path(s) could be stored in the registry, however VBA manages to get round that on some pcs.

For pre .net COM dlls (e.g. those generated by VB6), simply setting a reference to the dll in a VBA project was enough to register the dll, but for a .net COM enabled dll (such as I am using), it is not clear to me exactly what setting a reference to the .tlb file does.  Perhaps it registers the tlb file on some pcs, registers both the tlb and dll file on others, and simply enables the VBA project to compile on others without doing any registration.  (If so, that might explain why different actions are necessary on different pcs).  

I can run regasm with /codebase and /regfile to produce a .reg file which (without registering the dll or tlb file) shows me the registry entries needed to register the dll, but not the tlb file (one can't run regasm with both /regfile and /tlb, which seems silly, or at least Microsoft ought to have provided a way of producing a reg file with just the registry entries needed to register the tlb file).
Regtlib.exe and its successor regtlibv12.exe, which seem to be deprecated by Microsoft (they seem to have disappeared from my Vista pcs after some recent Windows updates, and don't seem to be documented properly, might provide information as to what registry entries are needed for a tlb file (e.g. if they allow a similar /regfile flag but I can't find any documentation that suggests that they do - I may try it as an experiment).
// 16 Jun 2008 Simple .net 3.5 C#Project to provide a minimum COM enabled (via Make COM visible and Register for COM Interop project properties) library
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;
 

namespace COMEnabledClassLibrary

{
 

    /// <summary>

    /// 

    /// </summary>

    public interface ICOMTestClass19

    {

        /// <summary>

        /// 

        /// </summary>

        int ReturnSeven();

     

    }
 

    /// <summary>

    /// 

    /// </summary>

    public class TestClass19 : ICOMTestClass19

    {
 

        #region ICOMTestClass19 Members
 

        public int ReturnSeven()

        {

           return 7;

        }
 

        #endregion

    }

}
 

// the corresponding simply VBA client code using this is

Public Sub Test

Dim obj as ICOMTestClass19

Set obj = new TestClass19

dim i1 as Long

i1 = obj.ReturnSeven

End Sub

Open in new window

0
 

Author Comment

by:patrick_exe
Comment Utility
Update: I have tested regtlibv12.exe and it ignores any /regfile flag so that can't be used to produce a .reg file with registry entries needed to register the .tlb file.
You experts might also like to know a bit of background: this is for a network installation of my software.  The dll and .tlb file are installed on a shared folder on the network, and each pc has been configured (via caspol) to trust software in that precise location (otherwise .net security by default won't allow the code in the dll to run).

Our client doesn't want to have to run a Windows Installer package on each pc, so, given the relatively small number of files involved (effectively just the dll and tlb file) putting the dll and tlb file in a shared folder on the network and getting them registered for each pc in an automated way seems to be the best way to deploy the software (with Excel VBA as one of the front ends).  Until recently, the registration has been done via login scripts (.bat files running [with admin rights] regasm pathtodllfile /codebase) but on a few pcs the tlb file seems not to have been registered (whereas it had been on the majority of pcs, possibly [as surmised above] via Excel VBA setting a reference to the tlb file), so we tried amending the .bat scripts to run regasm pathtodllfile /codebase /tlb: pathtotlbfile but the scripts seem to have failed silently, hence the need to run regasm pathtodllfile /codebase /tlb: pathtotlbfile as admin from the command line on each pc.
Am investigating why the scripts failed by getting them to report success/failure and to pause before being dismissed by the user, but any insights you can offer as to a) whether setting a reference in Excel VBA to a .tlb file sometimes registers the tlb file, and b) other advice if you would deploy things differently will be appreciated.
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
Comment Utility
Did you try registering as below

regasm/codebase yourdotnetdll.dll /tlb:yourdotnetdll.tlb
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 17

Expert Comment

by:Shanmuga Sundaram
Comment Utility
I forget to mention you to do that from the command prompt
0
 

Author Comment

by:patrick_exe
Comment Utility
shasunder: (apologies for not replying earlier, somehow I missed your post) if you read my comment of 06.17.2008 at 10:22PM BST immediately before yours, you'll see that in the end we had to do precisely what you suggested:

regasm pathtodllfile /codebase /tlb: pathtotlbfile as admin from the command line on each pc.

so I'm afraid your comment doesn't add anything new.  

I conclude that this is an area where noone seems to know the answer (or perhaps people at Microsoft do, but if so, they aren't posting on this exchange.)
I am going to suggest to the admins that this post remain as unsolved, because it at least offers some suggestions of things to try and some manual solutions if others are looking at this area.


 
0
 

Author Comment

by:patrick_exe
Comment Utility
The learned one:
Rather than classify this question as abandoned and deleting it, I suggest leaving it with some other status, e.g. unsolved, because it at least offers some suggestions of things to try and some manual solutions if others are looking at this area.
Regards
Patrick
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
Comment Utility
PAQed with no points refunded (of 500)

Computer101
EE Admin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now