Back

// PSLCurrentProcess.cpp : Implementation of CPSLCurrentProcess

#include "stdafx.h"
#include "PSLCurrentProcess.h"
#include "ProSysModule.h"
#include <ATLComTime.h>
#include <Psapi.h>

#define ProcessHandleCount 0x14

CPSLCurrentProcess::CPSLCurrentProcess()
{
   m_dwProcessID = ::GetCurrentProcessId();

   FILETIME create, exit, kernel, user;
   if(::GetProcessTimes(::GetCurrentProcess(), &create, &exit, &kernel, &user))
      m_Created = COleDateTime(create);
   else
      m_Created = NULL;

   m_sFilePath = _T("");
   m_sFileDir = _T("");
   m_sFileName = _T("");

   tstring path;
   path.resize(MAX_FILE_PATH);
   if(::GetModuleFileName(NULL, (LPTSTR)path.c_str(), MAX_FILE_PATH) > 0)
      CPSLUtilities::GetLongFilePathDetails(path.c_str(), m_sFilePath, m_sFileDir, m_sFileName);
}

HRESULT CPSLCurrentProcess::FinalConstruct()
{
   return S_OK;
}

void CPSLCurrentProcess::FinalRelease()
{
}

////////////////////////////////////////////////////////////////////////
// Interface Implementation;
////////////////////////////////////////////////////////////////////////

