Skip to main content
gamedev.net
🔒 Locked

About COM initialization

Started by Herr Poffe May 7, 2004 at 10:52 AM 2 replies 400+ views
Original Post
Herr Poffe
Herr Poffe
Howdy all! I am currently trying to implement direct sound in a game of mine and was thinking of the initialization of COM. The way my code looks at the moment I call "CoInitialize(NULL);" once for every sound object I wish to play. May this cause any problems or is it ok to call it as many times as I want? And also, must I call "CoUninitialize();" an equal amount of times or is one time enough? I read a little bit of the documentation but I am still quite sure so I really would appreciate some help.
Polymorphic OOP
Polymorphic OOP
quote:
Original post by Herr Poffe
Howdy all! I am currently trying to implement direct sound in a game of mine and was thinking of the initialization of COM. The way my code looks at the moment I call "CoInitialize(NULL);" once for every sound object I wish to play. May this cause any problems or is it ok to call it as many times as I want? And also, must I call "CoUninitialize();" an equal amount of times or is one time enough?

I read a little bit of the documentation but I am still quite sure so I really would appreciate some help.


You can call CoInitialize as many times as you want as long as everytime it succeeds you have a corresponding CoUninitialize when you are done.
nick5454
nick5454
use smart pointers they handle the coInitialize and add refs and what not.

Or call the objects class factory, unless you want to do it the hard way.
Shannon Barber
Shannon Barber
You only need to call CoInitialize (or CoIntializeEx) once per thread (and CoUninitialize prior to thread termination).

It reference counts, so you can call call it more than once (but need to match it up as mentioned above).

You can''t use a smart-pointer exactly, but I did make a CoInit class to handle this automatically.


class CCoInitMTA
{
public:
CCoInitMTA()
{
HRESULT hr;
//If this is undefined, put "#define _WIN32_WINNT 0x0500"

//alternatively, you can use #define _WIN32_WINNT 0x0501 for XP

// at the top of your StdAfx.h file - before *anything* else

if(FAILED(hr=CoInitializeEx(NULL, COINIT_MULTITHREADED)))
_com_issue_error(hr);
}

virtual ~CCoInitMTA()
{
CoUninitialize();
}
};

class CCoInitSTA
{
public:
CCoInitSTA()
{
HRESULT hr;
if(FAILED(hr=CoInitialize(NULL)))
_com_issue_error(hr);
}

virtual ~CCoInitSTA()
{
CoUninitialize();
}
};
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.