Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions | Variables
process.c File Reference
#include "process.h"
#include "extern.h"
#include "extra.h"
#include "dialogs.h"
#include <tlhelp32.h>
#include <winperf.h>

Go to the source code of this file.

Classes

struct  _CAWOWH
struct  _kpf

Defines

#define INDEX_STR_LEN   10
#define PN_PROCESS   TEXT("Process")
#define PN_PROCESS_ID   TEXT("ID Process")
#define PN_THREAD   TEXT("Thread")
#define CW_CLOSE_ALL   0x00000001
#define CW_CLOSE_VISIBLE_ONLY   0x00000002
#define CW_CHECK_VISIBLE_ONLY   0x00000003

Typedefs

typedef PERF_DATA_BLOCK PERF_DATA
typedef PERF_DATA_BLOCK * PPERF_DATA
typedef PERF_OBJECT_TYPE PERF_OBJECT
typedef PERF_OBJECT_TYPE * PPERF_OBJECT
typedef PERF_INSTANCE_DEFINITION PERF_INSTANCE
typedef PERF_INSTANCE_DEFINITION * PPERF_INSTANCE
typedef BOOL(WINAPI * NS_ProcessWalk )(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
typedef HANDLE(WINAPI * NS_CreateSnapshot )(DWORD dwFlags, DWORD th32ProcessID)
typedef struct _CAWOWH CAWOWH
typedef struct _kpf kpf

Functions

BOOL _FindAndKillProcessNT4 (LPSTR aProcessName, BOOL aKillProcess)
BOOL KillProcess (char *aProcessName, HWND aHwndProcess, DWORD aProcessID)
DWORD GetTitleIdx (HWND hWnd, LPTSTR Title[], DWORD LastIndex, LPTSTR Name)
PPERF_OBJECT FindObject (PPERF_DATA pData, DWORD TitleIndex)
PPERF_OBJECT NextObject (PPERF_OBJECT pObject)
PPERF_OBJECT FirstObject (PPERF_DATA pData)
PPERF_INSTANCE NextInstance (PPERF_INSTANCE pInst)
PPERF_INSTANCE FirstInstance (PPERF_OBJECT pObject)
DWORD GetPerfData (HKEY hPerfKey, LPTSTR szObjectIndex, PPERF_DATA *ppData, DWORD *pDataSize)
BOOL CALLBACK EnumWindowsProc (HWND hwnd, LPARAM aParam)
BOOL _FindAndKillProcess (kpf *kpfRoutines, LPSTR aProcessName, BOOL aKillProcess)
BOOL FindAndKillProcess (LPSTR aProcessName, BOOL aKillProcess)
BOOL CloseAllWindowsOfWindowHandle (HWND aHwndWindow, char *aMsgWait)

Variables

TCHAR INDEX_PROCTHRD_OBJ [2 *INDEX_STR_LEN]
DWORD PX_PROCESS
DWORD PX_PROCESS_ID
DWORD PX_THREAD

Class Documentation

struct _CAWOWH

Definition at line 81 of file process.c.

Class Members
DWORD cwState
DWORD processID
struct _kpf

Definition at line 87 of file process.c.

Class Members
NS_CreateSnapshot pCreateToolhelp32Snapshot
NS_ProcessWalk pProcessWalkFirst
NS_ProcessWalk pProcessWalkNext

Define Documentation

#define CW_CHECK_VISIBLE_ONLY   0x00000003

Definition at line 53 of file process.c.

#define CW_CLOSE_ALL   0x00000001

Definition at line 51 of file process.c.

#define CW_CLOSE_VISIBLE_ONLY   0x00000002

Definition at line 52 of file process.c.

Definition at line 45 of file process.c.

#define PN_PROCESS   TEXT("Process")

Definition at line 46 of file process.c.

Definition at line 47 of file process.c.

#define PN_THREAD   TEXT("Thread")

Definition at line 48 of file process.c.


Typedef Documentation

typedef struct _CAWOWH CAWOWH
typedef struct _kpf kpf
typedef HANDLE(WINAPI * NS_CreateSnapshot)(DWORD dwFlags, DWORD th32ProcessID)

Definition at line 59 of file process.c.

typedef BOOL(WINAPI * NS_ProcessWalk)(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)

Definition at line 58 of file process.c.

typedef PERF_DATA_BLOCK PERF_DATA

Definition at line 55 of file process.c.

typedef PERF_INSTANCE_DEFINITION PERF_INSTANCE

Definition at line 57 of file process.c.

typedef PERF_OBJECT_TYPE PERF_OBJECT

Definition at line 56 of file process.c.

typedef PERF_DATA_BLOCK * PPERF_DATA

Definition at line 55 of file process.c.

typedef PERF_INSTANCE_DEFINITION * PPERF_INSTANCE

Definition at line 57 of file process.c.

typedef PERF_OBJECT_TYPE * PPERF_OBJECT

Definition at line 56 of file process.c.


Function Documentation

BOOL _FindAndKillProcess ( kpf kpfRoutines,
LPSTR  aProcessName,
BOOL  aKillProcess 
)

Definition at line 254 of file process.c.

{
  BOOL            rv              = FALSE;
  HANDLE          hCreateSnapshot = NULL;
  PROCESSENTRY32  peProcessEntry;
  
  hCreateSnapshot = kpfRoutines->pCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if(hCreateSnapshot == (HANDLE)-1)
    return(rv);

  peProcessEntry.dwSize = sizeof(PROCESSENTRY32);
  if(kpfRoutines->pProcessWalkFirst(hCreateSnapshot, &peProcessEntry))
  {
    char  szBuf[MAX_BUF];

    do
    {
      ParsePath(peProcessEntry.szExeFile, szBuf, sizeof(szBuf), FALSE, PP_FILENAME_ONLY);
      
      /* do process name string comparison here */
      if(lstrcmpi(szBuf, aProcessName) == 0)
      {
        rv = TRUE;
        if(aKillProcess)
          KillProcess(aProcessName, NULL, peProcessEntry.th32ProcessID);
        else
          break;
      }

    } while(kpfRoutines->pProcessWalkNext(hCreateSnapshot, &peProcessEntry));
  }

  CloseHandle(hCreateSnapshot);
  return(rv);
}

Here is the call graph for this function:

Here is the caller graph for this function:

BOOL _FindAndKillProcessNT4 ( LPSTR  aProcessName,
BOOL  aKillProcess 
)

Definition at line 303 of file process.c.

{
  BOOL          bRv;
  HKEY          hKey;
  DWORD         dwType;
  DWORD         dwSize;
  DWORD         dwTemp;
  DWORD         dwTitleLastIdx;
  DWORD         dwIndex;
  DWORD         dwLen;
  DWORD         pDataSize = 50 * 1024;
  LPSTR         szCounterValueName;
  LPSTR         szTitle;
  LPSTR         *szTitleSz;
  LPSTR         szTitleBuffer;
  PPERF_DATA    pData;
  PPERF_OBJECT  pProcessObject;

  bRv   = FALSE;
  hKey  = NULL;

  if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                  TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\perflib"),
                  0,
                  KEY_READ,
                  &hKey) != ERROR_SUCCESS)
    return(bRv);

  dwSize = sizeof(dwTitleLastIdx);
  if(RegQueryValueEx(hKey, TEXT("Last Counter"), 0, &dwType, (LPBYTE)&dwTitleLastIdx, &dwSize) != ERROR_SUCCESS)
  {
    RegCloseKey(hKey);
    return(bRv);
  }
    

  dwSize = sizeof(dwTemp);
  if(RegQueryValueEx(hKey, TEXT("Version"), 0, &dwType, (LPBYTE)&dwTemp, &dwSize) != ERROR_SUCCESS)
  {
    RegCloseKey(hKey);
    szCounterValueName = TEXT("Counters");
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                    TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009"),
                    0,
                    KEY_READ,
                    &hKey) != ERROR_SUCCESS)
      return(bRv);
  }
  else
  {
    RegCloseKey(hKey);
    szCounterValueName = TEXT("Counter 009");
    hKey = HKEY_PERFORMANCE_DATA;
  }

  // Find out the size of the data.
  //
  dwSize = 0;
  if(RegQueryValueEx(hKey, szCounterValueName, 0, &dwType, 0, &dwSize) != ERROR_SUCCESS)
    return(bRv);


  // Allocate memory
  //
  szTitleBuffer = (LPTSTR)LocalAlloc(LMEM_FIXED, dwSize);
  if(!szTitleBuffer)
  {
    RegCloseKey(hKey);
    return(bRv);
  }

  szTitleSz = (LPTSTR *)LocalAlloc(LPTR, (dwTitleLastIdx + 1) * sizeof(LPTSTR));
  if(!szTitleSz)
  {
    if(szTitleBuffer != NULL)
    {
      LocalFree(szTitleBuffer);
      szTitleBuffer = NULL;
    }

    RegCloseKey(hKey);
    return(bRv);
  }

  // Query the data
  //
  if(RegQueryValueEx(hKey, szCounterValueName, 0, &dwType, (BYTE *)szTitleBuffer, &dwSize) != ERROR_SUCCESS)
  {
    RegCloseKey(hKey);
    if(szTitleSz)
      LocalFree(szTitleSz);
    if(szTitleBuffer)
      LocalFree(szTitleBuffer);

    return(bRv);
  }

  // Setup the TitleSz array of pointers to point to beginning of each title string.
  // TitleBuffer is type REG_MULTI_SZ.
  //
  szTitle = szTitleBuffer;
  while(dwLen = lstrlen(szTitle))
  {
    dwIndex = atoi(szTitle);
    szTitle = szTitle + dwLen + 1;

    if(dwIndex <= dwTitleLastIdx)
      szTitleSz[dwIndex] = szTitle;

    szTitle = szTitle + lstrlen(szTitle) + 1;
  }

  PX_PROCESS    = GetTitleIdx (NULL, szTitleSz, dwTitleLastIdx, PN_PROCESS);
  PX_PROCESS_ID = GetTitleIdx (NULL, szTitleSz, dwTitleLastIdx, PN_PROCESS_ID);
  PX_THREAD     = GetTitleIdx (NULL, szTitleSz, dwTitleLastIdx, PN_THREAD);
  wsprintf(INDEX_PROCTHRD_OBJ, TEXT("%ld %ld"), PX_PROCESS, PX_THREAD);
  pData = NULL;
  if(GetPerfData(HKEY_PERFORMANCE_DATA, INDEX_PROCTHRD_OBJ, &pData, &pDataSize) == ERROR_SUCCESS)
  {
    PPERF_INSTANCE pInst;
    DWORD          i = 0;

    pProcessObject = FindObject(pData, PX_PROCESS);
    if(pProcessObject)
    {
      LPSTR szPtrStr;
      int   iLen;
      char  szProcessNamePruned[MAX_BUF];
      char  szNewProcessName[MAX_BUF];

      if(sizeof(szProcessNamePruned) < (lstrlen(aProcessName) + 1))
      {
        if(hKey)
          RegCloseKey(hKey);
        if(szTitleSz)
          LocalFree(szTitleSz);
        if(szTitleBuffer)
          LocalFree(szTitleBuffer);

        return(bRv);
      }

      /* look for .exe and remove from end of string because the Process names
       * under NT are returned without the extension */
      lstrcpy(szProcessNamePruned, aProcessName);
      iLen = lstrlen(szProcessNamePruned);
      szPtrStr = &szProcessNamePruned[iLen - 4];
      if((lstrcmpi(szPtrStr, ".exe") == 0) || (lstrcmpi(szPtrStr, ".dll") == 0))
        *szPtrStr = '\0';

      pInst = FirstInstance(pProcessObject);
      for(i = 0; i < (DWORD)(pProcessObject->NumInstances); i++)
      {
        *szNewProcessName = '\0';
        if(WideCharToMultiByte(CP_ACP,
                               0,
                               (LPCWSTR)((PCHAR)pInst + pInst->NameOffset),
                               -1,
                               szNewProcessName,
                               MAX_BUF,
                               NULL,
                               NULL) != 0)
        {
          if(lstrcmpi(szNewProcessName, szProcessNamePruned) == 0)
          {
            if(aKillProcess)
              KillProcess(aProcessName, NULL, PX_PROCESS_ID);
            bRv = TRUE;
            break;
          }
        }

        pInst = NextInstance(pInst);
      }
    }
  }

  if(hKey)
    RegCloseKey(hKey);
  if(szTitleSz)
    LocalFree(szTitleSz);
  if(szTitleBuffer)
    LocalFree(szTitleBuffer);

  return(bRv);
}

