2

尝试调试要使用的修改代码会unsafe产生一个错误,我不知道我的代码在 astrics 上是否正确(所以请检查我是否正确放置了这些代码),尽管除此之外还有另一个问题......使用asp 中的编译器选项。 net 与 Windows 应用程序相反,没有合适的地方来检查/取消选中使用不安全的选项

并且据我所知,这涉及“手动”处理 Web.Config

所以我这样做了,添加了一个代码Web.Config,有两个版本的用户建议将代码放在配置部分或debug=true...的同一范围内

所以我把它放在两个(不是同时,但都试过了(:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
 <system.codedom>
   <compilers>
     <compiler language="C#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   </compilers>
  </system.codedom>
  <connectionStrings>....my secret connection here...
   then rest of configs..
     .....




    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);


    private struct PROCESS_MEMORY_COUNTERS
    {
        public uint cb;
        public uint PageFaultCount;
        public uint PeakWorkingSetSize;
        public uint WorkingSetSize;
        public uint QuotaPeakPagedPoolUsage;
        public uint QuotaPagedPoolUsage;
        public uint QuotaPeakNonPagedPoolUsage;
        public uint QuotaNonPagedPoolUsage;
        public uint PagefileUsage;
        public uint PeakPagefileUsage;
    }
    [StructLayout(LayoutKind.Sequential, Size = 40)]
    [DllImport("psapi.dll", SetLastError = true)]
    unsafe static extern bool GetProcessMemoryInfo(IntPtr* hProcess, out PROCESS_MEMORY_COUNTERS* Memcounters, int size);

这是实现本机 pinvok 代码的类

(一开始我试图测试本机与托管/.net 方法)但在尝试 pinvoke 与 .net 之前,我潜入了困境以检查 pinvoke *unsafe与 .net之间的性能

所以这部分仍处于“安全模式”是否也应该用 astrics 处理?

static class Nat
    {
        public static class IO
        {
            public static Dictionary<string, ulong> GetALLIO(Process procToRtrivIO)
            {
                IO_COUNTERS counters;
                Dictionary<string, ulong> retCountIoDict = new Dictionary<string, ulong>();
                GetProcessIoCounters(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.";

            }
        }
        public static class Mem
        {
            public static Dictionary<string, uint> GetAllMem(Process procToRtrivMem)
            {

                PROCESS_MEMORY_COUNTERS MemCounters;
                Dictionary<string, uint> retCountMemDict = new Dictionary<string, uint>();
                GetProcessMemoryInfo(System.Diagnostics.Process.GetCurrentProcess().Handle, out MemCounters, Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS))); //MemCounters.cb);
                retCountMemDict.Add("cb", MemCounters.cb);
                retCountMemDict.Add("PageFaultCount", MemCounters.PageFaultCount);
                retCountMemDict.Add("PeakWorkingSetSize", MemCounters.PeakWorkingSetSize);
                retCountMemDict.Add("WorkingSetSize", MemCounters.WorkingSetSize);
                retCountMemDict.Add("QuotaPeakPagedPoolUsage", MemCounters.QuotaPeakPagedPoolUsage);
                retCountMemDict.Add("QuotaPagedPoolUsage", MemCounters.QuotaPagedPoolUsage);

                retCountMemDict.Add("QuotaPeakNonPagedPoolUsage", MemCounters.QuotaPeakNonPagedPoolUsage);
                retCountMemDict.Add("QuotaNonPagedPoolUsage", MemCounters.QuotaNonPagedPoolUsage);
                retCountMemDict.Add("PagefileUsage", MemCounters.PagefileUsage);
                retCountMemDict.Add("PeakPagefileUsage", MemCounters.PeakPagefileUsage);

                return retCountMemDict;
                //return  "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
                //    " Mb of data.";

            }
        }

    }

对此感到抱歉,但更新是我错过了以下部分:编译器选项

compilerOptions="/unsafe" // <<-- you can add this to the compiler config line.

虽然还是。我的问题才开始,然后导致不仅有一个错误:

Unsafe code may only appear if compiling with /unsafe 

但是到处都是错误!

