ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

srvpage.c
Go to the documentation of this file.
00001 /*
00002  * PROJECT:     ReactOS Applications
00003  * LICENSE:     LGPL - See COPYING in the top level directory
00004  * FILE:        base/applications/srvpage.c
00005  * PURPOSE:     Services page message handler
00006  * COPYRIGHT:   Copyright 2005-2006 Christoph von Wittich <Christoph@ApiViewer.de>
00007  *
00008  */
00009 
00010 #include <precomp.h>
00011 
00012 HWND hServicesPage;
00013 HWND hServicesListCtrl;
00014 HWND hServicesDialog;
00015 
00016 void GetServices ( void );
00017 
00018 INT_PTR CALLBACK
00019 ServicesPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
00020 {
00021     LV_COLUMN   column;
00022     TCHAR       szTemp[256];
00023     DWORD dwStyle;
00024 
00025     UNREFERENCED_PARAMETER(lParam);
00026     UNREFERENCED_PARAMETER(wParam);
00027 
00028     switch (message) {
00029     case WM_INITDIALOG:
00030 
00031         hServicesListCtrl = GetDlgItem(hDlg, IDC_SERVICES_LIST);
00032         hServicesDialog = hDlg;
00033 
00034         dwStyle = (DWORD) SendMessage(hServicesListCtrl, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
00035         dwStyle = dwStyle | LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES;
00036         SendMessage(hServicesListCtrl, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, dwStyle);
00037 
00038         SetWindowPos(hDlg, NULL, 10, 32, 0, 0, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOZORDER);
00039 
00040         // Initialize the application page's controls
00041         column.mask = LVCF_TEXT | LVCF_WIDTH;
00042 
00043         LoadString(hInst, IDS_SERVICES_COLUMN_SERVICE, szTemp, 256);
00044         column.pszText = szTemp;
00045         column.cx = 200;
00046         (void)ListView_InsertColumn(hServicesListCtrl, 0, &column);
00047 
00048         column.mask = LVCF_TEXT | LVCF_WIDTH;
00049         LoadString(hInst, IDS_SERVICES_COLUMN_REQ, szTemp, 256);
00050         column.pszText = szTemp;
00051         column.cx = 70;
00052         (void)ListView_InsertColumn(hServicesListCtrl, 1, &column);
00053 
00054         column.mask = LVCF_TEXT | LVCF_WIDTH;
00055         LoadString(hInst, IDS_SERVICES_COLUMN_VENDOR, szTemp, 256);
00056         column.pszText = szTemp;
00057         column.cx = 200;
00058         (void)ListView_InsertColumn(hServicesListCtrl, 2, &column);
00059 
00060         column.mask = LVCF_TEXT | LVCF_WIDTH;
00061         LoadString(hInst, IDS_SERVICES_COLUMN_STATUS, szTemp, 256);
00062         column.pszText = szTemp;
00063         column.cx = 70;
00064         (void)ListView_InsertColumn(hServicesListCtrl, 3, &column);
00065 
00066         GetServices();
00067         return TRUE;
00068     }
00069 
00070     return 0;
00071 }
00072 
00073 void
00074 GetServices ( void )
00075 {
00076     LV_ITEM item;
00077     WORD wCodePage;
00078     WORD wLangID;
00079     SC_HANDLE ScHandle;
00080     SC_HANDLE hService;
00081     DWORD BytesNeeded = 0;
00082     DWORD ResumeHandle = 0;
00083     DWORD NumServices = 0;
00084     DWORD dwHandle, dwLen;
00085     size_t Index;
00086     UINT BufLen;
00087     TCHAR szStatus[128];
00088     TCHAR* lpData;
00089     TCHAR* lpBuffer;
00090     TCHAR szStrFileInfo[80];
00091     TCHAR FileName[MAX_PATH];
00092     LPVOID pvData;
00093 
00094     LPSERVICE_FAILURE_ACTIONS pServiceFailureActions = NULL;
00095     LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
00096     ENUM_SERVICE_STATUS_PROCESS *pServiceStatus = NULL;
00097 
00098     ScHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
00099     if (ScHandle != INVALID_HANDLE_VALUE)
00100     {
00101         if (EnumServicesStatusEx(ScHandle, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pServiceStatus, 0, &BytesNeeded, &NumServices, &ResumeHandle, 0) == 0)
00102         {
00103             /* Call function again if required size was returned */
00104             if (GetLastError() == ERROR_MORE_DATA)
00105             {
00106                 /* reserve memory for service info array */
00107                 pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *) HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
00108                 if (!pServiceStatus)
00109                     return;
00110 
00111                 /* fill array with service info */
00112                 if (EnumServicesStatusEx(ScHandle, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pServiceStatus, BytesNeeded, &BytesNeeded, &NumServices, &ResumeHandle, 0) == 0)
00113                 {
00114                     HeapFree(GetProcessHeap(), 0, pServiceStatus);
00115                     return;
00116                 }
00117             }
00118             else /* exit on failure */
00119             {
00120                 return;
00121             }
00122         }
00123 
00124         if (NumServices)
00125         {
00126             if (!pServiceStatus)
00127                 return;
00128             for (Index = 0; Index < NumServices; Index++)
00129             {
00130                 memset(&item, 0, sizeof(LV_ITEM));
00131                 item.mask = LVIF_TEXT;
00132                 item.iImage = 0;
00133                 item.pszText = pServiceStatus[Index].lpDisplayName;
00134                 item.iItem = ListView_GetItemCount(hServicesListCtrl);
00135                 item.lParam = 0;
00136                 item.iItem = ListView_InsertItem(hServicesListCtrl, &item);
00137 
00138                 if (pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING)
00139                 {
00140                     ListView_SetCheckState(hServicesListCtrl, item.iItem, TRUE);
00141                 }
00142 
00143                 BytesNeeded = 0;
00144                 hService = OpenService(ScHandle, pServiceStatus[Index].lpServiceName, SC_MANAGER_CONNECT);
00145                 if (hService != INVALID_HANDLE_VALUE)
00146                 {
00147                     /* check if service is required by the system*/
00148                     if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)NULL, 0, &BytesNeeded))
00149                     {
00150                         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
00151                         {
00152                             pServiceFailureActions = (LPSERVICE_FAILURE_ACTIONS) HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
00153                             if (pServiceFailureActions == NULL)
00154                                 return;
00155 
00156                             if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)pServiceFailureActions, BytesNeeded, &BytesNeeded))
00157                             {
00158                                 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
00159                                 HeapFree(GetProcessHeap(), 0, pServiceStatus);
00160                                 CloseServiceHandle(hService);
00161                                 CloseServiceHandle(ScHandle);
00162                                 return;
00163                             }
00164                         }
00165                         else /* exit on failure */
00166                         {
00167                             HeapFree(GetProcessHeap(), 0, pServiceStatus);
00168                             CloseServiceHandle(hService);
00169                             CloseServiceHandle(ScHandle);
00170                             return;
00171                         }
00172                     }
00173                     if (pServiceFailureActions->cActions)
00174                     {
00175                         if (pServiceFailureActions->lpsaActions[0].Type == SC_ACTION_REBOOT)
00176                         {
00177                             LoadString(hInst, IDS_SERVICES_YES, szStatus, 128);
00178                             item.pszText = szStatus;
00179                             item.iSubItem = 1;
00180                             SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
00181                         }
00182                     }
00183 
00184                     if (pServiceFailureActions != NULL)
00185                     {
00186                         HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
00187                         pServiceFailureActions = NULL;
00188                     }
00189 
00190                     /* get vendor of service binary */
00191                     BytesNeeded = 0;
00192                     if (!QueryServiceConfig(hService, NULL, 0, &BytesNeeded))
00193                     {
00194                         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
00195                         {
00196                             pServiceConfig = (LPQUERY_SERVICE_CONFIG) HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
00197                             if (pServiceConfig == NULL)
00198                             {
00199                                 HeapFree(GetProcessHeap(), 0, pServiceStatus);
00200                                 CloseServiceHandle(hService);
00201                                 CloseServiceHandle(ScHandle);
00202                                 return;
00203                             }
00204                             if (!QueryServiceConfig(hService, pServiceConfig, BytesNeeded, &BytesNeeded))
00205                             {
00206                                 HeapFree(GetProcessHeap(), 0, pServiceConfig);
00207                                 HeapFree(GetProcessHeap(), 0, pServiceStatus);
00208                                 CloseServiceHandle(hService);
00209                                 CloseServiceHandle(ScHandle);
00210                                 return;
00211                             }
00212                         }
00213                         else /* exit on failure */
00214                         {
00215                             HeapFree(GetProcessHeap(), 0, pServiceStatus);
00216                             CloseServiceHandle(hService);
00217                             CloseServiceHandle(ScHandle);
00218                             return;
00219                         }
00220                     }
00221 
00222                     memset(&FileName, 0, MAX_PATH);
00223                     if (_tcscspn(pServiceConfig->lpBinaryPathName, _T("\"")))
00224                     {
00225                         _tcsncpy(FileName, pServiceConfig->lpBinaryPathName, _tcscspn(pServiceConfig->lpBinaryPathName, _T(" ")) );
00226                     }
00227                     else
00228                     {
00229                         _tcscpy(FileName, pServiceConfig->lpBinaryPathName);
00230                     }
00231 
00232                     HeapFree(GetProcessHeap(), 0, pServiceConfig);
00233                     pServiceConfig = NULL;
00234 
00235                     dwLen = GetFileVersionInfoSize(FileName, &dwHandle);
00236                     if (dwLen)
00237                     {
00238                         lpData = (TCHAR*) HeapAlloc(GetProcessHeap(), 0, dwLen);
00239                         if (lpData == NULL)
00240                         {
00241                             HeapFree(GetProcessHeap(), 0, pServiceStatus);
00242                             CloseServiceHandle(hService);
00243                             CloseServiceHandle(ScHandle);
00244                             return;
00245                         }
00246                         if (!GetFileVersionInfo (FileName, dwHandle, dwLen, lpData))
00247                         {
00248                             HeapFree(GetProcessHeap(), 0, lpData);
00249                             HeapFree(GetProcessHeap(), 0, pServiceStatus);
00250                             CloseServiceHandle(hService);
00251                             CloseServiceHandle(ScHandle);
00252                             return;
00253                         }
00254 
00255                         if (VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), &pvData, (PUINT) &BufLen))
00256                         {
00257                             wCodePage = LOWORD(*(DWORD*) pvData);
00258                             wLangID = HIWORD(*(DWORD*) pvData);
00259                             wsprintf(szStrFileInfo, _T("StringFileInfo\\%04X%04X\\CompanyName"), wCodePage, wLangID);
00260                         }
00261 
00262                         if (VerQueryValue (lpData, szStrFileInfo, (void**) &lpBuffer, (PUINT) &BufLen))
00263                         {
00264                             item.pszText = lpBuffer;
00265                             item.iSubItem = 2;
00266                             SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
00267                         }
00268                         HeapFree(GetProcessHeap(), 0, lpData);
00269                     }
00270                     else
00271                     {
00272                         LoadString(hInst, IDS_SERVICES_UNKNOWN, szStatus, 128);
00273                         item.pszText = szStatus;
00274                         item.iSubItem = 2;
00275                         SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
00276                     }
00277                     CloseServiceHandle(hService);
00278                 }
00279 
00280                 LoadString(hInst, ((pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_STOPPED) ? IDS_SERVICES_STATUS_STOPPED : IDS_SERVICES_STATUS_RUNNING), szStatus, 128);
00281                 item.pszText = szStatus;
00282                 item.iSubItem = 3;
00283                 SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
00284 
00285             }
00286         }
00287 
00288         HeapFree(GetProcessHeap(), 0, pServiceStatus);
00289         CloseServiceHandle(ScHandle);
00290     }
00291 
00292 }

Generated on Sun May 27 2012 04:16:57 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.