0

Since the function's return value is utilized for error message, how do the functions return the necessary information back to the caller?

For example: IDirect3D9::CreateDevice method

Mgetz
  • 4,674
  • 2
  • 29
  • 46
zzz
  • 131
  • 4
  • 12

1 Answers1

1

So if you take a look at that link you'll notice that it has some parameters marked Out this is important because this denotes what will be returned to the caller.

HRESULT CreateDevice(
[in]           UINT Adapter,
[in]           D3DDEVTYPE DeviceType,
[in]           HWND hFocusWindow,
[in]           DWORD BehaviorFlags,
[in, out]      D3DPRESENT_PARAMETERS *pPresentationParameters,
[out, retval]  IDirect3DDevice9 **ppReturnedDeviceInterface
);

In the above sample (copied and pasted from the MSDN link), you'll notice a parameter ppReturnedDeviceInterface is marked as being ** or a pointer to a pointer, the caller would pass in a the address of their pointer and would be returned a pointer at that address. Also the D3DPRESENT_PARAMETERS structure passed in to pPresentationParameters will be updated on return, as noted by the out annotation. Ex:

IDirect3DDevice9 *pDevice = NULL;

HRESULT hr = pD3D->CreateDevice(
                     D3DADAPTER_DEFAULT,
                     D3DDEVTYPE_HAL,
                     hwnd,
                     pPresentationParams,
                     &pDevice);
 if(SUCCEEDED(hr))
 {
    //pDevice should be non null at this point
 }
Mgetz
  • 4,674
  • 2
  • 29
  • 46
  • So, the caller is CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, pPresentationParams, &pDevice); while the value is return through D3DPRESENT_PARAMETERS *pPresentationParameters. Am I right? – zzz Jul 01 '13 at 14:33
  • There are actually two returned values, the `D3DPRESENT_PARAMETERS` is updated (as indicated by being marked `out`) and `pDevice` is assigned with a pointer to an `IDirect3DDevice9`. All of this is of course assuming that the return value of function is `S_OK` if not... all bets are off. – Mgetz Jul 01 '13 at 14:38
  • 1
    @zzz be ware of the terms return value and output parameters. – zdd Jul 17 '13 at 13:15
  • @zdd Correct, to make it more confusing Microsoft uses an annotation to declare a "Return Value" in the parameters that is the actual "Returned Value" without breaking the COM function signature requirements. – Mgetz Jul 17 '13 at 15:24