Following is a listing of the client program. We will describe all the important parts of this code in the following sections.
// RemoteClient.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" // added _WIN32_DCOM
#include
#include
#include
// extract definitions from server project
#include "..\RemoteServer\RemoteServer.h"
#include "..\RemoteServer\RemoteServer_i.c"
// forward reference for status display method
void ShowStatus( HRESULT hr );
int main(int argc, char* argv[])
{
HRESULT hr; // COM error code
IGetInfo *pI; // pointer to interface
// Get the server name from user
char name[32];
cout << "Enter Server Name:" <<>
gets( name );
_bstr_t Server = name;
// remote server info
COSERVERINFO cs;
// Init structures to zero
memset(&cs, 0, sizeof(cs));
// Allocate the server name in the COSERVERINFO struct
cs.pwszName = Server;
// structure for CoCreateInstanceEx
MULTI_QI qi[1];
memset(qi, 0, sizeof(qi));
// initialize COM
hr = CoInitialize(0);
ShowStatus( hr );
// macro to check for success
if (SUCCEEDED(hr))
{
// set a low level of security
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_NONE,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL);
// init security
ShowStatus( hr );
}
if (SUCCEEDED(hr))
{
// Fill the qi with a valid interface
qi[0].pIID = &IID_IGetInfo;
// get the interface pointer
hr = CoCreateInstanceEx(
CLSID_GetInfo, // clsid
NULL, // outer unknown
CLSCTX_SERVER, // server context
&cs, // server info
1, // size of qi
qi ); // MULTI_QI array
ShowStatus( hr );
}
if (SUCCEEDED(hr))
http://devcentral.iticentral.com/articles/DCOM/intro_DCOM/part3/default.php (3 of 11) [7/9/2001 2:53:00 PM]
DevCentral - Understanding DCOM - Part III
{
BSTR bsName; // Basic style string
// Extract the interface from the MULTI_QI strucure
pI = (IGetInfo*)qi[0].pItf;
// Call a method on the remote server
hr = pI->GetComputerName( &bsName );
ShowStatus( hr );
// Convert name to a printable string
_bstr_t bst( bsName );
cout << "Server Name :" <<>
// get time from remote computer
time_t tt;
hr = pI->GetTimeT(&tt );
ShowStatus( hr );
// display time_t as a string
cout << "Server Time :" <<>
// Release the interface
pI->Release();
}
// Close COM
CoUninitialize();
// Prompt user to continue
cout << "Press ENTER to continue" <<>
getchar();
return 0;
}
// Display detailed status information
void ShowStatus( HRESULT hr )
{
if (SUCCEEDED(hr))
{
cout << "OK" <<>
}
else
{
// construct a _com_error using the HRESULT
_com_error e(hr);
char temp[32];
// convert to hexidecimal string and display
sprintf( temp, "0x%x", hr );
cout << "Error : " <<>
// The hr as a decimal number
cout << "Decimal : " <<>
// show the 1st 16 bits (SCODE)
cout << "SCODE : " <<>
// Show facility code as a decimal number
cout << "Facility: " <<>
// Show the severity bit
cout << "Severity: " <<>
// Use the _com_error object to format a message string. This is
// Much easier then using ::FormatMessage
cout << "Message : " <<>
}
}
0 comments:
Post a Comment