Tip

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;  		
    if  (HRESULT_FACILITY(hr) == FACILITY_WINDOWS) 		
	hr = HRESULT_CODE(hr);
   		wchar_t *pwszStatus;
 		// FormatMessageW is the Unicode version of FormatMessage.
 		FormatMessageW(
 			FORMAT_MESSAGE_ALLOCATE_BUFFER |
 			                FORMAT_MESSAGE_FROM_SYSTEM,
 			NULL,
 			hr,
 			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
 			(LPWSTR)&pwszStatus,
 			0,
 			NULL 		);
  		// MessageBoxW

    Requires Free Membership to View

is the Unicode version MessageBox. MessgeBoxW(NULL, pwszStatus, L"DisplayError", MB_OK); LocalFree(pwszStatus); }

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 http://www.oreilly.com/


This was first published in January 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.