Использование ZwQuerySystemInformation в C#: проблема
От: CL0NE Украина  
Дата: 07.09.09 20:32
Оценка:
Синтаксис данной функции (в дополнение к ссылке на мсдн — здесь):

NTSTATUS WINAPI ZwQuerySystemInformation(
  __in       SYSTEM_INFORMATION_CLASS SystemInformationClass,
  __inout    PVOID SystemInformation,
  __in       ULONG SystemInformationLength,
  __out_opt  PULONG ReturnLength
);


Привел я ее к такому виду в своем коде:
[DllImport("Ntdll.dll", EntryPoint="ZwQuerySystemInformation")]
static extern Int32 APIQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass,
               IntPtr SystemInformation,
               int SystemInformationLength,
               out long ReturnLength);


Первый параметр указывает на тип получаемой информации:

SystemInformationClass
The type of system information to be retrieved. This parameter can be one of the following values from the SYSTEM_INFORMATION_CLASS enumeration type.



сделал его енумерейшном:
        public enum SYSTEM_INFORMATION_CLASS
        {
            SystemBasicInformation = 0x02c,
            SystemPerformanceInformation = 0x138,
            SystemTimeOfDayInformation = 0x020,
            SystemProcessInformation = 0x088,
            SystemProcessorPerformanceInformation = 0x030,
            SystemInterruptInformation = 0x018,
            SystemExceptionInformation = 0x010
       }

Ради теста использовал первый вариант, самый простой:

SystemBasicInformation

The number of processors in the system in a SYSTEM_BASIC_INFORMATION structure. Use the GetSystemInfo function instead.


Второй параметр —

SystemInformation
A pointer to a buffer that receives the requested information. The size and structure of this information varies depending on the value of the SystemInformationClass parameter, as indicated in the following table.

(лень переводить)

Следовательно, при первом варианте получим структуру:

SYSTEM_BASIC_INFORMATION

When the SystemInformationClass parameter is SystemBasicInformation, the buffer pointed to by the SystemInformation parameter should be large enough to hold a single SYSTEM_BASIC_INFORMATION structure having the following layout:

typedef struct _SYSTEM_BASIC_INFORMATION {
    BYTE Reserved1[24];
    PVOID Reserved2[4];
    CCHAR NumberOfProcessors;
} SYSTEM_BASIC_INFORMATION;



The NumberOfProcessors member contains the number of processors present in the system. Use GetSystemInfo instead to retrieve this information.

The other members of the structure are reserved for internal use by the operating system.


Привел ее к "надлежащему" виду:


[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_BASIC_INFORMATION {
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
      public byte[] Reserved1;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
      public IntPtr[] Reserved2;
      public Char NumberOfProcessors;
}


При вызове функция возвращает код NTSATUS

//STATUS_SUCCESS              = NTStatus($00000000);
//STATUS_ACCESS_DENIED        = NTStatus($C0000022);
//STATUS_INFO_LENGTH_MISMATCH = NTStatus($C0000004);
//SEVERITY_ERROR              = NTStatus($C0000000);

Первый при удачном вызове, третий — при слишком маленьком буфере.

Третий параметр у функции — размер выделенного нами буфера, четвертый — обьем байт, записанных в буфер.

Мой тестовый код, попытка использовать данную функцию:


            long formal;
            int size = Marshal.SizeOf(typeof(SYSTEM_BASIC_INFORMATION));
            IntPtr ptr = Marshal.AllocHGlobal(size); 

            Console.WriteLine("NTSTATUS: " + APIQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemBasicInformation, ptr, size, out formal).ToString("X"));
            
            SYSTEM_BASIC_INFORMATION INFO = ((SYSTEM_BASIC_INFORMATION)Marshal.PtrToStructure(ptr, typeof(SYSTEM_BASIC_INFORMATION)));
            Console.WriteLine(INFO.NumberOfProcessors);
            Console.WriteLine(formal);


Ну а теперь, самое "вкусное":

  1. Если я неправильно привел к "надлежащему виду" сигнатуру функции и сами структуры — поправьте, пожалуйста, буду очень признателен
  2. Проблема: при вызове функция возвращает STATUS_INFO_LENGTH_MISMATCH. с чем это может быть связано?
Re: Использование ZwQuerySystemInformation в C#: проблема
От: Аноним  
Дата: 09.09.09 22:16
Оценка:
Благодаря форумчанину VPF mihryak'у обнаружил свою оплошность

а откуда ты взял значения энума SYSTEM_INFORMATION_CLASS?
везде, где попадались, SystemBasicInformation был равен 0
например, здесь
если взять оттуда и структуру

[StructLayout(LayoutKind.Sequential)]
struct SYSTEM_BASIC_INFORMATION
{
public int Reserved;
public int TimerResolution;
public int PageSize; // 4096 (4kB) on 32-bit systems, 8192 (8kB) on 64-bit systems
public int NumberOfPhysicalPages; // pages of physical memory
public int LowestPhysicalPageNumber;
public int HighestPhysicalPageNumber;
public int AllocationGranularity;
public int MinimumUserModeAddress;
public int MaximumUserModeAddress;
public int ActiveProcessorsAffinityMask;
public byte NumberOfProcessors;
}

