Using TraceSrv

[Previous] [Next]

As you can imagine, using TraceSrv is fairly easy. In fact, after all the fun of getting the service running, explaining the details would be anticlimactic. The code on the companion CD calls TraceSrv from three different languages. I wanted to show you the VBScript example, which is in Listing 11-4, to give you an idea of how easy TraceSrv is to use. The C++ example is more interesting in that you need to convert your trace strings to BSTRs before you can call in to TraceSrv. Listing 11-5 show the DCOMTEST.CPP program in action.

Listing 11-4 Calling TraceSrv from VBScript

<SCRIPT LANGUAGE="VBScript"><!-- Dim g_TraceObj set g_TraceObj = CreateObject ( "TraceSrv.Trace.1" ) g_TraceObj.Trace( "This is a test of the...?" + vbCRLF ) Sub ButtonOne_OnClick g_TraceObj.Trace ( "Hey! I'm Button One!" + vbCRLF ) End Sub Sub ButtonTwo_OnClick g_TraceObj.Trace ( "VBScript is dangerous!" + vbCRLF ) End Sub --></SCRIPT>

Listing 11-5 Calling TraceSrv from a C++ program (DCOMTEST.CPP)

void main ( void ) { HRESULT hr ; ITrace * lpTrace ; IUnknown * lpUnknown ; // Initialize the COM+ libraries. hr = CoInitializeEx ( NULL , COINIT_APARTMENTTHREADED ) ; if ( FAILED ( hr ) ) { printf ( "Unable to initialize COM+\n" ) ; return ; }   hr = CoCreateInstance ( CLSID_Trace , NULL , CLSCTX_SERVER , IID_IUnknown , (LPVOID*)&lpUnknown ) ; if ( FAILED ( hr ) ) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr, MAKELANGID ( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPTSTR) &lpMsgBuf, 0, NULL ); printf ( "CoCreateInstanceEx failed: 0x%08X\n" , hr ) ; printf ( "FormatMessage returned: %s\n" , lpMsgBuf ) ; return ; } hr = lpUnknown->QueryInterface ( IID_ITrace , (LPVOID*)&lpTrace ) ; lpUnknown->Release ( ) ; if ( FAILED ( hr ) ) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr, MAKELANGID ( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPTSTR) &lpMsgBuf, 0, NULL ); printf ( "QueryInterface failed: 0x%08X\n" , hr ) ; printf ( "FormatMessage returned: %s\n" , lpMsgBuf ) ; return ; } OLECHAR * pszTemp ; pszTemp = SysAllocString ( OLESTR ( "Hello from a C++ program!!!" ) ); lpTrace->Trace ( pszTemp ) ; SysFreeString ( pszTemp ) ; lpTrace->Release ( ) ; CoUninitialize ( ) ; }



Debugging Applications
Debugging Applications for MicrosoftВ® .NET and Microsoft WindowsВ® (Pro-Developer)
ISBN: 0735615365
EAN: 2147483647
Year: 2000
Pages: 122
Authors: John Robbins

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net