sharptooth
sharptooth

Reputation: 170509

How to translate RPC_STATUS into HRESULT?

In my COM component I need to translate all errors into the most suitable HRESULT values possible. Currently if I call some RPC interface method (I call a MIDL-generated stub that in turn calls NdrClientCall2()) and the call fails I return E_FAIL which is not very convenient.

There's so-called facility in HRESULT. Can I use this?

I tried to do the following:

HRESULT RpcStatusToHresult( RPC_STATUS status )
{
     if( status <= 0 ) {
         return status;
     }
     return ( status & 0x0000FFFF ) | (FACILITY_RPC << 16) | 0x80000000;
}

Will this properly translate RPC_STATUS to meaningful HRESULTs?

Upvotes: 3

Views: 1842

Answers (2)

Kevin
Kevin

Reputation: 66

Your RpcStatusToHresult(status) is equivalent to MAKE_HRESULT(1, FACILITY_RPC, status). HRESULT_FROM_WIN32(status) is equivalent to MAKE_HRESULT(1, FACILITY_WIN32, status).

I, like you, had guessed the former would be correct, but in practice, at least as far as getting a proper error message from FormatMessage() is concerned, the latter is what actually works.

Upvotes: 3

sam msft
sam msft

Reputation: 597

FWIW, that looks the same as HRESULT_FROM_WIN32

Upvotes: 1

Related Questions