Here is the call graph for this function:

Here is the caller graph for this function:

BOOL CloseAllWindowsOfWindowHandle ( HWND  aHwndWindow,
char *  aMsgWait 
)

Definition at line 215 of file process.c.

{
  CAWOWH closeWindowInfo;
  BOOL  rv = TRUE;

  assert(aHwndWindow);
  assert(aMsgWait);
  if(*aMsgWait != '\0')
    ShowMessage(aMsgWait, TRUE);

  GetWindowThreadProcessId(aHwndWindow, &closeWindowInfo.processID);

  /* only close the visible windows */
  closeWindowInfo.cwState = CW_CLOSE_VISIBLE_ONLY;
  EnumWindows(EnumWindowsProc, (LPARAM)&closeWindowInfo);

  /* only check to make sure that the visible windows were closed */
  closeWindowInfo.cwState = CW_CHECK_VISIBLE_ONLY;
  rv = EnumWindows(EnumWindowsProc, (LPARAM)&closeWindowInfo);
  if(rv)
  {
    /* All visible windows have been closed.  Close all remaining windows now */
    closeWindowInfo.cwState = CW_CLOSE_ALL;
    EnumWindows(EnumWindowsProc, (LPARAM)&closeWindowInfo);
  }
  if(*aMsgWait != '\0')
    ShowMessage(aMsgWait, FALSE);

  return(rv);
}

