code improvements and various bug fixes

svn path=/trunk/; revision=28541
This commit is contained in:
Ged Murphy 2007-08-25 13:41:44 +00:00
parent 850675cbdd
commit 1adba33d1e
4 changed files with 174 additions and 217 deletions

View file

@ -15,7 +15,7 @@ BOOL bSortAscending = TRUE;
/* Toolbar buttons */ /* Toolbar buttons */
TBBUTTON Buttons [NUM_BUTTONS] = static const TBBUTTON Buttons [] =
{ /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */ { /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
{TBICON_PROP, ID_PROP, TBSTATE_INDETERMINATE, BTNS_BUTTON, {0}, 0, 0}, /* properties */ {TBICON_PROP, ID_PROP, TBSTATE_INDETERMINATE, BTNS_BUTTON, {0}, 0, 0}, /* properties */
{TBICON_REFRESH, ID_REFRESH, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0}, /* refresh */ {TBICON_REFRESH, ID_REFRESH, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0}, /* refresh */
@ -141,31 +141,19 @@ VOID SetMenuAndButtonStates(PMAIN_WND_INFO Info)
{ {
HMENU hMainMenu; HMENU hMainMenu;
DWORD Flags, State; DWORD Flags, State;
UINT i;
/* get handle to menu */ /* get handle to menu */
hMainMenu = GetMenu(Info->hMainWnd); hMainMenu = GetMenu(Info->hMainWnd);
/* set all to greyed */ /* set all to greyed */
EnableMenuItem(hMainMenu, ID_START, MF_GRAYED); for (i = ID_START; i <= ID_RESTART; i++)
EnableMenuItem(hMainMenu, ID_STOP, MF_GRAYED); {
EnableMenuItem(hMainMenu, ID_PAUSE, MF_GRAYED); EnableMenuItem(hMainMenu, i, MF_GRAYED);
EnableMenuItem(hMainMenu, ID_RESUME, MF_GRAYED);
EnableMenuItem(hMainMenu, ID_RESTART, MF_GRAYED);
EnableMenuItem(Info->hShortcutMenu, ID_START, MF_GRAYED); EnableMenuItem(Info->hShortcutMenu, ID_START, MF_GRAYED);
EnableMenuItem(Info->hShortcutMenu, ID_STOP, MF_GRAYED); SendMessage(Info->hTool, TB_SETSTATE, i,
EnableMenuItem(Info->hShortcutMenu, ID_PAUSE, MF_GRAYED);
EnableMenuItem(Info->hShortcutMenu, ID_RESUME, MF_GRAYED);
EnableMenuItem(Info->hShortcutMenu, ID_RESTART, MF_GRAYED);
SendMessage(Info->hTool, TB_SETSTATE, ID_START,
(LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
SendMessage(Info->hTool, TB_SETSTATE, ID_STOP,
(LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
SendMessage(Info->hTool, TB_SETSTATE, ID_PAUSE,
(LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
SendMessage(Info->hTool, TB_SETSTATE, ID_RESTART,
(LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0)); (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
}
if (Info->SelectedItem != NO_ITEM_SELECTED) if (Info->SelectedItem != NO_ITEM_SELECTED)
{ {
@ -249,7 +237,7 @@ CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
static BOOL static BOOL
pCreateToolbar(PMAIN_WND_INFO Info) pCreateToolbar(PMAIN_WND_INFO Info)
{ {
INT NumButtons = sizeof(Buttons) / sizeof(Buttons[0]); INT numButtons = sizeof(Buttons) / sizeof(Buttons[0]);
Info->hTool = CreateWindowEx(0, Info->hTool = CreateWindowEx(0,
TOOLBARCLASSNAME, TOOLBARCLASSNAME,
@ -288,7 +276,7 @@ pCreateToolbar(PMAIN_WND_INFO Info)
SendMessage(Info->hTool, SendMessage(Info->hTool,
TB_ADDBUTTONS, TB_ADDBUTTONS,
NumButtons, numButtons,
(LPARAM)Buttons); (LPARAM)Buttons);
return TRUE; return TRUE;
@ -346,7 +334,6 @@ InitListViewImage(PMAIN_WND_INFO Info)
(void)ListView_SetImageList(Info->hListView, (void)ListView_SetImageList(Info->hListView,
hLarge, hLarge,
LVSIL_NORMAL); LVSIL_NORMAL);
} }
@ -472,7 +459,6 @@ CreateStatusBar(PMAIN_WND_INFO Info)
if(Info->hStatus == NULL) if(Info->hStatus == NULL)
return FALSE; return FALSE;
SendMessage(Info->hStatus, SendMessage(Info->hStatus,
SB_SETPARTS, SB_SETPARTS,
sizeof(StatWidths) / sizeof(INT), sizeof(StatWidths) / sizeof(INT),
@ -485,7 +471,6 @@ static VOID
ListViewSelectionChanged(PMAIN_WND_INFO Info, ListViewSelectionChanged(PMAIN_WND_INFO Info,
LPNMLISTVIEW pnmv) LPNMLISTVIEW pnmv)
{ {
HMENU hMainMenu; HMENU hMainMenu;
/* get handle to menu */ /* get handle to menu */
@ -543,16 +528,19 @@ ListViewSelectionChanged(PMAIN_WND_INFO Info,
} }
static VOID static BOOL
InitMainWnd(PMAIN_WND_INFO Info) InitMainWnd(PMAIN_WND_INFO Info)
{ {
if (!pCreateToolbar(Info)) if (!pCreateToolbar(Info))
{
DisplayString(_T("error creating toolbar")); DisplayString(_T("error creating toolbar"));
return FALSE;
}
if (!CreateListView(Info)) if (!CreateListView(Info))
{ {
DisplayString(_T("error creating list view")); DisplayString(_T("error creating list view"));
return; return FALSE;
} }
if (!CreateStatusBar(Info)) if (!CreateStatusBar(Info))
@ -563,6 +551,8 @@ InitMainWnd(PMAIN_WND_INFO Info)
MAKEINTRESOURCE(IDR_POPUP)); MAKEINTRESOURCE(IDR_POPUP));
Info->hShortcutMenu = GetSubMenu(Info->hShortcutMenu, Info->hShortcutMenu = GetSubMenu(Info->hShortcutMenu,
0); 0);
return TRUE;
} }
@ -820,7 +810,8 @@ MainWndProc(HWND hwnd,
GWLP_USERDATA, GWLP_USERDATA,
(LONG_PTR)Info); (LONG_PTR)Info);
InitMainWnd(Info); if (!InitMainWnd(Info))
return -1;
/* Show the window */ /* Show the window */
ShowWindow(hwnd, ShowWindow(hwnd,
@ -1007,10 +998,9 @@ MainWndProc(HWND hwnd,
case WM_CLOSE: case WM_CLOSE:
{ {
/* Free service array */
HeapFree(ProcessHeap, HeapFree(ProcessHeap,
0, 0,
Info->pServiceStatus); Info->pAllServices);
DestroyMenu(Info->hShortcutMenu); DestroyMenu(Info->hShortcutMenu);
DestroyWindow(hwnd); DestroyWindow(hwnd);
@ -1019,8 +1009,6 @@ MainWndProc(HWND hwnd,
case WM_DESTROY: case WM_DESTROY:
{ {
//DestroyMainWnd(Info);
HeapFree(ProcessHeap, HeapFree(ProcessHeap,
0, 0,
Info); Info);
@ -1028,7 +1016,6 @@ MainWndProc(HWND hwnd,
GWLP_USERDATA, GWLP_USERDATA,
0); 0);
/* Break the message queue loop */
PostQuitMessage(0); PostQuitMessage(0);
} }
break; break;
@ -1044,6 +1031,7 @@ HandleDefaultMessage:
} }
break; break;
} }
return Ret; return Ret;
} }
@ -1122,4 +1110,3 @@ UninitMainWindowImpl(VOID)
UnregisterClass(szMainWndClass, UnregisterClass(szMainWndClass,
hInstance); hInstance);
} }

View file

@ -15,9 +15,6 @@
#define NO_ITEM_SELECTED -1 #define NO_ITEM_SELECTED -1
#define MAX_KEY_LENGTH 256 #define MAX_KEY_LENGTH 256
#define NUM_BUTTONS 11
#define PROGRESSRANGE 8
typedef struct _MAIN_WND_INFO typedef struct _MAIN_WND_INFO
{ {
@ -28,7 +25,7 @@ typedef struct _MAIN_WND_INFO
HMENU hShortcutMenu; HMENU hShortcutMenu;
int nCmdShow; int nCmdShow;
ENUM_SERVICE_STATUS_PROCESS *pServiceStatus; /* Stores the complete services array */ ENUM_SERVICE_STATUS_PROCESS *pAllServices;
ENUM_SERVICE_STATUS_PROCESS *CurrentService; /* Stores the current selected service */ ENUM_SERVICE_STATUS_PROCESS *CurrentService; /* Stores the current selected service */
INT SelectedItem;/* selection number in the list view */ INT SelectedItem;/* selection number in the list view */
@ -79,7 +76,7 @@ BOOL SetDescription(LPTSTR, LPTSTR);
LPTSTR GetDescription(LPTSTR); LPTSTR GetDescription(LPTSTR);
LPTSTR GetExecutablePath(PMAIN_WND_INFO Info); LPTSTR GetExecutablePath(PMAIN_WND_INFO Info);
BOOL RefreshServiceList(PMAIN_WND_INFO Info); BOOL RefreshServiceList(PMAIN_WND_INFO Info);
DWORD GetServiceList(PMAIN_WND_INFO Info); //DWORD GetServiceList(PMAIN_WND_INFO Info);
/* propsheet.c */ /* propsheet.c */
LONG APIENTRY OpenPropSheet(PMAIN_WND_INFO Info); LONG APIENTRY OpenPropSheet(PMAIN_WND_INFO Info);
@ -91,35 +88,27 @@ VOID ExportFile(PMAIN_WND_INFO Info);
INT AllocAndLoadString(OUT LPTSTR *lpTarget, INT AllocAndLoadString(OUT LPTSTR *lpTarget,
IN HINSTANCE hInst, IN HINSTANCE hInst,
IN UINT uID); IN UINT uID);
DWORD LoadAndFormatString(IN HINSTANCE hInstance, DWORD LoadAndFormatString(IN HINSTANCE hInstance,
IN UINT uID, IN UINT uID,
OUT LPTSTR *lpTarget, OUT LPTSTR *lpTarget,
...); ...);
BOOL StatusBarLoadAndFormatString(IN HWND hStatusBar, BOOL StatusBarLoadAndFormatString(IN HWND hStatusBar,
IN INT PartId, IN INT PartId,
IN HINSTANCE hInstance, IN HINSTANCE hInstance,
IN UINT uID, IN UINT uID,
...); ...);
BOOL StatusBarLoadString(IN HWND hStatusBar, BOOL StatusBarLoadString(IN HWND hStatusBar,
IN INT PartId, IN INT PartId,
IN HINSTANCE hInstance, IN HINSTANCE hInstance,
IN UINT uID); IN UINT uID);
INT GetTextFromEdit(OUT LPTSTR lpString, INT GetTextFromEdit(OUT LPTSTR lpString,
IN HWND hDlg, IN HWND hDlg,
IN UINT Res); IN UINT Res);
VOID GetError(VOID); VOID GetError(VOID);
VOID DisplayString(PTCHAR); VOID DisplayString(PTCHAR);
HIMAGELIST InitImageList(UINT NumButtons, HIMAGELIST InitImageList(UINT NumButtons,
UINT StartResource, UINT StartResource,
UINT Width, UINT Width,
UINT Height); UINT Height);
#endif /* __SERVMAN_PRECOMP_H */ #endif /* __SERVMAN_PRECOMP_H */

View file

@ -9,6 +9,8 @@
#include "precomp.h" #include "precomp.h"
#define PROGRESSRANGE 8
VOID VOID
CompleteProgressBar(HWND hProgDlg) CompleteProgressBar(HWND hProgDlg)
{ {

View file

@ -27,7 +27,6 @@ GetSelectedService(PMAIN_WND_INFO Info)
} }
/* get vendor of service binary */ /* get vendor of service binary */
LPTSTR LPTSTR
GetExecutablePath(PMAIN_WND_INFO Info) GetExecutablePath(PMAIN_WND_INFO Info)
@ -99,21 +98,90 @@ cleanup:
} }
static BOOL
GetServiceList(PMAIN_WND_INFO Info,
DWORD *NumServices)
{
SC_HANDLE ScHandle;
BOOL bRet = FALSE;
DWORD BytesNeeded = 0;
DWORD ResumeHandle = 0;
*NumServices = 0;
ScHandle = OpenSCManager(NULL,
NULL,
SC_MANAGER_ENUMERATE_SERVICE);
if (ScHandle != INVALID_HANDLE_VALUE)
{
if (!EnumServicesStatusEx(ScHandle,
SC_ENUM_PROCESS_INFO,
SERVICE_WIN32,
SERVICE_STATE_ALL,
NULL,
0,
&BytesNeeded,
NumServices,
&ResumeHandle,
0))
{
/* Call function again if required size was returned */
if (GetLastError() == ERROR_MORE_DATA)
{
/* reserve memory for service info array */
Info->pAllServices = (ENUM_SERVICE_STATUS_PROCESS *) HeapAlloc(ProcessHeap,
0,
BytesNeeded);
if (Info->pAllServices)
{
/* fill array with service info */
if (EnumServicesStatusEx(ScHandle,
SC_ENUM_PROCESS_INFO,
SERVICE_WIN32,
SERVICE_STATE_ALL,
(LPBYTE)Info->pAllServices,
BytesNeeded,
&BytesNeeded,
NumServices,
&ResumeHandle,
0))
{
bRet = TRUE;
}
}
}
}
}
if (ScHandle)
CloseServiceHandle(ScHandle);
if (!bRet)
{
HeapFree(ProcessHeap,
0,
Info->pAllServices);
}
return bRet;
}
BOOL BOOL
RefreshServiceList(PMAIN_WND_INFO Info) RefreshServiceList(PMAIN_WND_INFO Info)
{ {
ENUM_SERVICE_STATUS_PROCESS *pService;
LVITEM lvItem; LVITEM lvItem;
TCHAR szNumServices[32]; TCHAR szNumServices[32];
TCHAR szStatus[64]; TCHAR szStatus[64];
DWORD NumServices = 0; DWORD NumServices;
DWORD Index; DWORD Index;
LPCTSTR Path = _T("System\\CurrentControlSet\\Services\\%s"); LPCTSTR Path = _T("System\\CurrentControlSet\\Services\\%s");
(void)ListView_DeleteAllItems(Info->hListView); (void)ListView_DeleteAllItems(Info->hListView);
NumServices = GetServiceList(Info); if (GetServiceList(Info, &NumServices))
if (NumServices)
{ {
TCHAR buf[300]; /* buffer to hold key path */ TCHAR buf[300]; /* buffer to hold key path */
INT NumListedServ = 0; /* how many services were listed */ INT NumListedServ = 0; /* how many services were listed */
@ -122,38 +190,43 @@ RefreshServiceList(PMAIN_WND_INFO Info)
{ {
HKEY hKey = NULL; HKEY hKey = NULL;
LPTSTR lpDescription = NULL; LPTSTR lpDescription = NULL;
LPTSTR LogOnAs = NULL; LPTSTR lpLogOnAs = NULL;
DWORD StartUp = 0; DWORD StartUp = 0;
DWORD dwValueSize; DWORD dwValueSize;
/* copy the service info over */
pService = &Info->pAllServices[Index];
/* open the registry key for the service */ /* open the registry key for the service */
_sntprintf(buf, _sntprintf(buf,
300, 300,
Path, Path,
Info->pServiceStatus[Index].lpServiceName); pService->lpServiceName);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
RegOpenKeyEx(HKEY_LOCAL_MACHINE,
buf, buf,
0, 0,
KEY_READ, KEY_READ,
&hKey); &hKey) != ERROR_SUCCESS)
{
HeapFree(ProcessHeap,
0,
pService);
continue;
}
/* set the display name */ /* set the display name */
ZeroMemory(&lvItem, sizeof(LVITEM)); ZeroMemory(&lvItem, sizeof(LVITEM));
lvItem.mask = LVIF_TEXT | LVIF_PARAM; lvItem.mask = LVIF_TEXT | LVIF_PARAM;
lvItem.pszText = Info->pServiceStatus[Index].lpDisplayName; lvItem.pszText = pService->lpDisplayName;
/* Set a pointer for each service so we can query it later. /* Add the service pointer */
* Not all services are added to the list, so we can't query lvItem.lParam = (LPARAM)pService;
* the item number as they become out of sync with the array */
lvItem.lParam = (LPARAM)&Info->pServiceStatus[Index];
lvItem.iItem = ListView_GetItemCount(Info->hListView); /* add it to the listview */
lvItem.iItem = ListView_InsertItem(Info->hListView, &lvItem); lvItem.iItem = ListView_InsertItem(Info->hListView, &lvItem);
/* set the description */ /* set the description */
if ((lpDescription = GetDescription(Info->pServiceStatus[Index].lpServiceName))) if ((lpDescription = GetDescription(pService->lpServiceName)))
{ {
lvItem.pszText = lpDescription; lvItem.pszText = lpDescription;
lvItem.iSubItem = 1; lvItem.iSubItem = 1;
@ -168,7 +241,7 @@ RefreshServiceList(PMAIN_WND_INFO Info)
} }
/* set the status */ /* set the status */
if (Info->pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING) if (pService->ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING)
{ {
LoadString(hInstance, LoadString(hInstance,
IDS_SERVICES_STARTED, IDS_SERVICES_STARTED,
@ -189,44 +262,34 @@ RefreshServiceList(PMAIN_WND_INFO Info)
NULL, NULL,
NULL, NULL,
(LPBYTE)&StartUp, (LPBYTE)&StartUp,
&dwValueSize)) &dwValueSize) == ERROR_SUCCESS)
{ {
RegCloseKey(hKey); switch (StartUp)
continue;
}
if (StartUp == 0x02)
{ {
LoadString(hInstance, case 2:
LoadStringW(hInstance,
IDS_SERVICES_AUTO, IDS_SERVICES_AUTO,
szStatus, szStatus,
sizeof(szStatus) / sizeof(TCHAR)); sizeof(szStatus) / sizeof(TCHAR));
lvItem.pszText = szStatus; break;
lvItem.iSubItem = 3; case 3:
SendMessage(Info->hListView, LoadStringW(hInstance,
LVM_SETITEMTEXT,
lvItem.iItem,
(LPARAM)&lvItem);
}
else if (StartUp == 0x03)
{
LoadString(hInstance,
IDS_SERVICES_MAN, IDS_SERVICES_MAN,
szStatus, szStatus,
sizeof(szStatus) / sizeof(TCHAR)); sizeof(szStatus) / sizeof(TCHAR));
lvItem.pszText = szStatus; break;
lvItem.iSubItem = 3;
SendMessage(Info->hListView, case 4:
LVM_SETITEMTEXT, LoadStringW(hInstance,
lvItem.iItem,
(LPARAM)&lvItem);
}
else if (StartUp == 0x04)
{
LoadString(hInstance,
IDS_SERVICES_DIS, IDS_SERVICES_DIS,
szStatus, szStatus,
sizeof(szStatus) / sizeof(TCHAR)); sizeof(szStatus) / sizeof(TCHAR));
break;
default:
szStatus[0] = 0;
break;
}
lvItem.pszText = szStatus; lvItem.pszText = szStatus;
lvItem.iSubItem = 3; lvItem.iSubItem = 3;
SendMessage(Info->hListView, SendMessage(Info->hListView,
@ -242,52 +305,39 @@ RefreshServiceList(PMAIN_WND_INFO Info)
NULL, NULL,
NULL, NULL,
NULL, NULL,
&dwValueSize)) &dwValueSize) == ERROR_SUCCESS)
{ {
RegCloseKey(hKey); lpLogOnAs = HeapAlloc(ProcessHeap,
continue; 0,
}
LogOnAs = HeapAlloc(ProcessHeap,
HEAP_ZERO_MEMORY,
dwValueSize); dwValueSize);
if (LogOnAs == NULL) if (lpLogOnAs != NULL)
{ {
RegCloseKey(hKey);
return FALSE;
}
if(RegQueryValueEx(hKey, if(RegQueryValueEx(hKey,
_T("ObjectName"), _T("ObjectName"),
NULL, NULL,
NULL, NULL,
(LPBYTE)LogOnAs, (LPBYTE)lpLogOnAs,
&dwValueSize)) &dwValueSize) == ERROR_SUCCESS)
{ {
HeapFree(ProcessHeap, lvItem.pszText = lpLogOnAs;
0,
LogOnAs);
RegCloseKey(hKey);
continue;
}
lvItem.pszText = LogOnAs;
lvItem.iSubItem = 4; lvItem.iSubItem = 4;
SendMessage(Info->hListView, SendMessage(Info->hListView,
LVM_SETITEMTEXT, LVM_SETITEMTEXT,
lvItem.iItem, lvItem.iItem,
(LPARAM)&lvItem); (LPARAM)&lvItem);
}
HeapFree(ProcessHeap, HeapFree(ProcessHeap,
0, 0,
LogOnAs); lpLogOnAs);
RegCloseKey(hKey);
} }
NumListedServ = ListView_GetItemCount(Info->hListView); RegCloseKey(hKey);
}
}
/* set the number of listed services in the status bar */ /* set the number of listed services in the status bar */
NumListedServ = ListView_GetItemCount(Info->hListView);
LoadString(hInstance, LoadString(hInstance,
IDS_NUM_SERVICES, IDS_NUM_SERVICES,
szNumServices, szNumServices,
@ -312,74 +362,3 @@ RefreshServiceList(PMAIN_WND_INFO Info)
return TRUE; return TRUE;
} }
DWORD
GetServiceList(PMAIN_WND_INFO Info)
{
SC_HANDLE ScHandle;
BOOL bGotServices = FALSE;
DWORD BytesNeeded = 0;
DWORD ResumeHandle = 0;
DWORD NumServices = 0;
ScHandle = OpenSCManager(NULL,
NULL,
SC_MANAGER_ENUMERATE_SERVICE);
if (ScHandle != INVALID_HANDLE_VALUE)
{
if (!EnumServicesStatusEx(ScHandle,
SC_ENUM_PROCESS_INFO,
SERVICE_WIN32,
SERVICE_STATE_ALL,
(LPBYTE)Info->pServiceStatus,
0,
&BytesNeeded,
&NumServices,
&ResumeHandle,
0))
{
/* Call function again if required size was returned */
if (GetLastError() == ERROR_MORE_DATA)
{
/* reserve memory for service info array */
Info->pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *)
HeapAlloc(ProcessHeap,
0,
BytesNeeded);
if (Info->pServiceStatus == NULL)
return FALSE;
/* fill array with service info */
if (EnumServicesStatusEx(ScHandle,
SC_ENUM_PROCESS_INFO,
SERVICE_WIN32,
SERVICE_STATE_ALL,
(LPBYTE)Info->pServiceStatus,
BytesNeeded,
&BytesNeeded,
&NumServices,
&ResumeHandle,
0))
{
bGotServices = TRUE;
}
}
}
}
if (ScHandle)
CloseServiceHandle(ScHandle);
if (!bGotServices)
{
HeapFree(ProcessHeap,
0,
Info->pServiceStatus);
}
return NumServices;
}