调用方法基本正确,WINDOWS中有很多怪问题出现null Exception的.用 try{
...
}catch(Exception){}
容错就好.

解决方案 »

  1.   

    对了,静态的API调是static 的.
      

  2.   

    我使用new 是开辟数组空间,函数原型如下
    BOOL EnumServicesStatus(
      SC_HANDLE hSCManager,
      DWORD dwServiceType,
      DWORD dwServiceState,
      LPENUM_SERVICE_STATUS lpServices,
      DWORD cbBufSize,
      LPDWORD pcbBytesNeeded,
      LPDWORD lpServicesReturned,
      LPDWORD lpResumeHandle
    );
      

  3.   

    ess=new EnumServiceStatus[100];   --》ess=EnumServiceStatus[100];  试试
      

  4.   

    yaoyaonet(绿洲)
    编译都不通过:(
    还有这个函数是用来枚举windows2000的服务,
    我试了,如果调用函数时出错,将不能完全枚举服务
      

  5.   

    在C#中使用API首先要使用引入名字空间:
    using System.Runtime.InteropServices;然后
    [DllImport("AdvAPI32")]
    +函数说明
      

  6.   

    using System.Runtime.InteropServices;名称空间已经引用
    编译都通过了,是在运行时出现的错误提示,用try...catch...finally
    捕捉异常,将不能得到一个我所需要的东西.
      

  7.   

    函数原型:
    BOOL EnumServicesStatus(
      SC_HANDLE hSCManager,
      DWORD dwServiceType,
      DWORD dwServiceState,
      LPENUM_SERVICE_STATUS lpServices,
      DWORD cbBufSize,
      LPDWORD pcbBytesNeeded,
      LPDWORD lpServicesReturned,
      LPDWORD lpResumeHandle
    );
    C#结构
    [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
    public struct ServiceStatus
    {
    public ServiceType Type;
    public ServiceCurrentType CurrentType;
    public SerivceControlAccepted ControlAccepted;
    public int Win32ExitCode;
    public int ServiceSpecificExitCode;
    public int CheckPoint;
    public int WaitHint;
    }
    [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
    public struct EnumServiceStatus
    {
    [MarshalAs(UnmanagedType.LPTStr)]
    public String ServiceName;
    [MarshalAs(UnmanagedType.LPTStr)]
    public String DisplayName;
    public ServiceStatus status;
    }
    使用的枚举类型
    public enum ServiceCurrentType
    {
    Stopped =0x00000001,
    StartPending =0x00000002,
    StopPending =0x00000003,
    Running =0x00000004,
    ContinuePending =0x00000005,
    PausePending =0x00000006,
    Paused =0x00000007
    }
    public enum ServiceType
    {
    KernelDriver =0x00000001,
    FileSystemDriver =0x00000002,
    Adapter =0x00000004,
    RecognizerDriver =0x00000008,
    ServiceDriver =(KernelDriver|FileSystemDriver|RecognizerDriver),
    Win32_OwnProcess =0x00000010,
    Win32_ShareProcess =0x00000020,
    ServiceWin32 =(Win32_OwnProcess|Win32_ShareProcess),
    InterActiveProcess =0x00000100,
    TypeAll =(Adapter|ServiceDriver|ServiceWin32|InterActiveProcess)
    }
    public enum SerivceControlAccepted
    {
    Stop =0x00000001,
    PauseContinue =0x00000002,
    Shutdown =0x00000004,
    ParamChange =0x00000008,
    NetBindChange =0x00000010,
    HardwareProfileChange =0x00000020,
    PowerEvent =0x00000040,
    SessionChange =0x00000080
    }
    public enum ServiceActiveStatus
    {
    Active =0x00000001,
    InActive =0x00000002,
    StateAll =(Active|InActive)
    }
    详细的API说明在
    ms-help://MS.MSDNQTR.2003FEB.2052/dllproc/base/enumservicesstatus.htm中