out parameters - How should I check that [out] params in COM can be used? -


Officially one should not use the [out] parameters from COM functions unless The function is not successful, it means that there can be (at least) three ways to see if any [out] parameter can be used

the following Consider the interface

  Interface IFoo: IUnknown {HRESULT GetOtherFoo ([Out] IFoo ** ppFoo); HRESULT bar (); };  

Which of the following ways would you advise to use it?

Check 1 return value

  CComPtr & lt; IFoo & gt; other; HRESULT hr = foo- & gt; GetOtherFoo (and others); If (SUCCEEDED (HR)) other-> bar ();   

IFoo due to a bug in NULL pointer may cause Dirensing, it would bother me a bit I

Check the 2 output parameters

It depends on the fact that if a method fails, then it will be [out] Parameter (if parameter is changed & lt; ==> it is safe to use).

  CComPtr & lt; IFoo & gt; other; Foo- & gt; GetOtherFoo (& amp; Other); If (other) other-> bar ();  

Note that this is none anyway, CComPtr of destructors Release will call if the pointer is not < Code> NULL therefore it can not be trash

Both in a crazy way

  CComPtr & lt; IFoo & gt; Check both. other; HRESULT hr = foo- & gt; GetOtherFoo (and others); If (SUCCEEDED (HR) and others) other-> times ();  

This is a bit verbose in my opinion.


P.S. See.

If you want to write more checks and make it more reliable, you can slow down the code. Option 3 is for you because you expect COM server There are bugs in it, it is fair enough to investigate against them.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -