I have a C# library that was developed in-house to provide an interface to our legacy software. Over the time requirements grew and now here I am to add COM visibility to this interface library so that it can be accessed from something like VBA in the MS Office applications.
Update 3: Added bit more details in the 'Existing Design' text.
Existing Design: Let's call my interface library as Interface.dll. It's a facade pattern which includes some backend /dlls, which are dependencies for this Interface.dll. Let's call them as: MyTypeLib.dll, MyHelper1.dll, MyHelper2.dll and few more helpers. Every .dll mentioned above is a separate library project in my solution.
My approach was:
Develop a new library that has a COM visible class. I did something like:
Update 1,2: Added my interface sample code
[Guid("CE3750B7-DE31-4635-A69C-110B1271B363")]
public interface ICOMVisibleClass
{
...
...
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("A023101A-D9B3-4A24-AAE4-B3CFEDA04BAF")]
public class MyCOMVisibleClass : ICOMVisibleClass
{
...
...
}
and selectively call methods from Interface.dll, making these selected methods COM visible. Let's say above class produces MyCOMVisibleClassLib.dll. I used RegAsm.exe to register this library and to produce MyCOMVisibleClassLib.tlb file.
To test this implementation, I decided to use MS Excel 2010. I opened VBA editor and added a reference to MyCOMVisibleClassLib.tlb. In the code, I can now see all the methods in MyCOMVisibleClass so it seems to be working until.....
I ran this code and got "Run-time Error -2146233088 (80131500). Unable to find MyTypeLib.dll" I used Assembly Binding Log Viewer to see more details of this error and found that MS Excel was looking for this (MyTypeLib.dll) everywhere except where actually this file is. If I copy this file into Excel installation folder under Program Files, Excel would compain about next dependency - MyHelper1.dll and so on. After I copied all the dlls to Excel install folder all was good. But this is not the solution I am looking for.
Questions:
- Is my approach correct to open up few methods to COM?
- I think that MyCOMVisibleClassLib.dll is the only one that needs to be registered. Am I right in thinking so?
- How do I make Excel to find all my dependencies (MyTypeLib.dll, MyHelper1.dll, MyHelper2.dll ) without having to copy them in MS Excel install folder?
I also think that I am missing something basic that I should have known before I start this but can't seem to figure it out.
Any help is very much appreciated. Thanks in advance.