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

query.c
Go to the documentation of this file.
00001 /*
00002  * PROJECT:     ReactOS Services
00003  * LICENSE:     GPL - See COPYING in the top level directory
00004  * FILE:        base/applications/mscutils/servman/query.c
00005  * PURPOSE:     Query service information
00006  * COPYRIGHT:   Copyright 2006-2007 Ged Murphy <gedmurphy@reactos.org>
00007  *
00008  */
00009 
00010 #include "precomp.h"
00011 
00012 
00013 ENUM_SERVICE_STATUS_PROCESS*
00014 GetSelectedService(PMAIN_WND_INFO Info)
00015 {
00016     LVITEM lvItem;
00017 
00018     lvItem.mask = LVIF_PARAM;
00019     lvItem.iItem = Info->SelectedItem;
00020     SendMessage(Info->hListView,
00021                 LVM_GETITEM,
00022                 0,
00023                 (LPARAM)&lvItem);
00024 
00025     /* return pointer to selected service */
00026     return (ENUM_SERVICE_STATUS_PROCESS *)lvItem.lParam;
00027 }
00028 
00029 
00030 LPQUERY_SERVICE_CONFIG
00031 GetServiceConfig(LPTSTR lpServiceName)
00032 {
00033     LPQUERY_SERVICE_CONFIG lpServiceConfig = NULL;
00034     SC_HANDLE hSCManager;
00035     SC_HANDLE hService;
00036     DWORD dwBytesNeeded;
00037 
00038     hSCManager = OpenSCManager(NULL,
00039                                NULL,
00040                                SC_MANAGER_ALL_ACCESS);
00041     if (hSCManager)
00042     {
00043         hService = OpenService(hSCManager,
00044                                lpServiceName,
00045                                SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS | SERVICE_QUERY_CONFIG);
00046         if (hService)
00047         {
00048             if (!QueryServiceConfig(hService,
00049                                     NULL,
00050                                     0,
00051                                     &dwBytesNeeded))
00052             {
00053                 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
00054                 {
00055                     lpServiceConfig = (LPQUERY_SERVICE_CONFIG)HeapAlloc(GetProcessHeap(),
00056                                                                         0,
00057                                                                         dwBytesNeeded);
00058                     if (lpServiceConfig)
00059                     {
00060                         if (!QueryServiceConfig(hService,
00061                                                 lpServiceConfig,
00062                                                 dwBytesNeeded,
00063                                                 &dwBytesNeeded))
00064                         {
00065                             HeapFree(GetProcessHeap(),
00066                                      0,
00067                                      lpServiceConfig);
00068                             lpServiceConfig = NULL;
00069                         }
00070                     }
00071                 }
00072             }
00073 
00074             CloseServiceHandle(hService);
00075         }
00076 
00077         CloseServiceHandle(hSCManager);
00078     }
00079 
00080     return lpServiceConfig;
00081 }
00082 
00083 
00084 BOOL
00085 SetServiceConfig(LPQUERY_SERVICE_CONFIG pServiceConfig,
00086                  LPTSTR lpServiceName,
00087                  LPTSTR lpPassword)
00088 {
00089     SC_HANDLE hSCManager;
00090     SC_HANDLE hSc;
00091     SC_LOCK scLock;
00092     BOOL bRet = FALSE;
00093 
00094     hSCManager = OpenSCManager(NULL,
00095                                NULL,
00096                                SC_MANAGER_LOCK);
00097     if (hSCManager)
00098     {
00099         scLock = LockServiceDatabase(hSCManager);
00100         if (scLock)
00101         {
00102             hSc = OpenService(hSCManager,
00103                               lpServiceName,
00104                               SERVICE_CHANGE_CONFIG);
00105             if (hSc)
00106             {
00107                 if (ChangeServiceConfig(hSc,
00108                                         pServiceConfig->dwServiceType,
00109                                         pServiceConfig->dwStartType,
00110                                         pServiceConfig->dwErrorControl,
00111                                         pServiceConfig->lpBinaryPathName,
00112                                         pServiceConfig->lpLoadOrderGroup,
00113                                         pServiceConfig->dwTagId ? &pServiceConfig->dwTagId : NULL,
00114                                         pServiceConfig->lpDependencies,
00115                                         pServiceConfig->lpServiceStartName,
00116                                         lpPassword,
00117                                         pServiceConfig->lpDisplayName))
00118                 {
00119                     bRet = TRUE;
00120                 }
00121 
00122                 CloseServiceHandle(hSc);
00123             }
00124 
00125             UnlockServiceDatabase(scLock);
00126         }
00127 
00128         CloseServiceHandle(hSCManager);
00129     }
00130 
00131     if (!bRet)
00132         GetError();
00133 
00134     return bRet;
00135 }
00136 
00137 
00138 LPTSTR
00139 GetServiceDescription(LPTSTR lpServiceName)
00140 {
00141     SC_HANDLE hSCManager = NULL;
00142     SC_HANDLE hSc = NULL;
00143     SERVICE_DESCRIPTION *pServiceDescription = NULL;
00144     LPTSTR lpDescription = NULL;
00145     DWORD BytesNeeded = 0;
00146     DWORD dwSize;
00147 
00148     hSCManager = OpenSCManager(NULL,
00149                                NULL,
00150                                SC_MANAGER_ENUMERATE_SERVICE);
00151     if (hSCManager == NULL)
00152     {
00153         GetError();
00154         return NULL;
00155     }
00156 
00157     hSc = OpenService(hSCManager,
00158                       lpServiceName,
00159                       SERVICE_QUERY_CONFIG);
00160     if (hSc)
00161     {
00162         if (!QueryServiceConfig2(hSc,
00163                                  SERVICE_CONFIG_DESCRIPTION,
00164                                  NULL,
00165                                  0,
00166                                  &BytesNeeded))
00167         {
00168             if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
00169             {
00170                 pServiceDescription = (SERVICE_DESCRIPTION *) HeapAlloc(ProcessHeap,
00171                                                                         0,
00172                                                                         BytesNeeded);
00173                 if (pServiceDescription == NULL)
00174                     goto cleanup;
00175 
00176                 if (QueryServiceConfig2(hSc,
00177                                         SERVICE_CONFIG_DESCRIPTION,
00178                                         (LPBYTE)pServiceDescription,
00179                                         BytesNeeded,
00180                                         &BytesNeeded))
00181                 {
00182                     if (pServiceDescription->lpDescription)
00183                     {
00184                         dwSize = _tcslen(pServiceDescription->lpDescription) + 1;
00185                         lpDescription = HeapAlloc(ProcessHeap,
00186                                                   0,
00187                                                   dwSize * sizeof(TCHAR));
00188                         if (lpDescription)
00189                         {
00190                             _tcscpy_s(lpDescription,
00191                                       dwSize,
00192                                       pServiceDescription->lpDescription);
00193                         }
00194                     }
00195                 }
00196             }
00197         }
00198     }
00199 
00200 cleanup:
00201     if (pServiceDescription)
00202         HeapFree(ProcessHeap,
00203                  0,
00204                  pServiceDescription);
00205     if (hSCManager != NULL)
00206         CloseServiceHandle(hSCManager);
00207     if (hSc != NULL)
00208         CloseServiceHandle(hSc);
00209 
00210     return lpDescription;
00211 }
00212 
00213 
00214 BOOL
00215 SetServiceDescription(LPTSTR lpServiceName,
00216                       LPTSTR lpDescription)
00217 {
00218     SC_HANDLE hSCManager;
00219     SC_HANDLE hSc;
00220     SC_LOCK scLock;
00221     SERVICE_DESCRIPTION ServiceDescription;
00222     BOOL bRet = FALSE;
00223 
00224     hSCManager = OpenSCManager(NULL,
00225                                NULL,
00226                                SC_MANAGER_LOCK);
00227     if (hSCManager)
00228     {
00229         scLock = LockServiceDatabase(hSCManager);
00230         if (scLock)
00231         {
00232             hSc = OpenService(hSCManager,
00233                               lpServiceName,
00234                               SERVICE_CHANGE_CONFIG);
00235             if (hSc)
00236             {
00237                 ServiceDescription.lpDescription = lpDescription;
00238 
00239                 if (ChangeServiceConfig2(hSc,
00240                                          SERVICE_CONFIG_DESCRIPTION,
00241                                          &ServiceDescription))
00242                 {
00243                     bRet = TRUE;
00244                 }
00245 
00246                 CloseServiceHandle(hSc);
00247             }
00248 
00249             UnlockServiceDatabase(scLock);
00250         }
00251 
00252         CloseServiceHandle(hSCManager);
00253     }
00254 
00255     if (!bRet)
00256         GetError();
00257 
00258     return bRet;
00259 }
00260 
00261 
00262 BOOL
00263 GetServiceList(PMAIN_WND_INFO Info,
00264                DWORD *NumServices)
00265 {
00266     SC_HANDLE ScHandle;
00267     BOOL bRet = FALSE;
00268 
00269     DWORD BytesNeeded = 0;
00270     DWORD ResumeHandle = 0;
00271 
00272     *NumServices = 0;
00273 
00274     if (Info->pAllServices != NULL)
00275     {
00276         HeapFree(ProcessHeap,
00277                      0,
00278                      Info->pAllServices);
00279     }
00280 
00281     ScHandle = OpenSCManager(NULL,
00282                              NULL,
00283                              SC_MANAGER_ENUMERATE_SERVICE);
00284     if (ScHandle != INVALID_HANDLE_VALUE)
00285     {
00286         if (!EnumServicesStatusEx(ScHandle,
00287                                   SC_ENUM_PROCESS_INFO,
00288                                   SERVICE_WIN32,
00289                                   SERVICE_STATE_ALL,
00290                                   NULL,
00291                                   0,
00292                                   &BytesNeeded,
00293                                   NumServices,
00294                                   &ResumeHandle,
00295                                   0))
00296         {
00297             /* Call function again if required size was returned */
00298             if (GetLastError() == ERROR_MORE_DATA)
00299             {
00300                 /* reserve memory for service info array */
00301                 Info->pAllServices = (ENUM_SERVICE_STATUS_PROCESS *) HeapAlloc(ProcessHeap,
00302                                                                          0,
00303                                                                          BytesNeeded);
00304                 if (Info->pAllServices)
00305                 {
00306                     /* fill array with service info */
00307                     if (EnumServicesStatusEx(ScHandle,
00308                                              SC_ENUM_PROCESS_INFO,
00309                                              SERVICE_WIN32,
00310                                              SERVICE_STATE_ALL,
00311                                              (LPBYTE)Info->pAllServices,
00312                                              BytesNeeded,
00313                                              &BytesNeeded,
00314                                              NumServices,
00315                                              &ResumeHandle,
00316                                              0))
00317                     {
00318                         bRet = TRUE;
00319                     }
00320                 }
00321             }
00322         }
00323     }
00324 
00325     if (ScHandle)
00326         CloseServiceHandle(ScHandle);
00327 
00328     if (!bRet)
00329     {
00330         HeapFree(ProcessHeap,
00331                  0,
00332                  Info->pAllServices);
00333     }
00334 
00335     return bRet;
00336 }
00337 
00338 
00339 BOOL
00340 UpdateServiceStatus(ENUM_SERVICE_STATUS_PROCESS* pService)
00341 {
00342     SC_HANDLE hScm;
00343     BOOL bRet = FALSE;
00344 
00345     hScm = OpenSCManager(NULL,
00346                              NULL,
00347                              SC_MANAGER_ENUMERATE_SERVICE);
00348     if (hScm != INVALID_HANDLE_VALUE)
00349     {
00350         SC_HANDLE hService;
00351 
00352         hService = OpenService(hScm,
00353                                pService->lpServiceName,
00354                                SERVICE_QUERY_STATUS);
00355         if (hService)
00356         {
00357             DWORD size;
00358 
00359             QueryServiceStatusEx(hService,
00360                                  SC_STATUS_PROCESS_INFO,
00361                                  (LPBYTE)&pService->ServiceStatusProcess,
00362                                  sizeof(SERVICE_STATUS_PROCESS),
00363                                  &size);
00364 
00365             CloseServiceHandle(hService);
00366             bRet = TRUE;
00367         }
00368 
00369         CloseServiceHandle(hScm);
00370     }
00371 
00372     return bRet;
00373 }

Generated on Thu May 24 2012 04:17:02 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.