public partial class test : Form
{
   [DllImport("Kernel32.dll")]
   public static extern bool CloseHandle(IntPtr hObject);   [DllImport("Ntdll.dll", EntryPoint = "NtQuerySystemInformation")]
    public static extern int NtQuerySystemInformation(
            int SystemInformationClass,             //系统信息类型枚举值
            uint[] SystemInformation,         //存放取到的信息的(c++用PVOID)
            ulong SystemInformationLength,    //参数2的大小
            //ulong returnLength //这个值好像调用后没效果,很多地方用IntPtr.Zero
            IntPtr handle
            );
    
        public test()
        {
            InitializeComponent();       
     
            ArrayList aList = new ArrayList();
            uint[] handleList = new uint[602400];//需要先分配好空间,这个602400是我多次调试弄出的,小点就出错了要取的大点。
            int iReturn = NtQuerySystemInformation(16, handleList, (ulong)handleList.Length, IntPtr.Zero);
            int h = Process.GetCurrentProcess().Id; 
            for (int i = 0; i < handleList.Length; i++)
            {
                if (h == (int)handleList[i]) //这里为了取到那些PID 等于当前进程ID的
                {
                    aList.Add(i);
                    uint hs = handleList[i + 3];
                    br = CloseHandle((IntPtr)handleList[i + 3]);//这里都false i+1,i+2都错误
                }
            }
            MessageBox.Show(aList.Count.ToString());//为了看当前程序含有的句柄数            
        }
}说明:SystemInformationClass为16取的是系统句柄信息。iReturn = 0 调用成功,本程序也调用成功的。
NtQuerySystemInformation在MSDN上的原型,感觉说的有点含糊,具体参数值都没介绍
NTSTATUS NtQuerySystemInformation(
  SYSTEM_INFORMATION_CLASS SystemInformationClass,
  PVOID SystemInformation,
  ULONG SystemInformationLength,
  PULONG ReturnLength
);
问题:
1.NtQuerySystemInformation是c++函数,我转移到c#中,那些参数是否正确??特别是uint[] SystemInformation, 这个参数在c++中是指针,用来存放取到的句柄信息的,我这里用uint[]可以取到数据,但用byte[],ulong[]也能取到数据,更奇怪的是每种类型取到的数据值都不一样的,很奇怪,比如4 在定义成uint,byte,ulong都是4吧?2。确认1中的参数没有问题后,我监听handleList中的数据,发现很明显handleList[0]就是句柄总数,下面的数据看去就是4个一组的,那handleList[1]到handleList[4]这4个数值代表什么呢?3.很多c++资料上看到他们调用NtQuerySystemInformation都把取到的信息都放到这么一个结构体中,
typedef struct _SYSTEM_HANDLE_INFORMATION {
    ULONG  ProcessId;
    UCHAR  ObjectTypeNumber;
    UCHAR  Flags;
    USHORT  Handle;
    PVOID  Object;
    ACCESS_MASK  GrantedAccess;
} SYSTEM_HANDLE_INFORMATION, *PSYSTEM_HANDLE_INFORMATION;
根据结构体是6个属性,那感觉(问题2)中返回数据也应该是6个一组呀,为什么我调用的是4个一组,还是我搞错了(因为我看到每隔4个那个值就重复了,那个值应该就是句柄的ProcessId)???请比较了解的人指导指导我,不甚感谢。联系我 QQ:94199785,说明来源。

解决方案 »

  1.   

    顺便给出我查询到得一些资料,吗地,MSDN上为什么就不能把他们给拿出来呢??气氛。
    系统句柄类型:
    SYSTEM_HANDLE_TYPE = (OB_TYPE_UNKNOWN, 
    OB_TYPE_TYPE, 
    OB_TYPE_DIRECTORY, 
    OB_TYPE_SYMBOLIC_LINK, 
    OB_TYPE_TOKEN, 
    OB_TYPE_PROCESS, 
    OB_TYPE_THREAD, 
    OB_TYPE_UNKNOWN_7, 
    OB_TYPE_EVENT, 
    OB_TYPE_EVENT_PAIR, 
    OB_TYPE_MUTANT, 
    OB_TYPE_UNKNOWN_11, 
    OB_TYPE_SEMAPHORE, 
    OB_TYPE_TIMER, 
    OB_TYPE_PROFILE, 
    OB_TYPE_WINDOW_STATION, 
    OB_TYPE_DESKTOP, 
    OB_TYPE_SECTION, 
    OB_TYPE_KEY, 
    OB_TYPE_PORT, 
    OB_TYPE_WAITABLE_PORT, 
    OB_TYPE_UNKNOWN_21, 
    OB_TYPE_UNKNOWN_22, 
    OB_TYPE_UNKNOWN_23, 
    OB_TYPE_UNKNOWN_24, 
    //OB_TYPE_CONTROLLER, 
    //OB_TYPE_DEVICE, 
    //OB_TYPE_DRIVER, 
    OB_TYPE_IO_COMPLETION, 
    OB_TYPE_FILE); 
      TSystemHandleType = SYSTEM_HANDLE_TYPE;SYSTEM_INFORMATION_CLASS = ( 
              SystemBasicInformation = 0, 
              SystemProcessorInformation, 
              SystemPerformanceInformation, 
              SystemTimeOfDayInformation, 
              SystemNotImplemented1, 
              SystemProcessesAndThreadsInformation, 
              SystemCallCounts, 
              SystemConfigurationInformation, 
              SystemProcessorTimes, 
              SystemGlobalFlag, 
              SystemNotImplemented2, 
              SystemModuleInformation, 
              SystemLockInformation, 
              SystemNotImplemented3, 
              SystemNotImplemented4, 
              SystemNotImplemented5, 
              SystemHandleInformation, 
              SystemObjectInformation, 
              SystemPagefileInformation, 
              SystemInstructionEmulationCounts, 
              SystemInvalidInfoClass1, 
              SystemCacheInformation, 
              SystemPoolTagInformation, 
              SystemProcessorStatistics, 
              SystemDpcInformation, 
              SystemNotImplemented6, 
              SystemLoadImage, 
              SystemUnloadImage, 
              SystemTimeAdjustment, 
              SystemNotImplemented7, 
              SystemNotImplemented8, 
              SystemNotImplemented9, 
              SystemCrashDumpInformation, 
              SystemExceptionInformation, 
              SystemCrashDumpStateInformation, 
              SystemKernelDebuggerInformation, 
              SystemContextSwitchInformation, 
              SystemRegistryQuotaInformation, 
              SystemLoadAndCallImage, 
              SystemPrioritySeparation, 
              SystemNotImplemented10, 
              SystemNotImplemented11, 
              SystemInvalidInfoClass2, 
              SystemInvalidInfoClass3, 
              SystemTimeZoneInformation, 
              SystemLookasideInformation, 
              SystemSetTimeSlipEvent, 
              SystemCreateSession, 
              SystemDeleteSession, 
              SystemInvalidInfoClass4, 
              SystemRangeStartInformation, 
              SystemVerifierInformation, 
              SystemAddVerifier, 
              SystemSessionProcessesInformation); 
      TSystemInformationClass = SYSTEM_INFORMATION_CLASS;