How to use IDispatch in simple C to call a COM object

I need to compile some code using the gcc compiler included in the R tools (R is a statistical program for Windows), the problem is that I need to use IDispatch in my code to create access to the COM methods of the object, and the gcc compiler does not support much part of the code that I use for this, which is mostly C ++ code.

So my question is how can I use IDispatch in C to create a COM object, independent of MFC, .NET, C #, WTL or ATL. I believe that if I do this, I can compile my code without any problems.

+12
source share
3 answers

CodeProject has an article called "COM in Simple C".

Here is a link to Part 1 .

There is a lot of very good information about working with COM in C in this article and in the subsequent comments of the author (I think there are 3 or 4 in the series).

Edit:
I was wrong, there are 8 parts!

Part 2
Part 3
Part 4
Part 5
Part 6
Part 7
part 8

+15
source

In general, the C ++ IDispatch interface is just a table of function pointers. In C, it looks something like this:

typedef struct {
  HRESULT(*pQueryInterface)(void* this, REFIID riid, void **ppvObject);
  ULONG(*pAddRef)(void* this);
  ULONG(*pRelease)(void* this);
  HRESULT(*pGetTypeInfoCount)(void* this, unsigned int* pctInfo);
  HRESULT(*pGetTypeInfo)(void* this, unsigned int iTInfo,LCID lcid, ITypeInfo** ppTInfo);
  HRESULT(*pGetIDsOfNames)(void* this, REFIID riid, OLECHAR** rgszNames, unsigned int cNames, LCID lcid, DISPID* rgDispId);
 HRESULT(*pInvoke)(void* this, DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags,DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, unsigned int* puArgErr);
} IDispatch_in_C;

Note that each method has this pointer as the first parameter, and you will need to define more types such as ITypeInfo, REFIID, DISPID, etc. etc.

, . ++- C.

+3

You can also use the disphelper library.

+3
source

Source: https://habr.com/ru/post/1652572/


All Articles