то на домашней висте успешно выдалось 2 процессора
а вот на рабочем 2003 сервере не прокатило
тем не менее, и там, и там по коду 0x02c мне выдавался требуемый размер памяти, равный 172, ни из твоего варианта структуры, ни из моего такой размер не получить, у нас 44
так, кстати, я и убедился, что 0 — подходящее значение, при нём и при 62(0x3E) выдавалось 44 требуемых байта (при 62 те же значения полей структуры). ни одно другое число из интервала 0..99999999 44 не дало



[q]
а откуда ты взял значения энума SYSTEM_INFORMATION_CLASS?

Только что, погуглив, нашел где я их взял. http://undocumented.ntinternals.net/
о_О Спросонья я почемуто посчитал

Buffer size : 0x02C

значением...

Мораль: никогда не пишите код на сонную голову! :D
[/q]
Re: Использование ZwQuerySystemInformation в C#: проблема
От: CL0NE Украина  
Дата: 22.09.09 12:43
Оценка:
 public enum SYSTEM_INFORMATION_CLASS : int
        {
            SystemBasicInformation,
            SystemProcessorInformation,
            SystemPerformanceInformation,
            SystemTimeOfDayInformation,
            SystemPathInformation,
            SystemProcessInformation,
            SystemCallCountInformation,
            SystemDeviceInformation,
            SystemProcessorPerformanceInformation,
            SystemFlagsInformation,
            SystemCallTimeInformation,
            SystemModuleInformation,
            SystemLocksInformation,
            SystemStackTraceInformation,
            SystemPagedPoolInformation,
            SystemNonPagedPoolInformation,
            SystemHandleInformation,
            SystemObjectInformation,
            SystemPageFileInformation,
            SystemVdmInstemulInformation,
            SystemVdmBopInformation,
            SystemFileCacheInformation,
            SystemPoolTagInformation,
            SystemInterruptInformation,
            SystemDpcBehaviorInformation,
            SystemFullMemoryInformation,
            SystemLoadGdiDriverInformation,
            SystemUnloadGdiDriverInformation,
            SystemTimeAdjustmentInformation,
            SystemSummaryMemoryInformation,
            SystemMirrorMemoryInformation,
            SystemPerformanceTraceInformation,
            SystemCrashDumpInformation,
            SystemExceptionInformation,
            SystemCrashDumpStateInformation,
            SystemKernelDebuggerInformation,
            SystemContextSwitchInformation,
            SystemRegistryQuotaInformation,
            SystemExtendServiceTableInformation, // used to be SystemLoadAndCallImage
            SystemPrioritySeparation,
            SystemVerifierAddDriverInformation,
            SystemVerifierRemoveDriverInformation,
            SystemProcessorIdleInformation,
            SystemLegacyDriverInformation,
            SystemCurrentTimeZoneInformation,
            SystemLookasideInformation,
            SystemTimeSlipNotification,
            SystemSessionCreate,
            SystemSessionDetach,
            SystemSessionInformation,
            SystemRangeStartInformation,
            SystemVerifierInformation,
            SystemVerifierThunkExtend,
            SystemSessionProcessInformation,
            SystemLoadGdiDriverInSystemSpace,
            SystemNumaProcessorMap,
            SystemPrefetcherInformation,
            SystemExtendedProcessInformation,
            SystemRecommendedSharedDataAlignment,
            SystemComPlusPackage,
            SystemNumaAvailableMemory, // 60
            SystemProcessorPowerInformation,
            SystemEmulationBasicInformation,
            SystemEmulationProcessorInformation,
            SystemExtendedHandleInformation,
            SystemLostDelayedWriteInformation,
            SystemBigPoolInformation,
            SystemSessionPoolTagInformation,
            SystemSessionMappedViewInformation,
            SystemHotpatchInformation,
            SystemObjectSecurityMode, // 70
            SystemWatchdogTimerHandler, // doesn't seem to be implemented
            SystemWatchdogTimerInformation,
            SystemLogicalProcessorInformation,
            SystemWow64SharedInformation,
            SystemRegisterFirmwareTableInformationHandler,
            SystemFirmwareTableInformation,
            SystemModuleInformationEx,
            SystemVerifierTriageInformation,
            SystemSuperfetchInformation,
            SystemMemoryListInformation, // 80
            SystemFileCacheInformationEx,
            SystemNotImplemented19,
            SystemProcessorDebugInformation,
            SystemVerifierInformation2,
            SystemNotImplemented20,
            SystemRefTraceInformation,
            SystemSpecialPoolTag, // MmSpecialPoolTag, then MmSpecialPoolCatchOverruns != 0
            SystemProcessImageName,
            SystemNotImplemented21,
            SystemBootEnvironmentInformation, // 90
            SystemEnlightenmentInformation,
            SystemVerifierInformationEx,
            SystemNotImplemented22,
            SystemNotImplemented23,
            SystemCovInformation,
            SystemNotImplemented24,
            SystemNotImplemented25,
            SystemPartitionInformation,
            SystemSystemDiskInformation, // this and SystemPartitionInformation both call IoQuerySystemDeviceName
            SystemPerformanceDistributionInformation, // 100
            SystemNumaProximityNodeInformation,
            SystemTimeZoneInformation2,
            SystemCodeIntegrityInformation,
            SystemNotImplemented26,
            SystemUnknownInformation, // No symbols for this case, very strange...
            SystemVaInformation // 106, calls MmQuerySystemVaInformation
        }

поправка)
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.