Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a better way to create an object?

8 views
Skip to first unread message

Barzo

unread,
Oct 12, 2009, 10:15:06 AM10/12/09
to
Hi,

in my project I have a "noncreatable" object.
The IDL (simplified) is the following:

[
object,
uuid(AB566C6D-AF54-11DE-B5BE-00A0D15E9B20),
oleautomation,
nonextensible,
helpstring("InterfaceA Interface"),
pointer_default(unique)
]
interface IAudioMixer : IUnknown
{
[propput, id(1)]
HRESULT PropertyA([in] short arg);
[propget, id(1)]
HRESULT PropertyA([out, retval] short* pVal);
};
//--------------------------------------------------
[
uuid(AB566C6F-AF54-11DE-B5BE-00A0D15E9B20),
dual,
nonextensible
]
interface IAudioPlayer : IDispatch
{
[propget, id(3), helpstring("Returns a reference to a IAudioMixer
interface.")]
HRESULT GetMixer([out, retval] IAudioMixer** pVal);
};

//--------------------------------------------------

[
uuid(AB566C66-AF54-11DE-B5BE-00A0D15E9B20),
version(1.0),
helpstring("....")
]
library MyLibrary
{
importlib("stdole2.tlb");


[
uuid(AB566C72-AF54-11DE-B5BE-00A0D15E9B20),
helpstring("AudioPlayer Class")
]
coclass AudioPlayer
{
[default] interface IAudioPlayer;
};

[
uuid(AB566C80-AF54-11DE-B5BE-00A0D15E9B20),
noncreatable,
helpstring("AudioMixer Class")
]
coclass AudioMixer
{
[default] interface IAudioMixer;
};
}


Now, in the CAudioPlayer class I have a member:

CComPtr<IAudioMixer> mixer_;

and in the FinalConstruct I have to create the object:


CAudioMixer* p_mixer;
IAudioMixer* i_mixer;

HRESULT hr = CComCoClass<CAudioMixer>::CreateInstance(&i_mixer);

if (SUCCEEDED(hr))
{
p_mixer = dynamic_cast<CAudioMixer*>(i_mixer);
p_mixer->SetMixerObj( player_->getAudioMixer() );
mixer_.Attach(i_mixer);
}

I coded in this way because CAudioMixer::SetMixerObject is not part of
the IAudioMixer interface.
Is there a better way to do the same job?

Thanks,
Daniele.

SvenC

unread,
Oct 12, 2009, 11:23:04 AM10/12/09
to
Hi Daniele,

> Now, in the CAudioPlayer class I have a member:
>
> CComPtr<IAudioMixer> mixer_;
>
> and in the FinalConstruct I have to create the object:
>
> CAudioMixer* p_mixer;
> IAudioMixer* i_mixer;
>
> HRESULT hr = CComCoClass<CAudioMixer>::CreateInstance(&i_mixer);
>
> if (SUCCEEDED(hr))
> {
> p_mixer = dynamic_cast<CAudioMixer*>(i_mixer);
> p_mixer->SetMixerObj( player_->getAudioMixer() );
> mixer_.Attach(i_mixer);
> }
>
> I coded in this way because CAudioMixer::SetMixerObject is not part of
> the IAudioMixer interface.
> Is there a better way to do the same job?

"Better" depends on your taste, I guess ;-)

I do it like this:
CComObject<CAudioMixer> *pMixer = NULL;
hr = pMixer->CreateInstance(&pMixer);
if(SUCCEEDED(hr) && pMixer)
{
pMixer->SetMixerObj(player_->getAudioMixer());
hr = pMixer->QueryInterface(&mixer_);
}
if(pMixer && !mixer_)
delete pMixer;
pMixer = NULL;

// now use mixer_ if it is not NULL

--
SvenC

Barzo

unread,
Oct 12, 2009, 11:37:02 AM10/12/09
to
On 12 Ott, 17:23, "SvenC" <Sv...@nospam.nospam> wrote:
>
> "Better" depends on your taste, I guess ;-)
>

Yes, of course....
I'm not an expert in ATL and I asked only to know if there was some
"standard way" :-)

> I do it like this:
> CComObject<CAudioMixer> *pMixer = NULL;
> hr = pMixer->CreateInstance(&pMixer);
> if(SUCCEEDED(hr) && pMixer)
> {
>   pMixer->SetMixerObj(player_->getAudioMixer());
>   hr = pMixer->QueryInterface(&mixer_);}
>
> if(pMixer && !mixer_)
>   delete pMixer;
> pMixer = NULL;
>

Thanks for your suggestion Sven!

Cheers,
Daniele.

SvenC

unread,
Oct 12, 2009, 12:01:49 PM10/12/09
to
> Thanks for your suggestion Sven!

You're welcome,
SvenC

0 new messages