I have some old ActiveX libraries, that are used in Internet Explorer. There, in JavaScript an object would be created with new ActiveXObject("MyLib.MyClass")
and used right away. The class is registered. But in my C# project I have some problems.
My first issue is that I had to add a direct reference to the library, with Browse button. In .csproj file I see <HintPath>..\..\..\..\..\Program Files (x86)\MyLib\MyLib.dll</HintPath>
. This doesn't feel good. What if another developer keeps the project on drive D:?
I tried to use RegAsm.exe with /tlb switch, and then the libraries are visible in Add Reference > COM > Type Libraries list. But if I try to add it from there I get this error: The ActiveX type library 'C:\Program Files (x86)\MyLib\MyLib.tlb' was exported from a .NET assembly and cannot be added as a reference.
Aha, this ActiveX is a .NET assembly. No trace of it in Add Reference > Assemblies > Extensions though, so I try to add it to GAC, with gacutil.exe. I get this error when I try: Failure adding assembly to the cache: Attempt to install an assembly without a strong name
. We didn't care much for strong names, because it just worked in JavaScript. It is primarily an ActiveX component, not a .NET assembly. It was built in C# because it was easier to do so.
My question: How to "properly" add ActiveX control to the project, what am I missing?
My second issue is that to load the component it has to be in the same folder as the host process. This also raises an alarm to me. It's not how ActiveX should be used. Naively I tried to fix this by adding the path of the library to PATH environment variable, but that didn't work. I assume if I solve the first issue that this second one will also be solved.
Thanks to Fixation's comment this is how I do it. Seems to be working for now:
Type type = Type.GetTypeFromProgID("MyLib.MyClass");
dynamic comObject = Activator.CreateInstance(type);
return comObject.MyMethod(myParam);