Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Remote Processes and Machine control of Windows NT based systems (2000/XP)

, 1 Apr 2012
Control certain aspects of machines sitting remotely, without having to install and trigger an application on the remote machine.
#include "stdafx.h"
#include "RemoteAdmin.h"
#include "MachineInfo.h"
#include "command.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_SERIAL(CMachineInfo, CObject, 1)

extern CRITICAL_SECTION g_CriticalSection;

CMachineInfo::CMachineInfo()
{
    m_strPwd = _T("");
    m_strIP  = _T("");

    m_hRemoteAdminPipe               = NULL;
    m_hRemoteAdminProcessExecutePipe = NULL;
    m_hRemoteAdminProcessInfoPipe    = NULL;
    m_hRemoteAdminProcessKillPipe    = NULL;
    m_hRemoteAdminSysShutDownPipe    = NULL;
}

CMachineInfo::~CMachineInfo()
{
	// Free the m_pilProcessList
	POSITION pos = m_pilProcessList.GetHeadPosition();

	while(pos != NULL)
	{
		//PROCESSENTRY32* pProcessEntry = m_pilProcessList.GetNext(pos);
        SProcessInfo* pProcessEntry = m_pilProcessList.GetNext(pos);

		if (pProcessEntry)
		{
			delete pProcessEntry;
		}
	}

	m_pilProcessList.RemoveAll();
}

CMachineInfo& CMachineInfo::operator = (CMachineInfo& miMachineInfo)
{
    m_strPwd   = miMachineInfo.GetPassword();
    m_strIP    = miMachineInfo.GetIP();
    m_strLogon = miMachineInfo.GetLogon();

    return *this;
}