Here is the caller graph for this function:

BOOL CALLBACK EnumWindowsProc ( HWND  hwnd,
LPARAM  aParam 
)
BOOL FindAndKillProcess ( LPSTR  aProcessName,
BOOL  aKillProcess 
)

Definition at line 106 of file process.c.

{
  HANDLE hKernel       = NULL;
  BOOL   bDoWin95Check = TRUE;
  kpf    kpfRoutines;

  if((hKernel = GetModuleHandle("kernel32.dll")) == NULL)
    return(FALSE);

  kpfRoutines.pCreateToolhelp32Snapshot = (NS_CreateSnapshot)GetProcAddress(hKernel, "CreateToolhelp32Snapshot");
  kpfRoutines.pProcessWalkFirst         = (NS_ProcessWalk)GetProcAddress(hKernel,    "Process32First");
  kpfRoutines.pProcessWalkNext          = (NS_ProcessWalk)GetProcAddress(hKernel,    "Process32Next");

  if(kpfRoutines.pCreateToolhelp32Snapshot && kpfRoutines.pProcessWalkFirst && kpfRoutines.pProcessWalkNext)
    return(_FindAndKillProcess(&kpfRoutines, aProcessName, aKillProcess));
  else
    return(_FindAndKillProcessNT4(aProcessName, aKillProcess));
}

