/// <summary> /// /// </summary> /// <param name="ServiceName"></param> /// <param name="DisplayName"></param> /// <param name="PathName"></param> /// <returns></returns> public static bool InstallService(string ServiceName, string DisplayName, string PathName) { bool retValue = false; IntPtr scm_handle = IntPtr.Zero; IntPtr srv_handle = IntPtr.Zero; try { scm_handle = ServicesAPI.OpenSCManager(null, null, ServicesAPI.SC_DESIRED_ACCESS.SC_MANAGER_CREATE_SERVICE); if (scm_handle == IntPtr.Zero) { return(false); } srv_handle = ServicesAPI.CreateService(scm_handle, ServiceName, DisplayName, (ServicesAPI.SERVICE_DESIRED_ACCESS) 0, ServicesAPI.SERVICE_TYPE.SERVICE_WIN32_OWN_PROCESS, ServicesAPI.START_TYPE.SERVICE_AUTO_START, ServicesAPI.ERROR_CONTROL_TYPE.SERVICE_ERROR_NORMAL, PathName, null, 0, null, null, null); retValue = srv_handle != IntPtr.Zero; } catch (Exception ex) { Console.WriteLine("(InstallService) Exception: {0}", ex.Message); retValue = false; } finally { if (srv_handle != IntPtr.Zero) { ServicesAPI.CloseServiceHandle(srv_handle); } if (scm_handle != IntPtr.Zero) { ServicesAPI.CloseServiceHandle(scm_handle); } } return(retValue); }
/// <summary> /// /// </summary> /// <param name="ServiceName"></param> /// <returns></returns> public static bool IsServiceInstalled(string ServiceName) { bool retValue = false; IntPtr scm_handle = IntPtr.Zero, srv_handle = IntPtr.Zero; try { scm_handle = ServicesAPI.OpenSCManager(null, null, ServicesAPI.SC_DESIRED_ACCESS.SC_MANAGER_CREATE_SERVICE); if (scm_handle == IntPtr.Zero) { return(false); } srv_handle = ServicesAPI.OpenService(scm_handle, ServiceName, ServicesAPI.SERVICE_DESIRED_ACCESS.SERVICE_ALL_ACCESS); retValue = srv_handle != IntPtr.Zero; } catch (Exception ex) { Console.WriteLine("(_IsServiceInstalled) Exception: {0}", ex.Message); retValue = false; } finally { if (srv_handle != IntPtr.Zero) { ServicesAPI.CloseServiceHandle(srv_handle); } if (scm_handle != IntPtr.Zero) { ServicesAPI.CloseServiceHandle(scm_handle); } } return(retValue); }
/// <summary> /// /// </summary> /// <param name="ServiceName"></param> /// <returns></returns> public static string GetServiceBinary(string ServiceName) { string retValue = null; IntPtr scm_handle = IntPtr.Zero; IntPtr srv_handle = IntPtr.Zero; try { scm_handle = ServicesAPI.OpenSCManager(null, null, ServicesAPI.SC_DESIRED_ACCESS.SC_MANAGER_ALL_ACCESS); if (scm_handle != IntPtr.Zero) { srv_handle = ServicesAPI.OpenService(scm_handle, ServiceName, ServicesAPI.SERVICE_DESIRED_ACCESS.SERVICE_QUERY_CONFIG); if (srv_handle != IntPtr.Zero) { UInt32 dwBytesNeeded = 0; // Allocate memory for struct. IntPtr ptr = Marshal.AllocHGlobal(4096); try { bool success = ServicesAPI.QueryServiceConfig(srv_handle, ptr, 4096, out dwBytesNeeded); if (success) { QUERY_SERVICE_CONFIG qsc = new QUERY_SERVICE_CONFIG(); // Copy Marshal.PtrToStructure(ptr, qsc); retValue = qsc.lpBinaryPathName; } } finally { // Free memory for struct. Marshal.FreeHGlobal(ptr); } } } } finally { if (srv_handle != IntPtr.Zero) { ServicesAPI.CloseServiceHandle(srv_handle); } if (scm_handle != IntPtr.Zero) { ServicesAPI.CloseServiceHandle(scm_handle); } } return(retValue); }