STDMETHODIMP CPSLCurrentProcess::get_ProcessID(long * pValue)
{
   PSL_BEGIN

   *pValue = m_dwProcessID;

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_ConsoleWnd(VARIANT * pValue)
{
   PSL_BEGIN

   CPSLUtilities::SetVariantBig(pValue, (ABIG)::GetConsoleWindow());

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_FileName(BSTR * pValue)
{
   PSL_BEGIN

   *pValue = m_sFileName.copy();

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_FilePath(BSTR * pValue)
{
   PSL_BEGIN

   *pValue = m_sFilePath.copy();

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_FileDir(BSTR * pValue)
{
   PSL_BEGIN

   *pValue = m_sFileDir.copy();

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_CurrentDir(BSTR * pValue)
{
   PSL_BEGIN

   tstring dir;
   dir.resize(MAX_FILE_PATH);
   if(::GetCurrentDirectory(MAX_FILE_PATH, (LPTSTR)dir.c_str()))
      ::GetLongPathName(dir.c_str(), (LPTSTR)dir.c_str(), MAX_FILE_PATH);
   *pValue = ::SysAllocString(dir.c_str());

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::put_CurrentDir(BSTR newValue)
{
   PSL_BEGIN

   ::SetCurrentDirectory(_bstr_t(newValue));

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Created(DATE * pValue)
{
   PSL_BEGIN

   *pValue = m_Created;

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Is64Bit(VARIANT_BOOL * pValue)
{
   PSL_BEGIN

#ifdef _WIN64
   *pValue = VARIANT_TRUE;
#else
   *pValue = VARIANT_FALSE;
#endif

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_IsDebugged(VARIANT_BOOL * pValue)
{
   PSL_BEGIN

   *pValue = FALSE;
   if(_Module.m_CheckRemoteDebuggerPresent)
   {
      BOOL bDebugged = FALSE;
      if(_Module.m_CheckRemoteDebuggerPresent(::GetCurrentProcess(), &bDebugged))
         *pValue = bDebugged?VARIANT_TRUE:VARIANT_FALSE;
   }
   else
      if(_Module.m_NtQueryInformationProcess)
      {
         DWORD dwDebugPort = 0;
         if(!_Module.m_NtQueryInformationProcess(::GetCurrentProcess(), 7, &dwDebugPort, sizeof(DWORD), NULL))
            *pValue = (dwDebugPort > 0)?VARIANT_TRUE:VARIANT_FALSE;
      }
      else
         *pValue = ::IsDebuggerPresent()?VARIANT_TRUE:VARIANT_FALSE;

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_HandleCount(long * pValue)
{
   PSL_BEGIN

   *pValue = 0;
   if(_Module.m_GetProcessHandleCount)
   {
      DWORD dwHandles = 0;
      if(_Module.m_GetProcessHandleCount(::GetCurrentProcess(), &dwHandles))
         *pValue = dwHandles;
   }
   else
      if(_Module.m_NtQueryInformationProcess)
      {
         DWORD dwCounter = 0;
         if(!_Module.m_NtQueryInformationProcess(::GetCurrentProcess(), ProcessHandleCount, &dwCounter, sizeof(DWORD), NULL))
            *pValue = dwCounter;
      }
      else
         *pValue = 0;

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_GDICount(long * pValue)
{
   PSL_BEGIN

   *pValue = ::GetGuiResources(::GetCurrentProcess(), GR_GDIOBJECTS);

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_USERCount(long * pValue)
{
   PSL_BEGIN

   *pValue = ::GetGuiResources(::GetCurrentProcess(), GR_USEROBJECTS);

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Priority(PSLProcessPriority * pValue)
{
   PSL_BEGIN

   *pValue = (PSLProcessPriority)::GetPriorityClass(::GetCurrentProcess());

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::put_Priority(PSLProcessPriority newValue)
{
   PSL_BEGIN

   if(!::SetPriorityClass(::GetCurrentProcess(), newValue))
   {
      if(::GetLastError() == ERROR_ACCESS_DENIED)
         SetException(EX_NOACCESS);
   }

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_ShutdownLevel(long * pValue)
{
   PSL_BEGIN

   DWORD dwLevel, dwFlags;
   if(::GetProcessShutdownParameters(&dwLevel, &dwFlags))
      *pValue = dwLevel;
   else
      *pValue = 0;

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::put_ShutdownLevel(long newValue)
{
   PSL_BEGIN

   // Only values between 0 and 0x4FF
   // are known to be valid at present;
   if(newValue < 0 || newValue > 0x4FF)
      return MakeException(EX_INVALIDPARAMETER);

   DWORD dwLevel, dwFlags;
   if(::GetProcessShutdownParameters(&dwLevel, &dwFlags))
   {
      dwLevel = newValue;
      ::SetProcessShutdownParameters(dwLevel, dwFlags);
   }

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_ShutdownFlags(long * pValue)
{
   PSL_BEGIN

   DWORD dwLevel, dwFlags;
   if(::GetProcessShutdownParameters(&dwLevel, &dwFlags))
      *pValue = dwFlags;
   else
      *pValue = 0;

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::put_ShutdownFlags(long newValue)
{
   PSL_BEGIN

   // Only 0 and SHUTDOWN_NORETRY are known 
   // to be supported at present;
   if(newValue != 0 && newValue != SHUTDOWN_NORETRY)
      return MakeException(EX_INVALIDPARAMETER);

   DWORD dwLevel, dwFlags;
   if(::GetProcessShutdownParameters(&dwLevel, &dwFlags))
   {
      dwFlags = newValue;
      ::SetProcessShutdownParameters(dwLevel, dwFlags);
   }

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_AffinityMask(VARIANT * pValue)
{
   PSL_BEGIN

   CPSLUtilities::SetVariantBig(pValue, 0);

   DWORD_PTR ProcessAffinityMask = 0;
   DWORD_PTR SystemAffinityMask = 0;
   if(::GetProcessAffinityMask(::GetCurrentProcess(), &ProcessAffinityMask, &SystemAffinityMask))
      CPSLUtilities::SetVariantBig(pValue, ProcessAffinityMask);

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::put_AffinityMask(VARIANT newValue)
{
   PSL_BEGIN

   ABIG Mask = CPSLUtilities::GetVariantBig(&newValue);
   if(Mask)
   {
      DWORD_PTR ProcessAffinityMask = Mask;
      ::SetProcessAffinityMask(::GetCurrentProcess(), ProcessAffinityMask);
   }
   else
      SetException(EX_INVALIDPARAMETER);

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Version(IPSLModuleVersion ** ppValue)
{
   PSL_BEGIN

   *ppValue = m_Version;

   CPSLModuleVersion * pVer = m_Version;
   pVer->InitInternal(m_sFilePath);
   pVer->Release();

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Memory(IPSLProcessMemory ** ppValue)
{
   PSL_BEGIN

   *ppValue = m_Memory;

   CPSLProcessMemory * pMemory = m_Memory;
   pMemory->Initialize(::GetCurrentProcessId());
   pMemory->Release();

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_IO(IPSLProcessIO ** ppValue)
{
   PSL_BEGIN

   *ppValue = m_ProcessIO;

   CPSLProcessIO * pProcessIO = m_ProcessIO;
   pProcessIO->Initialize(::GetCurrentProcessId());
   pProcessIO->Release();

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_EnvVars(IPSLEnvironmentVars ** ppValue)
{
   PSL_BEGIN

   *ppValue = m_EnvVars;

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Modules(IPSLModules ** ppValue)
{
   PSL_BEGIN

   *ppValue = m_Modules;

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Commands(IPSLCmdParams ** ppValue)
{
   PSL_BEGIN

   *ppValue = m_Commands;

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Threads(IPSLThreads ** ppValue)
{
   PSL_BEGIN

   *ppValue = m_Threads;

   PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Windows(IPSLWindows ** ppValue)
{
   PSL_BEGIN

   *ppValue = m_Windows;

   PSL_END
}

Top