Declaring and using COM interfaces in C#

P

Peter Zwosta

I have two C# projects:
- In project1 TestClass implements a COM interface ITest and builds a
class library (Register for COM interop = true)
- In project2 I create TestClass and want to typecast it to ITest to
call Init. The cast leads to an InvalidCastException.
For communication between .NET objects, type identity is scoped
by the assembly that defines the type. ...
Why are you using COM here at all?

Project 2 is a test container for project 1. What I'm actually trying
is to write a Filter-Dll for the Microsoft Indexing Service. The
Filter Dll must implement the IFilter interface. Project 2 searches
the windows registry for filter dlls, creates them, casts them to
IFilter and calls the IFilter-methods. That perfectly works for
filters written in any other language and it seems apart from filters
wirtten in c#?
So ITest in your first assembly is
different from ITest in the second, even if they happen to be defined
identically and exposed to COM with a single IID.

I excpected the statement [ComImport] to make it clear that it's not
an c# interface but from COM imported.

[ComImport]
[Guid("89BCB740-6119-101A-BCB7-00DD010655AF")]
public interface IFilter
{ void Init(); }

Thank you
Peter


PS: Sorry wasn't able to write a follow-up to the last thread
 
M

Mattias Sjögren

Peter,
I excpected the statement [ComImport] to make it clear that it's not
an c# interface but from COM imported.

But the implementer is still a .NET class, and therefore .NET rules
apply.

The easiest solution for this is to ensure that you're using a single
definition of IFilter in both projects. Either by referencing one from
the other of by moving the definition to a separate third assembly.



Mattias
 
P

peter.zwosta

Mattias said:
The easiest solution for this is to ensure that you're using a single
definition of IFilter in both projects. Either by referencing one from
the other of by moving the definition to a separate third assembly.

After what you said, a third assembly can't be the solution of all
problems. I want to implement a COM interface without knowing what
programs will use it. Of course I know of my test container and the Dll
can share a third assemly with my test container. What I don't know is
what other .Net assemlies will use the interface without knowing about
my third assembly.

Thank you
Peter
 
M

Mattias Sjögren

Peter,
After what you said, a third assembly can't be the solution of all
problems. I want to implement a COM interface without knowing what
programs will use it. Of course I know of my test container and the Dll
can share a third assemly with my test container. What I don't know is
what other .Net assemlies will use the interface without knowing about
my third assembly.

Right, this can be a problem, and it's what primary interop assemblies
are meant to solve. But AFAIK Microsoft haven't released a PIA for
IFilter.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top