//
// Replace the <...> placeholder in main() with correct data for
execution
//
#include <stdio.h>
#include <atlbase.h>
// Typedef for REMOTE_DESKTOP_SHARING_CLASS
typedef enum
{
DESKTOPSHARING_DEFAULT = 0x0,
NO_DESKTOP_SHARING = 0x1,
VIEWDESKTOP_PERMISSION_REQUIRE = 0x2,
VIEWDESKTOP_PERMISSION_NOT_REQUIRE = 0x4,
CONTROLDESKTOP_PERMISSION_REQUIRE = 0x8,
CONTROLDESKTOP_PERMISSION_NOT_REQUIRE = 0x10
} REMOTE_DESKTOP_SHARING_CLASS;
// import the type library
#import "HelpServiceInterfaces.tlb" rename_namespace("HSITLB")
named_guids raw_interfaces_only no_auto_exclude \
rename("GetUserName", "GetUserName_Renamed")\
rename("EncryptFile", "EncryptFile_Renamed")\
rename("DecryptFile", "DecryptFile_Renamed")\
rename("ULONG_PTR","ULONG_PTR1")
//+--------------------------------------------------------------------
-------
//
// Function: CallRemoteUserSessionInfoAPI
//
// Synopsis: Call RemoteUserSessionInfo API
//
// Arguments: bstrMachineName [in] - Remote MachineName
//
//---------------------------------------------------------------------
-------
HRESULT CallRemoteUserSessionInfoAPI(
IN BSTR bstrMachineName)
{
HRESULT hr = S_OK;
COSERVERINFO si; ::ZeroMemory( &si, sizeof( si ) );
MULTI_QI qi; ::ZeroMemory( &qi, sizeof( qi ) );
CComPtr<HSITLB::IPCHService> pIsvc = NULL;
CComPtr<IUnknown> pIUnkEnum;
CComPtr<IEnumVARIANT> pIEnum;
CComPtr<HSITLB::ISAFSession> pIItem;
CComPtr<IDispatch> pIDispItem = NULL;
CComPtr<HSITLB::IPCHCollection> pIColl = NULL;
long lsizeCollection = 0;
long lIndex = 0;
// The values returned by the APIs
BSTR bstrUserName = NULL;
BSTR bstrDomainName = NULL;
DWORD dwSessionID = 0;
HSITLB::SessionStateEnum SessionState;
CComVariant varSAFSession;
ULONG ulFetched;
si.pwszName = bstrMachineName;
qi.pIID = &(HSITLB::IID_IPCHService);
hr = CoCreateInstanceEx(HSITLB::CLSID_PCHService, NULL,
CLSCTX_REMOTE_SERVER, &si, 1, &qi );
if(hr != S_OK) goto ErrReturn;
// Get the IPCHService interface pointer to the pIsvc local pointer
pIsvc.Attach((HSITLB::IPCHService*) qi.pItf );
// RemoteUserSessionInfo() returns a pointer to IPCHCollection
interface
hr = pIsvc->RemoteUserSessionInfo(&pIColl);
if(hr != S_OK) goto ErrReturn;
if(pIColl == NULL)
{
// Display the error code
printf("\n The novice has Unsolicited turned off");
goto ErrReturn;
}
hr = pIColl->get_Count(&lsizeCollection);
if(hr != S_OK) goto ErrReturn;
hr = pIColl->get__NewEnum(&pIUnkEnum);
if(hr != S_OK) goto ErrReturn;
hr = pIUnkEnum.QueryInterface(&pIEnum);
if(hr != S_OK) goto ErrReturn;
if(lsizeCollection == 0)
{
// Display the error code
printf("\n There are no users logged into this machine");
goto ErrReturn;
}
printf("\n\t RemoteUserSessionInfo \n");
// For each distinct User Session on the machine you are pointing
at
// get their info
for (lIndex=0; lIndex < lsizeCollection; lIndex++)
{
// Display the list of session and user info
hr = pIEnum->Next( 1, &varSAFSession, &ulFetched);
if ((hr == S_FALSE) || (ulFetched != 1))
{
printf("\n Next failed! No more records!");
break;
}
pIDispItem = varSAFSession.pdispVal;
hr = pIDispItem.QueryInterface(&pIItem);
if(hr != S_OK) goto ErrReturn;
hr = pIItem->get_UserName(&bstrUserName);
if(hr != S_OK) goto ErrReturn;
hr = pIItem->get_DomainName(&bstrDomainName);
if(hr != S_OK) goto ErrReturn;
hr = pIItem->get_SessionID(&dwSessionID);
if(hr != S_OK) goto ErrReturn;
hr = pIItem->get_SessionState(&SessionState);
if(hr != S_OK) goto ErrReturn;
printf("\n MachineName = %S, \n UserName = %S, \n DomainName =
%S, \n SessionID = %u, \n SessionState = %d",
bstrMachineName, bstrUserName, bstrDomainName, dwSessionID,
SessionState);
}
ErrReturn:
if(hr != S_OK)
printf("ERROR in CallRemoteUserSessionInfoAPI");
return hr;
}
//+--------------------------------------------------------------------
-------
//
// Function: CallRemoteConnectionParmsAPI
//
// Synopsis: Call RemoteConnectionParms API generate a SALEM Ticket.
//
// Arguments: bstrMachineName [in] - Remote MachineName
// bstrUserName [in] - User Name
// bstrDomainName [in] - Domain Name
// lSessionID [in] - SessionId
// bstrUserHelpBlob [in] - User Help Blob
//
//---------------------------------------------------------------------
-------
HRESULT CallRemoteConnectionParmsAPI(
IN BSTR bstrMachineName,
IN BSTR bstrUserName,
IN BSTR bstrDomainName,
IN long lSessionID,
IN BSTR bstrUserHelpBlob)
{
HRESULT hr = S_OK;
COSERVERINFO si; ::ZeroMemory( &si, sizeof( si ) );
MULTI_QI qi; ::ZeroMemory( &qi, sizeof( qi ) );
CComPtr<HSITLB::IPCHService> pIsvc;
BSTR bstrGeneratedSALEMTicket = NULL;
si.pwszName = bstrMachineName;
qi.pIID = &(HSITLB::IID_IPCHService);
hr = ::CoCreateInstanceEx(
HSITLB::CLSID_PCHService, NULL, CLSCTX_REMOTE_SERVER,
&si, 1, &qi );
if(hr != S_OK) goto ErrReturn;
// Get the IPCHService interface pointer to the pIsvc local pointer
pIsvc.Attach((HSITLB::IPCHService*)qi.pItf );
printf("\n\n\t RemoteConnectionParms \n");
printf("\n MachineName = %S \n UserName = %S \n DomainName = %S \n
lSessionID = %d \n UserHelpBlob = %S",
bstrMachineName, bstrUserName, bstrDomainName, lSessionID,
bstrUserHelpBlob);
// Generate the Ticket
hr = pIsvc->RemoteConnectionParms(
bstrUserName,
bstrDomainName,
lSessionID,
bstrUserHelpBlob,
&bstrGeneratedSALEMTicket);
if(hr != S_OK) goto ErrReturn;
printf("\n The Ticket generated by RemoteConnectionParms: \n %S
\n",
bstrGeneratedSALEMTicket);
// Get the modem status
// This call will fail for Remote Machine that is XP. It will work
for Server 2003
// VARIANT_BOOL bModemConnected = FALSE;
// hr = pIsvc->get_RemoteModemConnected(&bModemConnected);
// if(hr != S_OK) goto ErrReturn;
// printf("\n The ModemConnected = %d", bModemConnected);
ErrReturn:
if(hr != S_OK)
printf("ERROR in CallRemoteConnectionParmsAPI");
return hr;
}
//+--------------------------------------------------------------------
-------
//
// Function: main
//
// Synopsis: Entry point for API Tests
//
//---------------------------------------------------------------------
-------
int __cdecl main (int argc, char *argv[])
{
HRESULT hr = S_OK;
// Replace the <...> placeholder with correct data
BSTR bstrMachineName = SysAllocString(L"<machine name>");
BSTR bstrUserHelpBlob = SysAllocString(L"<help blob>");
BSTR bstrUserName = SysAllocString(L"<user name>");
BSTR bstrDomainName = SysAllocString(L"<domain name>");
long lSessionID = 0;
hr = CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
if(hr != S_OK) goto ErrReturn;
// Call RemoteUserSessionInfo API
hr = CallRemoteUserSessionInfoAPI(bstrMachineName);
if(hr != S_OK) goto ErrReturn;
// Call RemoteConnectionParms API
hr = CallRemoteConnectionParmsAPI(
bstrMachineName,
bstrUserName,
bstrDomainName,
lSessionID,
bstrUserHelpBlob);
if(hr != S_OK) goto ErrReturn;
printf("\n Successfully called both APIs RemoteUserSessionInfo and
RemoteConnectionParms. \n\n");
ErrReturn:
CoUninitialize();
return 0;
} // main