gpt4 book ai didi

c# - 结构数组指针的问题

转载 作者:行者123 更新时间:2023-11-30 23:08:32 25 4
gpt4 key购买 nike

我想通过使用 ntdll 中的 NtQuerySystemInformation 获取有关每个处理器性能的信息。现在我遇到的问题是它只运行了所有 5 次尝试然后返回。

NtQuerySystemInformation 返回一个 NtStatus,它始终是 InfoLengthMismatch,这通常意味着我必须使我的缓冲区更大。如果我只使用一个处理器(无数组)和 0x10000 的缓冲区大小来尝试它,它工作得很好,它在第一次尝试时给了我 InfoLengthMismatch,但第二次尝试总是有效。

我尝试将缓冲区增加 100 倍,也尝试增加 1000 倍,但没有任何效果。

private _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[] GetPerformanceInfo()
{
try
{
//Getting count of processors
SYSTEM_INFO sysInfo = new SYSTEM_INFO();
GetSystemInfo(out sysInfo);
int processors = (int)sysInfo.numberOfProcessors;

int tries = 0;

while (true)
{

//buffer size
uint infoLength = (uint)(Marshal.SizeOf(typeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)) * processors);

//buffer
IntPtr infoPtr = Marshal.AllocHGlobal((int)infoLength);

//Problem: result is always InfoLengthMismatch
NtStatus result = NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemProcessorPerformanceInformation, infoPtr, infoLength, out infoLength);

//if success, get the array and return it
if (result == NtStatus.Success)
{
int szStruct = Marshal.SizeOf(typeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION));
_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[] localStructs = new _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[processors];

for (uint i = 0; i < processors; i++)
localStructs[i] = (_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)Marshal.PtrToStructure(new IntPtr(infoPtr.ToInt64() + (szStruct * processors)), typeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION));

return localStructs;
}

//free ptr when fail
Marshal.FreeHGlobal(infoPtr);

if (++tries > 5)
return null;

//set ptr again, new try
infoPtr = Marshal.AllocHGlobal((int)infoLength);

}
}
catch (Exception e)
{
Console.WriteLine(e.Source + ": " + e.Message.ToString());
return null;
}
}

}

//struct of a large int
[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct LARGE_INTEGER
{
[FieldOffset(0)] public Int64 QuadPart;
[FieldOffset(0)] public UInt32 LowPart;
[FieldOffset(4)] public Int32 HighPart;
}

//struct
public struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
{
public LARGE_INTEGER IdleTime;
public LARGE_INTEGER KernelTime;
public LARGE_INTEGER UserTime;
public LARGE_INTEGER Reserved1;
public ulong Reserved2;
}

编辑:这一行有误:

uint infoLength = (uint)(Marshal.SizeOf(typeof(_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)) * processors);

通常如果大小错误,它会将正确的大小输出到 infoLength var,但在这种情况下,我每次都在循环开始时设置它。

最佳答案

我注意到了一些事情。首先,您分配了两次内存,并且只释放了一次:

//buffer
IntPtr infoPtr = Marshal.AllocHGlobal((int)infoLength);

//...

//free ptr when fail
Marshal.FreeHGlobal(infoPtr);

//...

//set ptr again, new try
infoPtr = Marshal.AllocHGlobal((int)infoLength);

其次,当NtQuerySystemInformation 返回错误时,它会将可选的输出参数设置为所需的大小。与我看到的不同,您创建的缓冲区大小是 8 * 5 * processors。或 40 * 处理器

您可以通过在调用前后输出 infoLength 来确认这一点以及所需的适当大小。

// Should be 40 * processors
Console.WriteLine((int)infoLength);

//Problem: result is always InfoLengthMismatch
NtStatus result = NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemProcessorPerformanceInformation,
infoPtr, infoLength, out infoLength);

// Will be bigger than 40 * processors
Console.WriteLine((int)infoLength);

关于c# - 结构数组指针的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46475090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com