Click here to Skip to main content
15,881,803 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.80/5 (35 votes)
1 Apr 2012CPOL5 min read 156.5K   8.9K   103  
Control certain aspects of machines sitting remotely, without having to install and trigger an application on the remote machine.
#include "GlobalHelperFunc.h"

extern CRITICAL_SECTION g_CriticalSection;
void ErrorHandling::PopError(DWORD dwError)
{
    CString strErrorDescription = ErrorHandling::FormatError(dwError);
    
   ::AfxMessageBox(strErrorDescription);

}


CString ErrorHandling::FormatError(DWORD dwError)
{
    HLOCAL hlocal = NULL;   // Buffer that gets the error message string
    CString strErrorDescription = _T("");

    // Get the error code's textual description 
    BOOL bOK = ::FormatMessage(
                    FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 
                    NULL, 
                    dwError, 
                    MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), 
                    reinterpret_cast<PTSTR>(&hlocal),
                    0,
                    NULL
                    );

    if (hlocal != NULL) 
    {
        strErrorDescription = static_cast<PCSTR>(hlocal);
        ::LocalFree(hlocal);
    }
    else
    {
        strErrorDescription = _T("Failed to retrive error description !");
    }

    return strErrorDescription;
}


void ErrorHandling::PopLastError()
{
    DWORD dwLastError = ::GetLastError();
    ErrorHandling::PopError(dwLastError);
}


CString ErrorHandling::FormatLastError()
{
    DWORD dwLastError = ::GetLastError();

    CString strErrorDescription = ErrorHandling::FormatError(dwLastError);

    return strErrorDescription;
}


// Converts a string table ID to an application speific error message
CString ErrorHandling::ConvertStringTableIDToErrorMsg(CString strMachineIP, UINT iStringTableID)
{
	::EnterCriticalSection(&g_CriticalSection);
    TCHAR szMessage[_MAX_PATH] = _T("");
    CString strFormattedErrorMsg;

    // Load the partial string from the string table
    ::LoadString(AfxGetApp()->m_hInstance, iStringTableID, szMessage, _MAX_PATH);

    // Append and create a complete message combining the IP address and the actual 
    // message from the string table
    strFormattedErrorMsg.Format("IP %s : %s", strMachineIP.GetBuffer(0), szMessage);
	
	::LeaveCriticalSection(&g_CriticalSection);
    return strFormattedErrorMsg;
}


void ProcessHandling::LaunchApp(CString strAppPath, CString strCommandLine)
{
    TCHAR* szAppPath     = strAppPath.GetBuffer(0);
    TCHAR* szCommandline = strCommandLine.GetBuffer(0);

    STARTUPINFO si = {sizeof(si)};
    PROCESS_INFORMATION pi;

    BOOL bSuccess = ::CreateProcess(
                          szAppPath,
                          szCommandline,
                          NULL,
                          NULL,
                          FALSE,
                          0,
                          NULL,
                          NULL,
                          &si,
                          &pi
                          );
                        
    if (bSuccess)
    {
        // Added by Prateek Kaul on Sep. 28, 2002

        // --*******PREVENT MEMORY LEAKS---*************
        // This function was written for this purpose, as 
        // developers forget to call the next 2 calls to
        // close handle

        // Close the thread handle as soon as it is no longer needed!
        ::CloseHandle(pi.hThread);

        // Close the process handle as soon as it is no longer needed.
        ::CloseHandle(pi.hProcess);
    }

    else
    {
       ErrorHandling::PopLastError();
    }
}

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