Click here to Skip to main content
15,886,806 members
Articles / Desktop Programming / MFC

Remote processes and machine control of Windows NT based systems (2000/XP)

Rate me:
Please Sign up or sign in to vote.
4.79/5 (19 votes)
1 Apr 2012CPOL5 min read 117.9K   4.1K   71  
Trigger/monitor/kill processes and shutdown/reboot machines remotely.
#ifndef REMOTEADMINISTRATOR_H
#define REMOTEADMINISTRATOR_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxtempl.h>
#include "MachineInfo.h"

typedef CTypedPtrList<CPtrList, CString*>      CConnectionPendingList;
typedef CTypedPtrList<CPtrList, CMachineInfo*> CMachineInfoList;

#define  REMOTE_ADMIN_SERVICE              _T("RemoteAdminService")
#define  REMOTE_ADMIN_SERVICE_EXE          _T("RemoteAdminService.exe")
#define  REMOTE_ADMIN_SERVICE_PIPE         _T("RemoteAdminServivePipe")
#define  SERVICENAME                       _T("RemoteAdminService")
#define  LONGSERVICENAME                   _T("RemoteAdminService")
#define  REMOTE_ADMIN_PIPE                 _T("RempteAdminPipe")
#define  REMOTE_ADMIN_PROCESS_INFO_PIPE    _T("RemoteAdminProcessInfoPipe")
#define  REMOTE_ADMIN_PROCESS_EXECUTE_PIPE _T("RemoteAdminProcessExecutePipe")
#define  REMOTE_ADMIN_PROCESS_KILL_PIPE    _T("RemoteAdminProcessKillPipe")
#define  REMOTE_ADMIN_SYS_SHUTDOWN_PIPE    _T("RemoteAdminSysShutDownPipe")
 

class CRemoteAdministrator  
{
public:
	CRemoteAdministrator();
	virtual ~CRemoteAdministrator();

    BOOL EstablishAllConnections(CString strRemoteMachineIP, CString strPwd, BOOL bEstablish);
    void AddMachine(CMachineInfo& miMachineInfo);
    void DeleteMachine(CMachineInfo& miMachineInfo);
    BOOL CheckIfMachinePresent(CString strIP);
    void RefreshProcessList(CString strIP, CProcessInfoList& pilProcessList);
    CMachineInfo* GetMachineInfo(CString strIP);
    BOOL CopyServiceExeToRemoteMachine(CString strRenoteMachineIP);
    BOOL ConnectToRemoteService(CString strRemoteMachineIP, DWORD dwRetry, DWORD dwRetryTimeOut);
    BOOL InstallAndStartRemoteService(CString strRemoteMachineIP);
    CProcessInfoList* GetProcessInfoList(CString strRemoteMachineIP);
    void DeleteAndDisconnectAllMachines();
    void DeleteAndDisconnectMachine(CString strRemoteAdminMachine);
    void GetConnectedMachinesIP(CString** pstrConnctedMachinesIP /*out*/, int* piNumberOfConnectedMachines/*out*/);
    int GetTotalMachinesMonitored();
    BOOL AddToConnectionPendingList(CString strIP);
    BOOL RemoveFromConnecionPendingList(CString strIP);
    BOOL IsConnectionPending(CString strIP);

    HANDLE GetRemoteAdminPipe(CString strRemoteMachineIP);
    HANDLE GetRemoteAdminProcessInfoPipe(CString strRemoteMachineIP);
    HANDLE GetRemoteAdminProcessExecutePipe(CString strRemoteMachineIP);
    HANDLE GetRemoteAdminProcessKillPipe(CString strRemoteMachineIP);
    HANDLE GetRemoteAdminSysShutDownPipe(CString strRemoteMachineIP);
	CString GetPassword(CString strIP);
	UINT GetNumberOfMachineConnectionsStillInProgress();
	CString GetComputerNameFromIP(CString& strIP);
	CString GetComputerIPFromName(CString& strComputerName);
    
protected:
    CMachineInfoList m_milConnectedMachines;
    CConnectionPendingList m_cplPendingConnectionList;

    HANDLE m_hCommandPipe; 
    BOOL EstablishAdminConnection(CString strRemoteMachineIP, CString strPwd, BOOL bEstablish);
    BOOL EstablishIPCConnection(CString strRemoteMachineIP, CString strPwd, BOOL bEstablish);
    BOOL EstablishConnection(CString strResource, CString strLogon, CString strRemoteMachineIP, CString strPwd, BOOL bEstablish);
};


inline HANDLE CRemoteAdministrator::GetRemoteAdminPipe(CString strRemoteMachineIP)
{
    CMachineInfo* pMachineInfo = GetMachineInfo(strRemoteMachineIP);

    if (pMachineInfo != NULL)
        return pMachineInfo->GetRemoteAdminPipe();
    else
        return NULL;
}

inline HANDLE CRemoteAdministrator::GetRemoteAdminProcessInfoPipe(CString strRemoteMachineIP)
{
    CMachineInfo* pMachineInfo = GetMachineInfo(strRemoteMachineIP);

    if (pMachineInfo != NULL)
        return pMachineInfo->GetRemoteAdminProcessInfoPipe();
    else
        return NULL;
}

inline HANDLE CRemoteAdministrator::GetRemoteAdminProcessExecutePipe(CString strRemoteMachineIP)
{
    CMachineInfo* pMachineInfo = GetMachineInfo(strRemoteMachineIP);
    
    if (pMachineInfo != NULL)
       return pMachineInfo->GetRemoteAdminProcessExecutePipe();
    else
      return NULL;
}

inline HANDLE CRemoteAdministrator::GetRemoteAdminProcessKillPipe(CString strRemoteMachineIP)
{
    CMachineInfo* pMachineInfo = GetMachineInfo(strRemoteMachineIP);

    if (pMachineInfo != NULL)
        return pMachineInfo->GetRemoteAdminProcessKillPipe();
    else
        return NULL;
}

inline HANDLE CRemoteAdministrator::GetRemoteAdminSysShutDownPipe(CString strRemoteMachineIP)
{
    CMachineInfo* pMachineInfo = GetMachineInfo(strRemoteMachineIP);

    if (pMachineInfo != NULL)
        return pMachineInfo->GetRemoteAdminSysShutDownPipe();
    else
        return NULL;
}

#endif // REMOTEADMINISTRATOR_H

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions