4

I would like to implement a COM object in C# and also use it in C# (besides C++ and others).

Here is the code of the DLL implementing the COM object :

namespace TestComServer {
  [ComVisible(true),
  Guid("565D8202-6C0F-4AAB-B0F6-49719CD13045"),
  InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
  ]
  public interface ITestObject {
    void DoSomething();
  }

  [
  ComVisible(true),
  GuidAttribute("21293767-A713-49E2-968E-7DDE0B0DAB94"),
  ClassInterface(ClassInterfaceType.None)
  ]
  public class TestObject : ITestObject {
    public TestObject() {
    }

    public void DoSomething() {
    }
  }
}

I've used gacutil at add the DLL to the global assembly cache.

The EXE uses the COM object in the following way (like I successfully did with some COM objects implemented in C++):

[
ComImport(),
Guid("565D8202-6C0F-4AAB-B0F6-49719CD13045"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
public interface ITestObject {
  [PreserveSig ]
  void DoSomething();
}

[ComImport, Guid("21293767-A713-49E2-968E-7DDE0B0DAB94")]
public class TestObject {
}

private void button1_Click(object sender, EventArgs e) {
  object o = new TestObject();
  ITestObject t = (ITestObject)o;
  t.DoSomething();
}

When executing the line object o = new TestObject();, I get an InvalidCastException. What is wrong with this code?

An unhandled exception of type 'System.InvalidCastException' occurred in Test.exe Additional information: Das Objekt des Typs "TestComServer.TestObject" kann nicht in Typ "TestObject" umgewandelt werden.

9
  • Did you forget to have TestObject inherit from ITestObject? Commented Mar 5, 2012 at 22:58
  • Did you register the assembly as a COM object in the registry using regasm? regasm YourDll.dll /tlb Commented Mar 5, 2012 at 23:02
  • In the DLL implementation, I've inherited from ITestObject. In the EXE I cannot do that because I don't implement ITestObject, I would like to use the DLLs implementation like I did that with many C++ COM objects before. Commented Mar 5, 2012 at 23:05
  • 1
    Similar questions seem to indicate that a C#-defined COM object can only be used in a C# project as a .NET object, not through COM. See stackoverflow.com/q/9394133/385844 and stackoverflow.com/q/1482131/385844 Commented Mar 5, 2012 at 23:10
  • 1
    I'm afraid I can't be of much more help; my knowledge of COM interop is not deep enough to know if there would be some way to fool the marshaller into marshalling the C# object to COM and back. If your objective is to test a C# COM server, perhaps the answer is to write the tests in native C++ or another non-managed COM-capable language. If your objective is to consume COM-visible C# types in a .NET application, then obviously the answer is just to use the assemblies directly as managed assemblies. Or is your objective something else? Commented Mar 5, 2012 at 23:30

1 Answer 1

0

If you change the EXE so that it only imports the interface, then instantiate the object using Type.GetTypeFromCLSID and Activator.CreateInstance:

[
ComImport(),
Guid("565D8202-6C0F-4AAB-B0F6-49719CD13045"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
public interface ITestObject {
  [PreserveSig ]
  void DoSomething();
}

private void button1_Click(object sender, EventArgs e) {
  Type type = Type.GetTypeFromCLSID(new Guid("21293767-A713-49E2-968E-7DDE0B0DAB94"));
  object o = Activator.CreateInstance(type);
  ITestObject t = (ITestObject)o;
  t.DoSomething();
}

I've not used that method of importing COM objects before, so this might not work. Generally I define my interfaces in a separate assembly that is also exposed to COM that is then referenced by both the DLL and the EXE. The EXE instantiates the DLL using the above method and casts it to the .NET interface that it has referenced.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.