2
votes

Instruction on accessing a interface to an application, in plain C/C++ without:

  • MFC
  • ATL
  • WTL

Basically, I would like to make use of a COM object.

Working sample source code or guidance - to using (functionality) a COM object, not creating a COM server.

Regards

5
I'd really suggest you look at ATL. (WTL won't help; MFC is bloated) The 3 helper classes CComPtr, CComBSTR, CComVariant will make your life infinitely easier, & for using other COM objects you don't need much more than that.Jason S
Hi, I'd consider ATL - for simplicity, but I'd rather/like to learn/handle all control manually. Thanks :)Aaron

5 Answers

6
votes

Here is a simple example in plain C++:

CoInitialize(NULL); // absolutely essential: initialize the COM subsystem
IMyInterface* pIFace;
// create the object and obtain a pointer to the sought interface
CoCreateInstance(CLSID_MyObject, NULL, CLSCTX_ALL, IID_IMyInterface, &pIFace);
pIFace->MethodIReallyNeed(); // use the object
pIFace->Release(); // free the object
CoUninitialize(); // cleanup COM after you're done using its services

Copied from here: COM Basics

3
votes

There's an article on CodeProject, Introduction to COM - What It Is and How to Use It that you may find useful. It gives a pretty good introduction and a worked example.

1
votes

Well, assuming you have the interface declaration, all you need is a call to CoInitialize in order to initialize COM, then CoCreateInstance to get your instance (and use it), and then optionally, CoUnInitialize to uninitialize COM.

0
votes

Avoid codeproject (newbie), and see all MSDN chapters about COM.

Everything is there, with tons of C and C++ sample codes

0
votes

Just refer a good book on COM (Don Box or Dale Rougerson). Those are the good starting points to COM World.