#### Archived

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

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

## 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 on other sites
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 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 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();					}			};