osPlanning / omx

Open Matrix (OMX)
https://github.com/osPlanning/omx/wiki
Apache License 2.0
49 stars 18 forks source link

Make C# API COM visible #19

Closed bstabler closed 3 years ago

bstabler commented 8 years ago

Makes loading the library in Excel via VBA possible

Ennazus commented 8 years ago

I've never done this but it looks pretty easy. Have you ever done this @bstabler? It looks like you just put a little preamble before each class declaration, like this: [ComVisible(true)] [Guid("7884998B-0504-4CBE-9FF9-6BBAA9776188")] [ClassInterface(ClassInterfaceType.None)] [ProgId("MyNamespace.MyClass")] public class MyClass : IMyClass { }

from http://www.codeproject.com/Articles/612604/Best-Practice-in-Writing-a-COM-Visible-Assembly-Cs

I don't really know how to test it either.

bstabler commented 8 years ago

I have not built COM functionality in C#, but I have done lots of VBA and COM programming in Excel. I think you would test this by writing VBA in Excel using your COM library. To start, turn on the Developer Ribbon in Excel. Then click Visual Basic to get the VBA Editor. Then you add COM libraries via Tools + References. Finally, write a little VBA program to test it out.

Ennazus commented 8 years ago

Okay, I looked at this again and seems like the code should be com visible because in AssemblyInfo.cs, we have it set like this [assembly: ComVisible(true)] I started to try to test this by writing a vb script. I was able to add the dlls needed CSharpOMX.dll and HDF5DotNet.dll. Then I tried to start writing code and realized I haven't written VB in ten years so it would take me too long to try it out! So I think I need to pass this off to someone else.

bstabler commented 8 years ago

Two updates:

1) In order to add the type library in the VBA editor, you first have to create a type library file. You can do this as follows:

RegAsm.exe C:...\CSharpOMX.dll /tlb:omx.tlb

This creates an omx.tlb that you can add via Tools + References

2) Unfortunately I don't believe the C# API will work in COM since COM does not support static methods. I tested this by trying to call the static read OMX matrix method in VBA, but it doesn't work. So, @Ennazus (or @mmilkovits or me) needs to make some changes to get this to work. Here is a link to this issue.

http://stackoverflow.com/questions/24193183/can-i-call-a-static-method-of-a-c-sharp-class-from-vba-via-com

mmilkovits commented 8 years ago

That's too bad about the static methods - I'm a bit buried now, but I can take a look later on this month. I'll touch base before I do.

bstabler commented 3 years ago

I'm closing this since we should move this to https://github.com/osPlanning/omx-csharp