例如第一个:

            public static Dictionary<string, ulong> GetALLIO(Process procToRtrivIO)
            {
                IO_COUNTERS counters;
                Dictionary<string, ulong> retCountIoDict = new Dictionary<string, ulong>();

            --->>   GetProcessIoCounters(System.Diagnostics.Process.GetCurrentProcess().Handle, out counters);


                retCountIoDict.Add("ReadOperationCount", counters.ReadOperationCount);
                retCountIoDict.Add("WriteOperationCount", counters.WriteOperationCount);

错误是针对调用 GetProcesIoCounters() 的那一行

错误 3 参数 1:无法从 'System.IntPtr' 转换为 'System.IntPtr*' g:\RobDevI5-Raid-0\Documents\Visual Studio 2010\WebSites\WebSite2\App_Code\CsExtensions.cs 416 42 g:.. .\网站2\

UPDAT - 尽管我无法验证它是否正确使用不安全的代码,但它似乎有效

不安全的签名

    [DllImport("psapi.dll", SetLastError = true)]
    unsafe static extern bool GetProcessMemoryInfo(IntPtr* hProcess, out PROCESS_MEMORY_COUNTERS Memcounters, int size);

我的“不安全”代码

        public static class IO
        {
            public static unsafe Dictionary<string, ulong> GetALLIO(Process procToRtrivIO)
            {
                IO_COUNTERS counters;
                Dictionary<string, ulong> retCountIoDict = new Dictionary<string, ulong>();
                IntPtr* Hw = (IntPtr*)System.Diagnostics.Process.GetCurrentProcess().Handle;
                GetProcessIoCounters(Hw, 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.";

            }
        }
4

4 回答 4

3

死灵术。
接受的答案包含不起作用的代码。
这是有效的更正版本:

namespace MemoryInfo
{


    class Program
    {

        static void Main(string[] args)
        {
            System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();
            Nat.Mem.GetAllMem(proc);
            Nat.IO.GetALLIO(proc);
        }
    }



    // http://www.pinvoke.net/default.aspx/psapi.getprocessmemoryinfo
    static class Nat
    {


        // [DllImport("kernel32.dll")]
        // public static extern IntPtr GetCurrentProcess();

        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
        private struct IO_COUNTERS
        {
            public ulong ReadOperationCount;
            public ulong WriteOperationCount;
            public ulong OtherOperationCount;
            public ulong ReadTransferCount;
            public ulong WriteTransferCount;
            public ulong OtherTransferCount;
        }


        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Size = 40)]
        private struct PROCESS_MEMORY_COUNTERS
        {
            public uint cb; // The size of the structure, in bytes (DWORD).
            public uint PageFaultCount; // The number of page faults (DWORD).
            public uint PeakWorkingSetSize; // The peak working set size, in bytes (SIZE_T).
            public uint WorkingSetSize; // The current working set size, in bytes (SIZE_T).
            public uint QuotaPeakPagedPoolUsage; // The peak paged pool usage, in bytes (SIZE_T).
            public uint QuotaPagedPoolUsage; // The current paged pool usage, in bytes (SIZE_T).
            public uint QuotaPeakNonPagedPoolUsage; // The peak nonpaged pool usage, in bytes (SIZE_T).
            public uint QuotaNonPagedPoolUsage; // The current nonpaged pool usage, in bytes (SIZE_T).
            public uint PagefileUsage; // The Commit Charge value in bytes for this process (SIZE_T). Commit Charge is the total amount of memory that the memory manager has committed for a running process.
            public uint PeakPagefileUsage; // The peak value in bytes of the Commit Charge during the lifetime of this process (SIZE_T).
        }


        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private unsafe static extern bool GetProcessIoCounters(System.IntPtr ProcessHandle, out IO_COUNTERS IoCounters);

        [System.Runtime.InteropServices.DllImport("psapi.dll", SetLastError = true)]
        private unsafe static extern bool GetProcessMemoryInfo(System.IntPtr hProcess, out PROCESS_MEMORY_COUNTERS counters, uint size);


        public static class IO
        {
            unsafe public static System.Collections.Generic.Dictionary<string, ulong> GetALLIO(System.Diagnostics.Process procToRtrivIO)
            {
                IO_COUNTERS counters;
                System.Collections.Generic.Dictionary<string, ulong> retCountIoDict = 
                    new System.Collections.Generic.Dictionary<string, ulong>();
                System.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.";

            }
        } // End Class IO 


        public static class Mem
        {
            unsafe public static System.Collections.Generic.Dictionary<string, uint> GetAllMem(System.Diagnostics.Process procToRtrivMem)
            {
                PROCESS_MEMORY_COUNTERS MemCounters;
                System.Collections.Generic.Dictionary<string, uint> retCountMemDict = 
                    new System.Collections.Generic.Dictionary<string, uint>();
                System.IntPtr ptr = System.Diagnostics.Process.GetCurrentProcess().Handle;
                uint nativeStructSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS));


                GetProcessMemoryInfo(ptr, out MemCounters, nativeStructSize); //MemCounters.cb);
                retCountMemDict.Add("cb", MemCounters.cb);
                retCountMemDict.Add("PageFaultCount", MemCounters.PageFaultCount);
                retCountMemDict.Add("PeakWorkingSetSize", MemCounters.PeakWorkingSetSize);
                retCountMemDict.Add("WorkingSetSize", MemCounters.WorkingSetSize);
                retCountMemDict.Add("QuotaPeakPagedPoolUsage", MemCounters.QuotaPeakPagedPoolUsage);
                retCountMemDict.Add("QuotaPagedPoolUsage", MemCounters.QuotaPagedPoolUsage);

                retCountMemDict.Add("QuotaPeakNonPagedPoolUsage", MemCounters.QuotaPeakNonPagedPoolUsage);
                retCountMemDict.Add("QuotaNonPagedPoolUsage", MemCounters.QuotaNonPagedPoolUsage);
                retCountMemDict.Add("PagefileUsage", MemCounters.PagefileUsage);
                retCountMemDict.Add("PeakPagefileUsage", MemCounters.PeakPagefileUsage);

                return retCountMemDict;
                //return  "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
                //    " Mb of data.";
            }


        } // End Class Mem

    }




}
于 2015-10-28T09:03:35.867 回答
1