Here is the caller graph for this function:

PPERF_OBJECT FindObject ( PPERF_DATA  pData,
DWORD  TitleIndex 
)

Definition at line 645 of file process.c.

{
PPERF_OBJECT pObject;
DWORD        i = 0;

    if (pObject = FirstObject (pData))
        while (i < pData->NumObjectTypes)
            {
            if (pObject->ObjectNameTitleIndex == TitleIndex)
                return pObject;

            pObject = NextObject (pObject);
            i++;
            }

    return NULL;
}

Here is the call graph for this function:

Definition at line 569 of file process.c.

{
    if (pObject)
        return (PPERF_INSTANCE)((PCHAR) pObject + pObject->DefinitionLength);
    else
        return NULL;
}

Here is the caller graph for this function:

Definition at line 608 of file process.c.

{
    if (pData)
        return ((PPERF_OBJECT) ((PBYTE) pData + pData->HeaderLength));
    else
        return NULL;
}

Here is the caller graph for this function:

DWORD GetPerfData ( HKEY  hPerfKey,
LPTSTR  szObjectIndex,
PPERF_DATA ppData,
DWORD pDataSize 
)

Definition at line 518 of file process.c.

{
DWORD   DataSize;
DWORD   dwR;
DWORD   Type;


    if (!*ppData)
        *ppData = (PPERF_DATA) LocalAlloc (LMEM_FIXED, *pDataSize);


    do  {
        DataSize = *pDataSize;
        dwR = RegQueryValueEx (hPerfKey,
                               szObjectIndex,
                               NULL,
                               &Type,
                               (BYTE *)*ppData,
                               &DataSize);

        if (dwR == ERROR_MORE_DATA)
            {
            LocalFree (*ppData);
            *pDataSize += 1024;
            *ppData = (PPERF_DATA) LocalAlloc (LMEM_FIXED, *pDataSize);
            }

        if (!*ppData)
            {
            LocalFree (*ppData);
            return ERROR_NOT_ENOUGH_MEMORY;
            }

        } while (dwR == ERROR_MORE_DATA);

    return dwR;
}

