1

I am trying to use InteropServices.Marshal.QueryInterface to convert a COM object to a specific type in C#, but I am having trouble to pass in the first and last parameters.

I am assuming that IntPtr is a reference, but I cannot get IntPtr to reference my classes to use QueryInterface due to syntax errors.

Of course, the code that is shown below does not have the correct syntax, but it gives you an idea of what I am trying to do.

set
{
    ClassA a;
    Guid guid = Guid.Parse("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
    System.Runtime.InteropServices.Marshal.QueryInterface(ref value, 
        ref guid, out a);
}

EDIT:

[ComVisible(true), Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
public ClassA : COMInterface{ ... }

ClassA is the C# class, and COMInterface is the COM interface. It is the only class that implements COMInterface. value in the set scope is the reference to the COM Interface, COMInterface

7
  • 1
    Is the type in C# an interface? Does the COM object implement that interface? Can you show the definition of the interface? Commented Sep 25, 2012 at 14:26
  • ClassA is the C# class, and it implements a COMInterface, which is the COM interface. It is the only class that implements the COMInterface. The value is the reference to the COM interface. Commented Sep 25, 2012 at 14:39
  • What do you mean by "put an interface between"? That doesn't make sense. Commented Sep 25, 2012 at 14:45
  • I meant ClassA : InterfaceB, and InterfaceB : COMInterface. Ya, never mind. Commented Sep 25, 2012 at 14:48
  • This is really a duplicate of yesterday's stackoverflow.com/questions/12557544/… Commented Sep 25, 2012 at 14:52

1 Answer 1

3

You're not going to be able to do this.

The IntPtr that you have is a pointer to the implementation of the COMInterface. It's perfectly feasible that you could have a completely separate class implementing COMInterface and that's what you're holding a pointer to.

That said, the call to QueryInterface can only be used to get a reference to the .NET representation of COMInterface, not an instance of ClassA; it would require you to shape the contents of the class implementing COMInterface in unmanaged code into something that managed code can understand and then write it to ClassA which is a completely different thing altogether.

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

4 Comments

Is it possible to put an interface, say InterfaceB, between COMInterface and ClassA. Is it possible to cast the COMObject to InterfaceB?
@swtdrgn What do you mean by "put an interface between"? That doesn't make sense.
Could you explain what are you trying to achieve? forcing an interface into a class makes no sense with or without COM. Imagine normal C# implementation of interfaces and classes - if you would have an ILog fileLog interface instance implemented by FileLog class, and you call ConsoleLog l = (ConsoleLog)fileLog what would you expect?
I was planning to write a self-wrapping class, and the first created instance comes back to this class (of a different instance).

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.