你能否安全地说这段代码“不安全”

时间:2013-06-20 11:13:41

标签: c# pinvoke

这是实现本机pinvok代码的calss

代码似乎有效但我无法验证它是否正确使用不安全

不安全的signeture

    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.";

    }
}

所以问题是,我是否真的做到了......并安全地使用了一个不安全的代码(顶部的代码块) 或者我错过了什么,如果我的代码没问题(我仍然怀疑它没有完全使用不安全的装备),建议的代码版本有什么问题?

1 个答案:

答案 0 :(得分:2)

你的代码严重受损。您对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();
相关问题