Here is the caller graph for this function:

DWORD GetTitleIdx ( HWND  hWnd,
LPTSTR  Title[],
DWORD  LastIndex,
LPTSTR  Name 
)

Definition at line 669 of file process.c.

{
  DWORD Index;

  for(Index = 0; Index <= LastIndex; Index++)
    if(Title[Index])
      if(!lstrcmpi (Title[Index], Name))
        return(Index);

  MessageBox(hWnd, Name, TEXT("Setup cannot find process title index"), MB_OK);
  return 0;
}

Here is the caller graph for this function:

BOOL KillProcess ( char *  aProcessName,
HWND  aHwndProcess,
DWORD  aProcessID 
)

Definition at line 132 of file process.c.

{
  BOOL rv = FALSE;
  HWND hwndProcess;

  hwndProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, aProcessID);
  if(!hwndProcess)
    return(rv);

  if(!TerminateProcess(hwndProcess, 1))
  {
    char errorMsg[MAX_BUF];

    if(GetPrivateProfileString("Messages", "ERROR_TERMINATING_PROCESS", "", errorMsg, sizeof(errorMsg), szFileIniInstall))
    {
      char buf[MAX_BUF];

      wsprintf(buf, errorMsg, aProcessName);
      PrintError(buf, ERROR_CODE_SHOW);
    }
  }
  else
    rv = TRUE;

  CloseHandle(hwndProcess);
  return(rv);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 588 of file process.c.

{
PERF_COUNTER_BLOCK *pCounterBlock;

    if (pInst)
        {
        pCounterBlock = (PERF_COUNTER_BLOCK *)((PCHAR) pInst + pInst->ByteLength);
        return (PPERF_INSTANCE)((PCHAR) pCounterBlock + pCounterBlock->ByteLength);
        }
    else
        return NULL;
}

Here is the caller graph for this function:

Definition at line 630 of file process.c.

{
    if (pObject)
        return ((PPERF_OBJECT) ((PBYTE) pObject + pObject->TotalByteLength));
    else
        return NULL;
}

Here is the caller graph for this function:


Variable Documentation

Definition at line 61 of file process.c.

Definition at line 62 of file process.c.

Definition at line 63 of file process.c.

Definition at line 64 of file process.c.