On C # it is necessary to connect DLL working with COM technology.

Connecting the COM-DLL and calling functions from it I wrote - documentation sea. But in one function, the DLL requires that a pointer to IDispatch be passed as a parameter. B in this index on IDispatch two standard COM interfaces were implemented: IMsgBox and IPlatformInfo

Can this be done in C #? If so, can we have a good example?

Update

Do not tell me: the interfaces I need are inherited from IUnknown . Do I also need to implement the IUnknown interface with its functions ( QueryInterface , AddRef ...)?

I need to get this interface:

 MIDL_INTERFACE("55272A00-42CB-11CE-8135-00AA004BB851") IPropertyBag : public IUnknown { public: virtual /* [local] */ HRESULT STDMETHODCALLTYPE Read( /* [in] */ LPCOLESTR pszPropName, /* [out][in] */ VARIANT *pVar, /* [unique][in] */ IErrorLog *pErrorLog) = 0; virtual HRESULT STDMETHODCALLTYPE Write( /* [in] */ __RPC__in LPCOLESTR pszPropName, /* [in] */ __RPC__in VARIANT *pVar) = 0; }; 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

The first. Add to your project a link to the COM type library in the studio, or otherwise get the IMsgBox and IPlatformInfo interfaces.

The second. Implement these interfaces in some class. To make this class in a special way is not necessary:

 class MyClass : IMsgBox, IPlatformInfo { // ... } 

Third. Import a function from a DLL through P / Invoke, specifying MarshallAs ( IDispatch ):

 [DllImport("bar")] static extern void Foo([MarshallAs(UnmanagedType.IDispatch)] object obj) 

Regarding the update. No, you do not need to implement IUnknown on your own, just like IDispatch. These interfaces will be implemented by the environment.

  • And to transfer in obj - what? MyClass? But then the compiler rightly curses: "The given cast is invalid." - Dmitry Garbuz
  • And to transfer in obj - what? MyClass? But then the compiler rightly curses: "The given cast is invalid." - Dmitry Garbuz
  • @DimitriGarbuz do not believe. Check again if you did everything right. - Pavel Mayorov
  • It worked only after I marked the class with attributes: [ComVisible (true)] [ClassInterface (ClassInterType.AutoDispatch)] - Dmitry Garbuz
  • @DmitryGarbuz e ... If a class implements the necessary interfaces - this is not necessary. - Pavel Mayorov