BOOL CMachineInfo::operator == (CMachineInfo& miMachineInfo)
{
    if (m_strIP == miMachineInfo.GetIP() && 
        m_strPwd == miMachineInfo.GetPassword() &&
        m_strLogon == miMachineInfo.GetLogon())
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

void CMachineInfo::RefreshProcessList(CProcessInfoList& pilList)
{
	::EnterCriticalSection(&g_CriticalSection);

    int iProcessCount = m_pilProcessList.GetCount();
    POSITION pos = NULL;
    //PROCESSENTRY32* pProcessEntry = NULL;
    SProcessInfo* pProcessEntry = NULL;

    // Free all allocated memeory in the existing inner list
	/*for (int i = 0; i < iProcessCount; ++i)
    {
        pos = m_pilProcessList.FindIndex(i);
        
		if (pos != (POSITION)0xcdcdcdcd && pos != NULL)
		{
			pProcessEntry = m_pilProcessList.GetAt(pos);

			if (pProcessEntry != NULL)
			{
				delete pProcessEntry;
			}
		}
    }*/

	pos = m_pilProcessList.GetHeadPosition();
	while (pos != NULL)
	{
		pProcessEntry = m_pilProcessList.GetNext(pos);
		if (pProcessEntry != NULL)
		{
			delete pProcessEntry;
		}
		
	}
	
	// Remove all PROCESSENTRY32 from the list
    m_pilProcessList.RemoveAll();

    
    // Update the prcoessinfolist with new PROCESSENTRY32's, i.e make copies
    // of the new PROCESSENTRY32's and store them in the m_pilProcessList
    iProcessCount = pilList.GetCount();

    // Keep making new copies of new PROCESSENTRY32's and add to the process list
   /* for (int i = 0; i < iProcessCount; ++i)
    {
        pProcessEntry = new PROCESSENTRY32;

        if (pProcessEntry != NULL)
        {
            pos = pilList.FindIndex(i);

            ::memcpy(pProcessEntry, pilList.GetAt(pos), sizeof(PROCESSENTRY32));
		
			m_pilProcessList.AddTail(pProcessEntry);
		}
        else
        {
            ::AfxMessageBox(IDS_NO_MEMORY_FOR_NEW_PROCESSINFO);
        }
    }*/

	pos = pilList.GetHeadPosition();
	while (pos != NULL)
	{
		//pProcessEntry = new PROCESSENTRY32;
        pProcessEntry = new SProcessInfo;
		if (pProcessEntry != NULL)
		{
			::memcpy(pProcessEntry, pilList.GetAt(pos), sizeof(SProcessInfo));
			m_pilProcessList.AddTail(pProcessEntry);
		}
		else
        {
            ::AfxMessageBox(IDS_NO_MEMORY_FOR_NEW_PROCESSINFO);
        }

		// Move ahead to next iteration
		pilList.GetNext(pos);
	}

	::LeaveCriticalSection(&g_CriticalSection);
}

CProcessInfoList* CMachineInfo::GetProcessInfoList()
{
	::EnterCriticalSection(&g_CriticalSection);
    // Make a copy of the existing list and return the copy
    CProcessInfoList* pProcessInfoList = new CProcessInfoList;

    POSITION pos = m_pilProcessList.GetHeadPosition();
    while (pos != (POSITION)0xcdcdcdcd && pos != NULL)
    {
        //PROCESSENTRY32* pPe = pProcessInfoList->GetNext(pos);
		//PROCESSENTRY32* pPe = m_pilProcessList.GetNext(pos);
        //PROCESSENTRY32* p = new PROCESSENTRY32;
        SProcessInfo* pPe = m_pilProcessList.GetNext(pos);
        SProcessInfo* p = new SProcessInfo;
        
		if (p != NULL)
		{
			// make a copy and add to the list which the copy of the original list
			if (pPe != NULL)
			{
				::memcpy(p, pPe, sizeof(SProcessInfo));

				pProcessInfoList->AddTail(p);
			}
		}
    }

	::LeaveCriticalSection(&g_CriticalSection);
    return pProcessInfoList;
}

void CMachineInfo::ClosePipeHandles()
{
    //::CloseHandle(m_hRemoteAdminPipe);
    ::CloseHandle(m_hRemoteAdminProcessInfoPipe);
    ::CloseHandle(m_hRemoteAdminProcessExecutePipe);
    ::CloseHandle(m_hRemoteAdminProcessKillPipe);
    ::CloseHandle(m_hRemoteAdminSysShutDownPipe);
}

void CMachineInfo::SendEndThreadMessage()
{
    DWORD dwRead    = 0;
    DWORD dwWritten = 0;
    SCommand cmd    = {0};
    BOOL bOk        = FALSE;

    cmd.m_bThreadExit = TRUE;

    // Send the server process to end the threads
   // bOk = ::WriteFile(m_hRemoteAdminPipe,               &cmd, sizeof(SCommand), &dwWritten, NULL);
    bOk = ::WriteFile(m_hRemoteAdminProcessInfoPipe,    &cmd, sizeof(SCommand), &dwWritten, NULL);
    bOk = ::WriteFile(m_hRemoteAdminProcessExecutePipe, &cmd, sizeof(SCommand), &dwWritten, NULL);
    bOk = ::WriteFile(m_hRemoteAdminProcessKillPipe,    &cmd, sizeof(SCommand), &dwWritten, NULL);
    bOk = ::WriteFile(m_hRemoteAdminSysShutDownPipe,    &cmd, sizeof(SCommand), &dwWritten, NULL);
}


void CMachineInfo::Serialize(CArchive& ar)
{
    CObject::Serialize(ar);

    // Only three variables are enough to be
    // serialized. Rest variables are not important.
    if (ar.IsLoading())
    {
        ar >> m_strIP;
        ar >> m_strPwd;
        ar >> m_strLogon;
    }
    else
    {
        ar << m_strIP;
        ar << m_strPwd;
        ar << m_strLogon;
    }
}

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)

Share

About the Author

Prateek Kaul
Web Developer
India India
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 1 Apr 2012
Article Copyright 2003 by Prateek Kaul
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid