Using COM Interfaces

When using COM interfaces, a similar approach is taken as when using unmanaged API's. The interface needs to be declared, using custom attributes to describe the type interface and the GUID. Next the methods are declared; using the same approach as for unmanaged API's. The following example uses the IAutoComplete interface, defined as follows in Delphi 7:

IAutoComplete = interface(IUnknown)

['{00bb2762-6a77-11d0-a535-00c04fd7d062}'] function Init(hwndEdit: HWND; punkACL: IUnknown;

pwszRegKeyPath: LPCWSTR; pwszQuickComplete: LPCWSTR): HRESULT; stdcall; function Enable(fEnable: BOOL): HRESULT; stdcall; end;

In Delphi 8 for .NET it is declared as follows:

[ComImport, GuidAttribute('00BB2762-6A77-11D0-A535-00C04FD7D062'), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] IAutoComplete = interface function Init(hwndEdit: HWND; punkACL: IEnumString;

pwszRegKeyPath: IntPtr; pwszQuickComplete: IntPtr): HRESULT; function Enable(fEnable: BOOL): HRESULT; end;

Note the custom attributes used to describe the GUID and type of interface. It is also essential to use the ComImportAttribute class. There are some important notes when importing COM interfaces. You do not need to implement the IUnknown/IDispatch methods, and inheritance is not supported.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment