First-time using Interop


A

adx

I want to use an interface of a COM object (implemented in C++) using
Interop.

1. Should it be simply just implement an interface on C# side with syntax
like below?

[ComImport]
[Guid("xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ITestInterface
{
[PreserveSig]
int GetTestLevel(out int level);

2. Do all methods in the original interface have to be implemented on C#
side?

3. Should "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx" be the interface GUID or CoClass
ID of the COM object?

4. What do "[ComImport]" and "[PreserveSig]" mean?
 
Ad

Advertisements

M

Mattias Sjögren

1. Should it be simply just implement an interface on C# side with syntax
like below?

[ComImport]
[Guid("xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ITestInterface
{
[PreserveSig]
int GetTestLevel(out int level);

2. Do all methods in the original interface have to be implemented on C#
side?

You don't have to implement the interface if you're just consuming an
existing class that implements it.

3. Should "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx" be the interface GUID or CoClass
ID of the COM object?

The IID if you're declaring an interface. The CLSID if you're
declaring a class.

4. What do "[ComImport]" and "[PreserveSig]" mean?

ComImport means that the type is originally defined in the COM world.
As a result, the type is not exported and registered by Tlbexp/Regasm
for example.

PreserveSig means you're taking responsability to handle the HRESULT
return value from the method. If you remove the attribute (and change
the return type to void) the CLR will do that for you and convert
error HRESULTs to corresponding exceptions.


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