C#对应的函数声明
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{
public partial class WinAPI
{
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static IntPtr OpenSCManager(
string lpMachineName, string lpDatabaseName, uint dwDesiredAccess);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static IntPtr CreateService(
IntPtr hSCManager, string lpServiceName, string lpDisplayName,
uint dwDesiredAccess, uint dwServerType, uint dwStartType,
uint dwErrorControl, string lpBinaryPathName,
// 后面的参数都填null就行了
string lpLoadOrderGroup, [Out] uint[] lpdwTagId, string lpDependencies,
string lpServiceStartName, string lpPassword);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static IntPtr OpenService(
IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool CloseServiceHandle(IntPtr hSCObject);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool StartService(
IntPtr hService, uint dwNumServiceArgs, string[] lpServiceArgVectors);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool ControlService(
IntPtr hService, uint dwControl, [Out] ServiceStatus lpServiceStatus);
[StructLayout(LayoutKind.Sequential)]
public class ServiceStatus
{
public uint dwServiceType;
public uint dwCurrentState;
public uint dwControlsAccepted;
public uint dwWin32ExitCode;
public uint dwServiceSpecificExitCode;
public uint dwCheckPoint;
public uint dwWaitHint;
}
public static readonly uint SC_MANAGER_ALL_ACCESS = 0xf003f;
public static readonly uint SERVICE_ALL_ACCESS = 0xf01ff;
public static readonly uint SERVICE_KERNEL_DRIVER = 1;
public static readonly uint SERVICE_DEMAND_START = 3;
public static readonly uint SERVICE_ERROR_IGNORE = 0;
public static readonly uint SERVICE_CONTROL_STOP = 1;
}
}