Advertisement

Creating and using COM objects

Started by July 12, 2005 01:16 PM
5 comments, last by blueshogun96 19 years, 3 months ago
Ok, my question is how do I create my own com object (like creating a class)? I would like to use COM when I am programming in C so I can have sort of a C++ like class.
I can show you in ASP or VB :)

Dim objScripting As Object
Set objScripting = CreateObject("Scripting.Dictionary")
' use it here.
Set objScripting = Nothing
Advertisement
You can use and create COM classes in C. However, it is pretty awkward. Microsoft's examples (and others') typically only demonstrate using C++.

COM is not a good way to make your C program object-oriented. COM is used for communicating with bits of software (potentially) written in other languages or by other people.

You can of course emulate OO in C just by having structs with function pointers in; lots of people do this. But it's much better to just use C++

Mark
Well not only did I answer in a different language, but I also misunderstood your question. :P
No its not weird or awkward to use them, but not the answer for OOP C;
Use C++ or say hello to structs and function pointers. That's a way to fake it not a very good one though.
But heres an example how to use them.

CComPtr<CLASSHERE> name;

name->

name.

both resolve to whatever.

lets say name is an IWMPPlayer*;
IWMPControls* videoControls;
IWMP->get_controls(&videoControls);

(sorry the last example was incorrect that was getting a player from the view)

go along that line.

all of the com functions return an HRESULT you'll need to check fo S_OK if not act accordingly.
COM in C is a pain, but I wouldn't say it was wierd. It's more work than in C++ and the stuff in MSDN kind of implies that MS thinks you should be using C++, but you can find examples easily enough (although often used to illustrate why you should be using C++).

But you DEFINITELY don't want to go near COM without a very good reason. Faking classes is not what COM does - you have to learn about .IDL/ODL files to start with and understand what interfaces really do. COM lets you abstract implementation from interface like OO does but it's designed more for allowing cross-language support. COM is difficult and not too performance-friendly - why not just use C++ or implement your own vTables in C somehow?
Advertisement
Quote: Original post by d000hg
COM in C is a pain, but I wouldn't say it was wierd. It's more work than in C++ and the stuff in MSDN kind of implies that MS thinks you should be using C++, but you can find examples easily enough (although often used to illustrate why you should be using C++).

But you DEFINITELY don't want to go near COM without a very good reason. Faking classes is not what COM does - you have to learn about .IDL/ODL files to start with and understand what interfaces really do. COM lets you abstract implementation from interface like OO does but it's designed more for allowing cross-language support. COM is difficult and not too performance-friendly - why not just use C++ or implement your own vTables in C somehow?

Hey, sorry it's been so long since I replied to this thread :(

In certain projects (most actually), I like to avoid C++ as much as possible. Yeah, I know it's very unusual for a game programmer these days to use 100% pure C, but that is what I prefer. And if I have to use C++, I usually mix C and C++ files in my project.

This topic is closed to new replies.

Advertisement