2

使用する変更されたコードをデバッグしようとするとunsafe、自分のコードが astrics で正しいかどうかわからないエラーが生成されます (したがって、それらを正しく配置しているかどうかを確認してください) 。 Windowsアプリ とは対照的に、安全でないオプションを使用するオプションをチェック/チェック解除する適切な場所はありません。

そして、それに関する情報を得ることができる限り、Web.Configを「手動で」処理する必要があります

だから私はそれをやっていて、にコードを追加しましたWeb.Config.2つのバージョンのユーザーが、コードを構成セクション内または同じ範囲内に配置することを提案しています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 コードを実装する calss です。

(最初は、ネイティブ vs マネージド / .net アプローチをテストしようとしていました)しかし、pinvoke vs .net を試す前に、pinvoke *unsafe vs .net間のパフォーマンスをチェックするために海峡に潜りました

したがって、この部分はまだ「セーフモード」にあり、アトリクスでも処理する必要がありますか?

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.

まだです。私の問題は始まったばかりで、エラーは1つだけではありません:

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:.. .\WebSite2\

UPDAT - unsafe を適切に使用していることを確認できませんでしたが、機能しているように見えるコード

安全でない署名

    [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

1

Web 構成行に 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 と安全でないコードの使用の間でどちらかまたは両方の決定があったことを示唆しているようです。私の経験では、異なる問題を解決するために 2 つの手法が存在するため、これが発生することはめったにありません。P/invoke を使用すると、通常はアンセーフ コードを使用する必要がない方法でネイティブ コードを操作できますが、実際には、操作するネイティブ コードの性質によって異なります。

「セーフモード」コードを「アトリクスでも扱うべきか」と尋ねると、私の説明で、コードをアスタリスクで処理するのではなく、必要に応じてケースバイケースで変数を宣言することが示されていることを願っています。

さらに、 p/invoke の宣言GetProcessIoCountersが間違っているように見えます。type のパラメーターはめったに見られません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 に答える