在没有权限的情况下获取系统信息(一)

写了一个监控服务的DLL,提供用户签入服务中

可以自行上行当前系统运行状态。

但是当用户是IIS站点的时候,权限比较小,所以无法使用性能计数器和wmi服务

只能通过调用系统API的形式完成, 查询到 NtQuerySystemInformation 这个API。

先解决 取CPU负载的

 

 1     internal class CpuUsageNt
 2     {
 3         /// <summary>
 4         /// Initializes a new CpuUsageNt instance.
 5         /// </summary>
 6         /// <exception cref="NotSupportedException">One of the system calls fails.</exception>
 7         public static void Init()
 8         {
 9             byte[] timeInfo = new byte[32];     // SYSTEM_TIME_INFORMATION structure
10             byte[] perfInfo = new byte[600];    // SYSTEM_PERFORMANCE_INFORMATION structure
11             byte[] baseInfo = new byte[200];     // SYSTEM_BASIC_INFORMATION structure
12             int ret;
13 
14             // get new system time
15             ret = NtQuerySystemInformation(SYSTEM_TIMEINFORMATION, timeInfo, timeInfo.Length, IntPtr.Zero);
16             if (ret != NO_ERROR)
17                 throw new NotSupportedException();
18 
19             // get new CPU's idle time
20             ret = NtQuerySystemInformation(SYSTEM_PERFORMANCEINFORMATION, perfInfo, perfInfo.Length, IntPtr.Zero);
21             if (ret != NO_ERROR)
22                 throw new NotSupportedException();
23 
24             // store new CPU's idle and system time and number of processors
25             oldIdleTime = BitConverter.ToInt64(perfInfo, 0); // SYSTEM_PERFORMANCE_INFORMATION.liIdleTime
26             oldSystemTime = BitConverter.ToInt64(timeInfo, 8); // SYSTEM_TIME_INFORMATION.liKeSystemTime
27             processorCount = Environment.ProcessorCount;// baseInfo[40];
28         }
29         /// <summary>
30         /// Determines the current average CPU load.
31         /// </summary>
32         /// <returns>An integer that holds the CPU load percentage.</returns>
33         /// <exception cref="NotSupportedException">One of the system calls fails. The CPU time can not be obtained.</exception>
34         public static float Query()
35         {
36             try
37             {
38                 byte[] timeInfo = new byte[32];     // SYSTEM_TIME_INFORMATION structure
39                 byte[] perfInfo = new byte[600];    // SYSTEM_PERFORMANCE_INFORMATION structure
40                 double dbIdleTime, dbSystemTime;
41                 int ret;
42                 // get new system time
43                 ret = NtQuerySystemInformation(SYSTEM_TIMEINFORMATION, timeInfo, timeInfo.Length, IntPtr.Zero);
44                 if (ret != NO_ERROR)
45                     throw new NotSupportedException();
46                 // get new CPU's idle time
47                 ret = NtQuerySystemInformation(SYSTEM_PERFORMANCEINFORMATION, perfInfo, perfInfo.Length, IntPtr.Zero);
48                 if (ret != NO_ERROR)
49                     throw new NotSupportedException();
50                 // CurrentValue = NewValue - OldValue
51                 dbIdleTime = BitConverter.ToInt64(perfInfo, 0) - oldIdleTime;
52                 dbSystemTime = BitConverter.ToInt64(timeInfo, 8) - oldSystemTime;
53                 // CurrentCpuIdle = IdleTime / SystemTime
54                 if (dbSystemTime != 0)
55                     dbIdleTime = dbIdleTime / dbSystemTime;
56                 // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
57                 dbIdleTime = 100.0 - dbIdleTime * 100.0 / processorCount + 0.5;
58                 // store new CPU's idle and system time
59                 oldIdleTime = BitConverter.ToInt64(perfInfo, 0); // SYSTEM_PERFORMANCE_INFORMATION.liIdleTime
60                 oldSystemTime = BitConverter.ToInt64(timeInfo, 8); // SYSTEM_TIME_INFORMATION.liKeSystemTime
61                 return (float)dbIdleTime;
62             }
63             catch
64             {
65                 return 0;
66             }
67         }
68         /// <summary>
69         /// NtQuerySystemInformation is an internal Windows function that retrieves various kinds of system information.
70         /// </summary>
71         /// <param name="dwInfoType">One of the values enumerated in SYSTEM_INFORMATION_CLASS, indicating the kind of system information to be retrieved.</param>
72         /// <param name="lpStructure">Points to a buffer where the requested information is to be returned. The size and structure of this information varies depending on the value of the SystemInformationClass parameter.</param>
73         /// <param name="dwSize">Length of the buffer pointed to by the SystemInformation parameter.</param>
74         /// <param name="returnLength">Optional pointer to a location where the function writes the actual size of the information requested.</param>
75         /// <returns>Returns a success NTSTATUS if successful, and an NTSTATUS error code otherwise.</returns>
76         [DllImport("ntdll", EntryPoint = "NtQuerySystemInformation")]
77         private static extern int NtQuerySystemInformation(int dwInfoType, byte[] lpStructure, int dwSize, IntPtr returnLength);
78         /// <summary>Returns the number of processors in the system in a SYSTEM_BASIC_INFORMATION structure.</summary>
79         private const int SYSTEM_BASICINFORMATION = 0;
80         /// <summary>Returns an opaque SYSTEM_PERFORMANCE_INFORMATION structure.</summary>
81         private const int SYSTEM_PERFORMANCEINFORMATION = 2;
82         /// <summary>Returns an opaque SYSTEM_TIMEOFDAY_INFORMATION structure.</summary>
83         private const int SYSTEM_TIMEINFORMATION = 3;
84         /// <summary>The value returned by NtQuerySystemInformation is no error occurred.</summary>
85         private const int NO_ERROR = 0;
86         /// <summary>Holds the old idle time.</summary>
87         private static long oldIdleTime;
88         /// <summary>Holds the old system time.</summary>
89         private static long oldSystemTime;
90         /// <summary>Holds the number of processors in the system.</summary>
91         private static double processorCount;
92     }

 

明天再研究研究怎么取内存和分区信息,那些本来是用wmi取的

 

转载于:https://www.cnblogs.com/dl2k/p/3548588.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值