How to use Invoke COM Method

The Invoke COM Method activity offers the possibility to call a Component Object Model (COM) method. This post shows how to create a COM library and to use it with the Invoke COM Method activity.

image

Build a COM library

We will start with the source code of an easy COM library. Here we define an interface and a class which are visible for COM, both get a GUID. In the interface the attribute InterfaceType is set to InterfaceIsDual, which means that it is useable for early and late binding. In the class we set the attribute ProgId, which specifies a more understandable name of the class. The class contains one method which delivers a tiny text back.

//-Begin----------------------------------------------------------------

using System;
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyVersion ("1.0.0.0")]
[assembly: AssemblyFileVersion ("1.0.0.0")]
[assembly: ComCompatibleVersion(1,0,0,0)]
namespace Testing {

  [Guid("0FFAE26E-F945-4B3D-829B-BA3F2979CC84")]
  [ComVisible(true)]
  [InterfaceType(ComInterfaceType.InterfaceIsDual)]
  public interface ITest {
    [DispId(1)]
    string TestMessage(string Msg);
  }

  [Guid("FB294C42-90CD-4B46-B059-03816783DAB1")]
  [ComVisible(true)]
  [ClassInterface(ClassInterfaceType.None)]
  [ProgId("Testing.Test")]
  public class Test : ITest {

    public string TestMessage(string Msg) {
      if(String.IsNullOrEmpty(Msg)) {
        return "Hello World";
      } else {
        return "Hello " + Msg;
      }
    }

  }

  }

//-End------------------------------------------------------------------

Now it is necessary to compile this library, to create a Dynamic Link Library (DLL). It is not necessary to install any additional software for that. The dotNET Framework contains all we need. All we have to do is to execute the following command in a console:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:library /platform:x86 /out:COMTest_x86.dll COMTest.cs

Now it is necessary to register the library in the Windows registry. All COM libraries are registered here. All we have here to do is to execute the following command in a console:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe /tlb:COMTest_x86.tlb /codebase COMTest_x86.dll

Independent Test with VBScript

To check our library we use a tiny test with a VBScript program.

'-Begin-----------------------------------------------------------------

'-Sub Main--------------------------------------------------------------
Sub Main()

  On Error Resume Next
  Set Test = CreateObject("Testing.Test")
  If Not IsObject(Test) Then
    MsgBox "Can't create Testing.Test", vbOkOnly, "Important hint"
    Exit Sub
  End If
  On Error Goto 0

  MsgBox(Test.TestMessage(Null))
  MsgBox(Test.TestMessage("Stefan"))

End Sub

'-Main------------------------------------------------------------------
Main

'-End-------------------------------------------------------------------

Here the use of the Program ID becomes visible, at CreateObject. If it works correctly we have done everything right. This functional test is very important and helpful, because it excludes further interfering factors.

Invoke COM Method

Now we can use the COM library with the method with Invoke COM Method. As you can see nothing else is done than in the VB Script. An object of a class with the program ID Testing.Test is created and the method TestMessage is called. The argument Msg is empty.

image

image

And it delivers exactly the expected result.

image

It’s all very simple, isn’t it.
:wink:

Restriction

As far as I can see an instance is created for each Invoke COM Method call. As far as I know, dependencies that refer to one object cannot be used on this way.

'Not possible
Set Test = CreateObject("Testing.Test")
Test.SetAttribute = 25
Test.TestMessage(Null)

'That is the way
Set Test = CreateObject("Testing.Test")
Test.SetAttribute = 25

Set Test = CreateObject("Testing.Test")
Test.TestMessage(Null)

It is possible to set properties via the BindingFlags, but unfortunately I have not found a way to apply this to one instantiated class. If anyone knows, please let me know.

image

Conclusion

Using Invoke COM Method actvitiy is very simple and easy to understand. On this way we are able to use a large part of methods from the COM universe on an easy way in our automation workflows. Via the COM interface we can also use these approaches in many other contexts. Not the fanciest way, but solid and consolidated.

Addendum 11.09.2022: The approach presented here is deprecated, because it can’t work in the Windows compatibility mode.

8 Likes