0

I have the COM idl definition file, and i have a VB project that demonstrate how to communicate thru an application thru COM.

unfortunately, i cannot straight up port the VB code to C#. The COM is written in C++ and is reference by its program id

How can I instantiate the COM from C# and call functions from there?

here is the vb code

  Dim DrCephPatient As Object
  SampleCom = CreateObject("Sample.SampleCOM")

  SampleCom.Attribute1= "Hello"
  SampleCom.Method1(1)

Also, since i have the IDL definition, i have the UUID i can use to access.

Any resources I need to read to make this work?

curiousJorgeXX
  • 363
  • 1
  • 10

3 Answers3

0

You just need to add a COM reference to your C# project.

Add Reference context menu

The COM tab of the Add Reference dialog window lists all COM components that are available for referencing. If you want to add a reference to a registered COM DLL that contains an internal manifest, unregister the DLL first. Otherwise, Visual Studio adds the assembly reference as an ActiveX control instead of as a native DLL.

If a project type doesn't support COM, the tab doesn't appear in the Reference Manager dialog box. Read more about that in the How to: Add or remove references by using the Reference Manager article.

After adding a COM reference you can use your component as a regular .net classes (using the interop library generated automatically behind the scene).

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

Using C#, you can quasi get it to perform like VB script code by using Type.GetTypeFromProgID(), the Activator class, and using the dynamic type. I wouldn't necessarily prefer this, though. It's better to add the COM reference to your project. In my example I'm using, Word which runs out of process (is an .exe) so it will work with 32-bit or 64-bit versions of the runtime. However, for an in process server (.dll), the bitness of your code and the server need to match (either both 32- or 64-bit). However, for a quick test, this sometimes will do the job.

using System;

class Test {
   public static void Main(string[] args) {

      Type t = Type.GetTypeFromProgID("Word.Application");

      dynamic word = Activator.CreateInstance(t);

      Console.WriteLine("Word title:" + word.Name);

      word.Quit(); // otherwise hanging reference ... is Word API specific
   }
}
Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29
-1

You can use the SerialPort Class https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport?view=dotnet-plat-ext-5.0

Basically works with Open, Write/Read and Close functions but it's in C#, I guess it's not exactly what you look for

Also there is this list of articles about VB that could help you https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/computer-resources/accessing-the-computer-s-ports

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jacques R
  • 377
  • 3
  • 12