Click here to Skip to main content
12,292,969 members (66,596 online)
Click here to Skip to main content

Tagged as

Stats

131.3K views
8.5K downloads
104 bookmarked
Posted

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

, 1 Apr 2012 CPOL
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)

Share

About the Author

Prateek Kaul
Web Developer
India India
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 1 Apr 2012
Article Copyright 2003 by Prateek Kaul
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid