gpt4 book ai didi

c# - 你能安全地说这个代码是 "unsafe"

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:24 26 4
gpt4 key购买 nike

这是实现原生pinvok代码的类

虽然我无法验证它是否正确使用了 unsafe,但它似乎可以工作

不安全的签名

    struct IO_COUNTERS
{
public ulong ReadOperationCount;
public ulong WriteOperationCount;
public ulong OtherOperationCount;
public ulong ReadTransferCount;
public ulong WriteTransferCount;
public ulong OtherTransferCount;
}
[DllImport("kernel32.dll")]
unsafe static extern bool GetProcessIoCounters(IntPtr* ProcessHandle, out IO_COUNTERS IoCounters);

用我的“不安全”代码

        public static unsafe class IO
{
public static Dictionary<string, ulong> GetALLIO(Process procToRtrivIO)
{
IO_COUNTERS counters;
Dictionary<string, ulong> retCountIoDict = new Dictionary<string, ulong>();
GetProcessIoCounters((IntPtr*)System.Diagnostics.Process.GetCurrentProcess().Handle, out counters);
retCountIoDict.Add("ReadOperationCount", counters.ReadOperationCount);
retCountIoDict.Add("WriteOperationCount", counters.WriteOperationCount);
retCountIoDict.Add("OtherOperationCount", counters.OtherOperationCount);
retCountIoDict.Add("ReadTransferCount", counters.ReadTransferCount);
retCountIoDict.Add("WriteTransferCount", counters.WriteTransferCount);
retCountIoDict.Add("OtherTransferCount", counters.OtherTransferCount);
return retCountIoDict;
//return "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
// " Mb of data.";

}
}

现在下一段代码是在我的另一个问题中提出的(就在我设法编译和运行上面的代码之前,...因为我遇到了一些问题)

所以就在我通过反复试验解决了我的问题后,有人发布了这个部分解决方案,它是部分解决方案,因为它不包括两者structsigneture,所以我问它是否正在测试,因为它编译但不运行。我的意思是当它运行时(没有编译错误)但在运行时,错误是

An exception of type 'System.NullReferenceException' occurred in App_Code.1ri3mog1.dll but was not handled in user code

附加信息:对象引用未设置到对象的实例。

所以这是我从上一个问题中得到的代码

Ok you need to change a few things, shown below

public static class IO
{
unsafe public static Dictionary<string, ulong> GetALLIO(Process procToRtrivIO)
{
IO_COUNTERS* counters;
Dictionary<string, ulong> retCountIoDict = new Dictionary<string, ulong>();
IntPtr ptr = System.Diagnostics.Process.GetCurrentProcess().Handle;

GetProcessIoCounters(&ptr, out counters);
retCountIoDict.Add("ReadOperationCount", counters->ReadOperationCount);
retCountIoDict.Add("WriteOperationCount", counters->WriteOperationCount);
retCountIoDict.Add("OtherOperationCount", counters->OtherOperationCount);
retCountIoDict.Add("ReadTransferCount", counters->ReadTransferCount);
retCountIoDict.Add("WriteTransferCount", counters->WriteTransferCount);
retCountIoDict.Add("OtherTransferCount", counters->OtherTransferCount);
return retCountIoDict;
//return "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
// " Mb of data.";

}
}

所以问题是,我真的成功了吗...并安全地使用了不安全的代码(最上面的代码块)或者我错过了一些东西,如果我的代码没问题(我仍然怀疑它没有完全使用不安全的装置),那么建议的代码版本有什么问题?

最佳答案

您的代码严重损坏。您对 GetProcessIoCounters 的声明是错误的。您正在传递进程句柄的地址,但您需要按值传递进程句柄。更重要的是,这里根本不需要 unsafe 代码。我建议您停止使用 unsafe

下面是声明 GetProcessIoCounters 的方式:

[DllImport(@"kernel32.dll", SetLastError=true)]
static extern bool GetProcessIoCounters(
IntPtr hProcess,
out IO_COUNTERS IoCounters
);

IO_COUNTERS 的声明没问题。

要调用该函数,您只需执行以下操作:

IO_COUNTERS IoCounters;
if (!GetProcessIoCounters(Process.GetCurrentProcess().Handle, out IoCounters))
throw new Win32Exception();

关于c# - 你能安全地说这个代码是 "unsafe",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17212159/

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