directx, COM and CoInitialize

Started by
11 comments, last by ctaclas 19 years, 5 months ago
my book says that directX uses COM, and in order to use COM you have to call CoInitialize. however, in my first little program that draws some sprites to the screen i didn't have any calls to CoInitialize. my question is do i need to use CoInitialize and COM? if i do what benefits does it give me?
Advertisement
Quote:Original post by ctaclas
my book says that directX uses COM, and in order to use COM you have to call CoInitialize. however, in my first little program that draws some sprites to the screen i didn't have any calls to CoInitialize.

my question is do i need to use CoInitialize and COM? if i do what benefits does it give me?


DirectX has always been based on COM, which is what gives it it's backwards compatability...

However, in the later versions of the SDK you don't need to mess with the "innards" of COM very much (you still do in DirectShow). I don't have the SDK to hand right now, but I'm pretty sure the various create functions are just aliases for the various CoInitialize/QueryInterface type calls..

As for benefits, backwards (and hence forwards) compatability is about all you're gonna get from it. There might be a few minor resource/performance issues - but I don't think they're particularly significant.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

I know for a fact that DirectMusic requires these lines because it uses Direct COM calls to initialise itself. (eg. CoCreateInstance(...)).

I think though that Direct3D and DirecInput don't can get away without the two calls, though as jollyjeffers said, COM is more than likely used internally and does COM initialisation for you.
I looked into this, and I found some unusual results. DirectInput, DirectShow, and DirectMusic are all COM-based (as you guys stated before). However, I can't find *any* Direct3D references to COM whatsoever.

Direct3DCreate9() makes no calls to any COM functions. The *Create*() functions (like IDirect3DDevice9::CreateVertexBuffer(), IDirect3DDevice9::CreateTexture(), D3DXCreateSprite(), D3DXCreateEffect(), ect...) instantiate the objects directly. They don't go through COM at all. Interesting...[wink]
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
Every Direct3D object contains an IUnknown Interface (do a watch on an IDirect3DDevice). So therefore Direct3D uses COM.

Even checked MSDN and all DirectX objects use COM

From MSDN for IDirect3D8:

This interface, like all COM interfaces, inherits additional functionality from the IUnknown Interface.

The LPDIRECT3D8 and PDIRECT3D8 types are defined as pointers to the IDirect3D8 interface.

so direct3d uses COM, as does all directx, but it takes care of COM initialization for me?
but there are other parts of directX that require com initialization?
so i might as well just include those calls, i think my book only does it in the function that creates the window anyway, it'll be just two lines of code i think :)
Quote:Original post by ctaclas
so direct3d uses COM, as does all directx, but it takes care of COM initialization for me?
That's right!

Quote:but there are other parts of directX that require com initialization?
Yes, like DirectPlay and DirectShow. (You rarely touch those though)

Quote:so i might as well just include those calls, i think my book only does it in the function that creates the window anyway, it'll be just two lines of code i think :)
Don't really understand what you mean by this, but if you're refering to COM code, DON'T WRITE IT! Seriously, 8% of all DirectX programmers knows COM. And those are the freaks who program for $10,000 a month. [grin] Just keep it simple, for D3D is!

Edited by Coder: Fixed mismatched quote

[Edited by - Coder on December 7, 2004 7:42:00 PM]
Quote:Original post by khalligan
Every Direct3D object contains an IUnknown Interface (do a watch on an IDirect3DDevice). So therefore Direct3D uses COM.

Yes, I know this. I was simply stating that D3D doesn't use COM in a traditional way. Technically, Direct3D interfaces are still COM objects, but they are definetly not instantiated using any COM calls (ie CoCreateInstance()), even inside the D3D Create functions.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
Quote:Original post by circlesoft
Quote:Original post by khalligan
Every Direct3D object contains an IUnknown Interface (do a watch on an IDirect3DDevice). So therefore Direct3D uses COM.

Yes, I know this. I was simply stating that D3D doesn't use COM in a traditional way. Technically, Direct3D interfaces are still COM objects, but they are definetly not instantiated using any COM calls (ie CoCreateInstance()), even inside the D3D Create functions.


I don't think it's too out of question to assume that D3D is handling these things "under the bonnet" for you. Have you considered that the Direct3DCreate( ) calls are just entries into the respective DLL that then does the necessary CoCreate*( ) stuff?

If you look up the definitions of IDirect3D?? type definitions in the header files (forget which exactly) they, at some point, seem to resolve down to GUID/UUID's. Been a while since I looked at it (cant remember specifics), but as with a lot of the D3D stuff that we directly use - it's just aliases and typedef's...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Quote:Original post by jollyjeffers
I don't think it's too out of question to assume that D3D is handling these things "under the bonnet" for you. Have you considered that the Direct3DCreate( ) calls are just entries into the respective DLL that then does the necessary CoCreate*( ) stuff?
Yea, that's certainly what I thought was going on, too. However, when I looked at the code, there were no COM-related calls be made what-so-ever. All of the D3D Create calls basically just instantiate an object (ie a VertexBuffer) using new.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )

This topic is closed to new replies.

Advertisement