Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Herr Poffe

About COM initialization

This topic is 5303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();
}
};

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!