473,320 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ServiceController class startup type

HI,
I am writing a utility which will list all the services
running on system like services.msc program. Now I want
to display the Startup type of service like
manual/automation.

How can I get that property? I just want to read that
property, and do not want to create new one.

TIA,
Vinay
Nov 15 '05 #1
3 12628
hi Vinay,
I'm not sure if there's a .net function for what you need, but i know
for some information about services, you need to resort to win32 calls,
so you might find this helpful. be aware that it was my first time
getting down and dirty with marshalling in c#, but it seems to work fine
for me (except for TagID. i wasn't really sure what it does, so don't
rely on it's value).

Vinay wrote:
HI,
I am writing a utility which will list all the services
running on system like services.msc program. Now I want
to display the Startup type of service like
manual/automation.

How can I get that property? I just want to read that
property, and do not want to create new one.

TIA,
Vinay

public namespace Services
{
/// <summary>
/// Some wrappers around Win32 calls dealing with services.
/// </summary>
public class ServiceConfigurator
{
[StructLayout(LayoutKind.Sequential)]
private struct QueryServiceConfigStruct
{
public int serviceType;
public int startType;
public int errorControl;
public IntPtr binaryPathName;
public IntPtr loadOrderGroup;
public int tagID;
public IntPtr dependencies;
public IntPtr startName;
public IntPtr displayName;
}

public struct ServiceInfo
{
public int serviceType;
public int startType;
public int errorControl;
public string binaryPathName;
public string loadOrderGroup;
public int tagID;
public string dependencies;
public string startName;
public string displayName;
}

#region constants
private enum SCManagerAccess :int
{
GENERIC_ALL = 0x10000000
}
private enum ServiceAccess :int
{
QUERY_CONFIG = 0x1,
CHANGE_CONFIG = 0x2,
}
private const int SERVICE_NO_CHANGE = 0xFFFF;
#endregion

#region DllImports
[DllImport("advapi32.dll",
SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr OpenSCManager(
[MarshalAs(UnmanagedType.LPTStr)]
string machineName,
[MarshalAs(UnmanagedType.LPTStr)]
string databaseName,
int desiredAccess);

[DllImport("advapi32.dll",
SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr OpenService(
IntPtr scManager,
[MarshalAs(UnmanagedType.LPTStr)]
string serviceName,
int desiredAccess);

[DllImport("advapi32.dll",
SetLastError = true, CharSet = CharSet.Auto)]
private static extern int ChangeServiceConfig(
IntPtr service,
int serviceType,
int startType,
int errorControl,
[MarshalAs(UnmanagedType.LPTStr)]
string binaryPathName,
[MarshalAs(UnmanagedType.LPTStr)]
string loadOrderGroup,
IntPtr tagID,
[MarshalAs(UnmanagedType.LPTStr)]
string dependencies,
[MarshalAs(UnmanagedType.LPTStr)]
string startName,
[MarshalAs(UnmanagedType.LPTStr)]
string password,
[MarshalAs(UnmanagedType.LPTStr)]
string displayName);

[DllImport("advapi32.dll",
SetLastError = true, CharSet = CharSet.Auto)]
private static extern int QueryServiceConfig(
IntPtr service,
IntPtr queryServiceConfig,
int bufferSize,
ref int bytesNeeded);
#endregion

public static ServiceInfo GetServiceInfo(string ServiceName)
{
if (ServiceName.Equals(""))
throw new NullReferenceException(
"ServiceName must contain a valid service name.");
IntPtr scManager = OpenSCManager(".", null,
(int)SCManagerAccess.GENERIC_ALL);
if (scManager.ToInt32() <= 0)
throw new Win32Exception();

IntPtr service = OpenService(scManager,
ServiceName, (int) ServiceAccess.QUERY_CONFIG);
if (service.ToInt32() <= 0)
throw new NullReferenceException();

int bytesNeeded = 5;
QueryServiceConfigStruct qscs = new QueryServiceConfigStruct();
IntPtr qscPtr = Marshal.AllocCoTaskMem(0);

int retCode = QueryServiceConfig(service, qscPtr,
0, ref bytesNeeded);
if (retCode == 0 && bytesNeeded == 0)
{
throw new Win32Exception();
}
else
{
qscPtr = Marshal.AllocCoTaskMem(bytesNeeded);
retCode = QueryServiceConfig(service, qscPtr,
bytesNeeded, ref bytesNeeded);
if (retCode == 0)
{
throw new Win32Exception();
}
qscs.binaryPathName = IntPtr.Zero;
qscs.dependencies = IntPtr.Zero;
qscs.displayName = IntPtr.Zero;
qscs.loadOrderGroup = IntPtr.Zero;
qscs.startName = IntPtr.Zero;

qscs = (QueryServiceConfigStruct)
Marshal.PtrToStructure(qscPtr,
new QueryServiceConfigStruct().GetType());
}

ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.binaryPathName =
Marshal.PtrToStringAuto(qscs.binaryPathName);
serviceInfo.dependencies =
Marshal.PtrToStringAuto(qscs.dependencies);
serviceInfo.displayName =
Marshal.PtrToStringAuto(qscs.displayName);
serviceInfo.loadOrderGroup =
Marshal.PtrToStringAuto(qscs.loadOrderGroup);
serviceInfo.startName =
Marshal.PtrToStringAuto(qscs.startName);

serviceInfo.errorControl = qscs.errorControl;
serviceInfo.serviceType = qscs.serviceType;
serviceInfo.startType = qscs.startType;
serviceInfo.tagID = qscs.tagID;

Marshal.FreeCoTaskMem(qscPtr);
return serviceInfo;
}

public static void ChangeAccount(string ServiceName,
string Username, string Password)
{
ServiceInfo serviceInfo = GetServiceInfo(ServiceName);

IntPtr scManager = OpenSCManager(".", null,
(int)SCManagerAccess.GENERIC_ALL);
if (scManager.ToInt32() <= 0)
throw new Win32Exception();

IntPtr service = OpenService(scManager,
ServiceName, (int)ServiceAccess.CHANGE_CONFIG);
if (service.ToInt32() <= 0)
throw new Win32Exception();

if (ChangeServiceConfig(service, serviceInfo.serviceType,
serviceInfo.startType, serviceInfo.errorControl,
serviceInfo.binaryPathName, serviceInfo.loadOrderGroup,
IntPtr.Zero, serviceInfo.dependencies,
Username, Password, serviceInfo.displayName) == 0)
{
throw new Win32Exception();
}
}

}
}
Nov 15 '05 #2
Vinay,

You can get service information through the QueryServiceConfig API
function. You can call this through the P/Invoke layer and it should give
you all the information that you need about the service.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Vinay" <vi***********@hotmail.com> wrote in message
news:3e****************************@phx.gbl...
HI,
I am writing a utility which will list all the services
running on system like services.msc program. Now I want
to display the Startup type of service like
manual/automation.

How can I get that property? I just want to read that
property, and do not want to create new one.

TIA,
Vinay

Nov 15 '05 #3
-----Original Message-----
Vinay,

You can get service information through the QueryServiceConfig APIfunction. You can call this through the P/Invoke layer and it should giveyou all the information that you need about the service.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
Nicholas,
Thanks for the help. Do you tell me the protocol of the
imported function in C#?
like bool QueryServiceConfig(int, ??, int, int**)
or you want me to write QUERY_SERVICE_CONFIG structure on
my own?

TIA,
Vinay - ni**************@exisconsulting.com

"Vinay" <vi***********@hotmail.com> wrote in message
news:3e****************************@phx.gbl...
HI,
I am writing a utility which will list all the services
running on system like services.msc program. Now I want
to display the Startup type of service like
manual/automation.

How can I get that property? I just want to read that
property, and do not want to create new one.

TIA,
Vinay

.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jon Hyland | last post by:
Ok, I'm a little rusty on this, it should be a simple problem but I can't figure it out. How can I handle form events in my main code page?? I'm creating a Windows App in C#. Rather than make...
10
by: Bill Burris | last post by:
How do I set the ServiceType to InteractiveProcess, since this property is read only? Is there a way to set this with code in my Installer class, or a standalone process? I normally set this...
2
by: Jonathan Leonard | last post by:
Hello, I'm using the .NET class ServiceController to start my service. After calling start, a subsequent call to ServiceController.Status is returning 'stopped.' I know the service is running...
0
by: Ray L | last post by:
Hi guys. Simple question. I gather that a service can not pause and start/stop itself so you need a controller class. Do I need a separate solution/program. Or can I just create a new form in the...
6
by: Arnie | last post by:
We're using the ServiceController class provided by the .NET Framework, programming in C#. We are using the Start() method to start a service from another service. This works fine most of the...
3
by: Bram Hoefnagel | last post by:
Hi, On the 1.1 framework I couldn't find a solution to get a list of services from an other PC. Only a list of services on My local PC. (witch is strange i guess, as you look to the MSDN doc.) ...
16
by: Robert Dufour | last post by:
Here's the class code snippet Imports System.Configuration.ConfigurationManager Public Class Class1 Public _strTestSetting As String Public Sub SetTestsetting()
7
by: =?Utf-8?B?YXVsZGg=?= | last post by:
hello, i'm try to research the way to open a remote machine's to collect the services and processes. i have the local data collection running but i can not figure out what MSDN or the C# help file...
3
by: MedIt | last post by:
Hi all, I am trying to controll a service programatically using the ServiceController class .net c#. It is giving me an error that it cannot find the service name. I have tried it stopping...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.