我认为您的网络配置行缺少 compilerOptions="/unsafe+" 部分

例如

        <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" compilerOptions="/unsafe+" warningLevel="4" />
    </compilers>

于 2013-06-20T07:25:07.847 回答
1

听起来您需要了解星号代表什么。在 C# 中,与其他类 C 语言一样,它表示指针类型。例如:

int *

...表示“指向 int 的指针”。也就是说,这个变量的值表示一个包含 int 的内存位置。C# 和其他 .NET 语言实现了一种称为类型安全的东西,这意味着很难误解内存中的值(例如,尝试在实际包含 int 的内存位置读取字符串),这从广义上讲是有用的事物。但是,有时,当您与本机代码交互时,能够根据指针进行处理很有用。这颠覆了类型安全机制,因此您的代码必须被标记unsafe,并且您需要使用/unsafe前面提到的开关进行编译。

您似乎暗示您在 p/invoke 和使用不安全代码之间做出了非此即彼的决定。以我的经验,这种情况很少出现,因为存在两种技术来解决不同的问题。P/invoke 允许您以通常不需要您使用不安全代码的方式与本机代码进行交互,但实际上它取决于您正在与之交互的本机代码的性质。

当你问你的“安全模式”代码是否“也应该用 astrics 处理?” 我希望我的解释告诉您,您不使用星号处理代码,而是根据具体情况根据需要声明变量。

此外,您的 p/invoke 声明GetProcessIoCounters看起来错误。您很少看到类型为 的参数IntPtr *。查看http://pinvoke.net以查找正确的声明。

为了澄清一些更多的术语,代码可以是托管的或非托管的。C# 代码始终是托管的。非托管代码可以用多种语言中的一种编写,例如 C、C++ 等。C# 代码虽然始终是托管的,但可以是类型安全的(默认设置),或者您可以将 C# 代码块标记为不安全,这样您就可以使用指针类型。

于 2013-06-20T07:42:10.450 回答
1

好的,您需要更改一些内容,如下所示

using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
static class Nat
{
    [StructLayout(LayoutKind.Sequential]
    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);

    [StructLayout(LayoutKind.Sequential, Size = 40)]
    private struct PROCESS_MEMORY_COUNTERS
    {
        public uint cb;
        public uint PageFaultCount;
        public uint PeakWorkingSetSize;
        public uint WorkingSetSize;
        public uint QuotaPeakPagedPoolUsage;
        public uint QuotaPagedPoolUsage;
        public uint QuotaPeakNonPagedPoolUsage;
        public uint QuotaNonPagedPoolUsage;
        public uint PagefileUsage;
        public uint PeakPagefileUsage;
    }

    [DllImport("psapi.dll", SetLastError = true)]
    unsafe static extern bool GetProcessMemoryInfo(IntPtr* hProcess, out PROCESS_MEMORY_COUNTERS* Memcounters, int size);

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

        }
    }
    public static class Mem
    {
        unsafe public static Dictionary<string, uint> GetAllMem(Process procToRtrivMem)
        {

            PROCESS_MEMORY_COUNTERS* MemCounters;
            Dictionary<string, uint> retCountMemDict = new Dictionary<string, uint>();
            IntPtr ptr = System.Diagnostics.Process.GetCurrentProcess().Handle;

            GetProcessMemoryInfo(&ptr, out MemCounters, Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS))); //MemCounters.cb);
            retCountMemDict.Add("cb", MemCounters->cb);
            retCountMemDict.Add("PageFaultCount", MemCounters->PageFaultCount);
            retCountMemDict.Add("PeakWorkingSetSize", MemCounters->PeakWorkingSetSize);
            retCountMemDict.Add("WorkingSetSize", MemCounters->WorkingSetSize);
            retCountMemDict.Add("QuotaPeakPagedPoolUsage", MemCounters->QuotaPeakPagedPoolUsage);
            retCountMemDict.Add("QuotaPagedPoolUsage", MemCounters->QuotaPagedPoolUsage);

            retCountMemDict.Add("QuotaPeakNonPagedPoolUsage", MemCounters->QuotaPeakNonPagedPoolUsage);
            retCountMemDict.Add("QuotaNonPagedPoolUsage", MemCounters->QuotaNonPagedPoolUsage);
            retCountMemDict.Add("PagefileUsage", MemCounters->PagefileUsage);
            retCountMemDict.Add("PeakPagefileUsage", MemCounters->PeakPagefileUsage);

            return retCountMemDict;
            //return  "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
            //    " Mb of data.";

        }
    }

}
于 2013-06-20T08:09:16.013 回答