Problem solve Get help with specific problems with your technologies, process and projects.

DCOM Debugging Techniques

DCOM Debugging Techniques

From Learning DCOM by Thuan L. Thai, O'Reilly and Associates, 1999

Here is one simple, but very powerful, technique for debugging distributed components.

Using the FormatMessage Function

Recall that you must check the return HRESULT for all COM API function an method invocations, because the HRESULT is an important element in determining what has gone wrong in the most recent COM-related call. In the following code snippet, if the returned HRESULT is bad, we call a special helper function, DisplayError, to the error message associated with the HRESULT:

HRESULT hr = pOcr->OcrImage( ); 	
if (FAILED(hr)) DisplayError(hr); 

To obtain the textual representation of a system-defined HRESULT, you can use the FormatMessage Win32 API function. Here's the DisplayError helper function that uses FormatMessage to obtain the error string with a particular status code.

void DisplayError (HRESULT hr) 	{ 		
    if  (hr == S_OK) return;  		
	hr = HRESULT_CODE(hr);
   		wchar_t *pwszStatus;
 		// FormatMessageW is the Unicode version of FormatMessage.
 			NULL 		);
  		// MessageBoxW is the Unicode version MessageBox.
 		MessgeBoxW(NULL, pwszStatus, L"DisplayError", MB_OK);

The previous code snippet displays the error string associated with an HRESULT. This information helps tremendously in debugging your code, since it presents to you the exact problem that has occurred in the last COM-related invocation. For example, if a piece of client code calls CoCreateInstanceEx to activate a COM class but the class is not registered, you'll receive a message saying that "The class is not registered." Knowing the exact error helps you take the appropriate actions.

Note that a helper function like DisplayError can be used on both the client and server side. Whenever you receive a bad HRESULT, call this function to find out exactly what went wrong.

The O'Reilly book Learning DCOM has two other techniques for debugging distributed components. It is available at

Dig Deeper on Windows client management

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.