Ignore:
Timestamp:
May 26, 2009, 3:31:47 AM (15 years ago)
Author:
rryu
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MacFaceLibrary/NtKernel.cs

    • Property svn:keywords set to Id
    r121 r124  
    1 using System;
     1// $Id$
     2using System;
    23using System.Runtime.InteropServices;
    34
    4 class NtKernel {
     5public class NtKernel {
    56
    6     unsafe public static SYSTEM_BASIC_INFORMATION QuerySystemBasicInformation()
     7    public static SYSTEM_BASIC_INFORMATION QuerySystemBasicInformation()
    78    {
    8         SYSTEM_BASIC_INFORMATION info = new SYSTEM_BASIC_INFORMATION();
    9         UInt32 len = (UInt32)Marshal.SizeOf(typeof(SYSTEM_BASIC_INFORMATION));
     9        int len = Marshal.SizeOf(typeof(SYSTEM_BASIC_INFORMATION));
     10        IntPtr ptr = Marshal.AllocCoTaskMem(len);
    1011        UInt32 outLen;
    1112
    1213        NtQuerySystemInformation(
    13             SYSTEM_INFORMATION_CLASS.SystemBasicInformation, &info, len, out outLen
     14            SYSTEM_INFORMATION_CLASS.SystemBasicInformation, ptr, (UInt32)len, out outLen
    1415        );
    15         return info;
     16
     17        return (SYSTEM_BASIC_INFORMATION)Marshal.PtrToStructure(ptr, typeof(SYSTEM_BASIC_INFORMATION));
    1618    }
    1719
    18     unsafe public static SYSTEM_PERFORMANCE_INFORMATION QuerySystemPerformanceInformation()
     20    public static SYSTEM_PERFORMANCE_INFORMATION QuerySystemPerformanceInformation()
    1921    {
    20         SYSTEM_PERFORMANCE_INFORMATION info = new SYSTEM_PERFORMANCE_INFORMATION();
    21         UInt32 len = (UInt32)Marshal.SizeOf(typeof(SYSTEM_PERFORMANCE_INFORMATION));
     22        int len = Marshal.SizeOf(typeof(SYSTEM_PERFORMANCE_INFORMATION));
     23        IntPtr ptr = Marshal.AllocCoTaskMem(len);
    2224        UInt32 outLen;
    2325
    2426        NtQuerySystemInformation(
    25             SYSTEM_INFORMATION_CLASS.SystemPerformanceInformation, &info, len, out outLen
     27            SYSTEM_INFORMATION_CLASS.SystemPerformanceInformation, ptr, (UInt32)len, out outLen
    2628        );
    27         return info;
     29
     30        return (SYSTEM_PERFORMANCE_INFORMATION)Marshal.PtrToStructure(ptr, typeof(SYSTEM_PERFORMANCE_INFORMATION));
    2831    }
    2932
    30     unsafe public static SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION QuerySystemProcessorPerformanceInfomation()
     33    public static SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[] QuerySystemProcessorPerformanceInfomation(int processorCount)
    3134    {
    32         SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION info = new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION();
    33         UInt32 len = (UInt32)(Marshal.SizeOf(typeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)));
     35        int len = Marshal.SizeOf(typeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)) * processorCount;
     36        IntPtr ptr = Marshal.AllocCoTaskMem(len);
    3437        UInt32 outLen;
     38
    3539        NtQuerySystemInformation(
    36             SYSTEM_INFORMATION_CLASS.SystemProcessorPerformanceInformation, &info, len, out outLen
     40            SYSTEM_INFORMATION_CLASS.SystemProcessorPerformanceInformation, ptr, (UInt32)len, out outLen
    3741        );
    38         return info;
     42
     43        SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[] list = new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[processorCount];
     44        for (int i = 0; i < processorCount; i++) {
     45            IntPtr p = new IntPtr(ptr.ToInt32() + Marshal.SizeOf(typeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)) * i);
     46            list[i] = (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)Marshal.PtrToStructure(p, typeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION));
     47        }
     48   
     49        return list;
    3950    }
    4051
    4152    [DllImport("ntdll.dll")]
    42     unsafe protected extern static UInt32 NtQuerySystemInformation(
     53    protected extern static UInt32 NtQuerySystemInformation(
    4354        SYSTEM_INFORMATION_CLASS SystemInformationClass,
    44         void* SystemInformation,
     55        IntPtr SystemInformation,
    4556        UInt32 SystemInformationLength,
    4657        out UInt32 ReturnLength
     
    6374    public struct SYSTEM_BASIC_INFORMATION
    6475    {
    65         public UInt64 Unknown;
    66         public UInt64 MaximumIncrement;
    67         public UInt64 PhysicalPageSize;
    68         public UInt64 NumberOfPhysicalPages;
    69         public UInt64 LowestPhysicalPage;
    70         public UInt64 HighestPhysicalPage;
    71         public UInt64 AllocationGranularity;
    72         public UInt64 LowestUserAddress;
    73         public UInt64 HighestUserAddress;
    74         public UInt64 ActiveProcessors;
     76        public UInt32 Unknown;
     77        public UInt32 MaximumIncrement;
     78        public UInt32 PhysicalPageSize;
     79        public UInt32 NumberOfPhysicalPages;
     80        public UInt32 LowestPhysicalPage;
     81        public UInt32 HighestPhysicalPage;
     82        public UInt32 AllocationGranularity;
     83        public UInt32 LowestUserAddress;
     84        public UInt32 HighestUserAddress;
     85        public UInt32 ActiveProcessors;
    7586        public Byte NumberProcessors;
    7687    }
Note: See TracChangeset for help on using the changeset viewer.