title: using unsafe code in C# asp.net
body: <p>trying to debug a modified code to be using <code>unsafe</code> generates an error 
  i dont know if my code is correct with the astrics (so please check if i put those correctly) though there is another issue apart from that... with compiler options in <strong>asp.net</strong> 
  as opposed to Windows apps, there's no sutible place to check/uncheck the option to using unsafe </p>
  
  <p>and as far as i could get to info about that envolves dealing with the Web.Config "manually"</p>
  
  <p>so i was doing that , added a code to <code>Web.Config</code>, 
  there are two versions of users suggesting to put code within configuration section or within the same scope of <code>debug=true</code> ... </p>
  
  <p>so i put it in both(not at same time , but tried both (:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!--
    For more information on how to configure your ASP.NET application, please visit
    http://go.microsoft.com/fwlink/?LinkId=169433
    --&gt;
  &lt;configuration&gt;
   &lt;system.codedom&gt;
     &lt;compilers&gt;
       &lt;compiler language="C#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /&gt;
     &lt;/compilers&gt;
    &lt;/system.codedom&gt;
    &lt;connectionStrings&gt;....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);
  </code></pre>
  
  <p>this is the calss that implements native pinvok codes</p>
  
  <p>(at the beginning i was trying to test native vs managed / .net approach )
  but before even trying the pinvoke vs .net i dived strait to check on 
  performance btween pinvoke <strong>*unsafe</strong> vs .net </p>
  
  <p>so this part is still in "safe mode" 
  should it be also treated with the astrics  ? </p>
  
  <pre><code>static class Nat
      {
          public static class IO
          {
              public static Dictionary&lt;string, ulong&gt; GetALLIO(Process procToRtrivIO)
              {
                  IO_COUNTERS counters;
                  Dictionary&lt;string, ulong&gt; retCountIoDict = new Dictionary&lt;string, ulong&gt;();
                  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&lt;string, uint&gt; GetAllMem(Process procToRtrivMem)
              {
  
                  PROCESS_MEMORY_COUNTERS MemCounters;
                  Dictionary&lt;string, uint&gt; retCountMemDict = new Dictionary&lt;string, uint&gt;();
                  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.";
  
              }
          }
  
      }
  </code></pre>
  
  <p>sorry for that, but the update is i missed the part of :
  compiler option</p>
  
  <pre><code>compilerOptions="/unsafe" // &lt;&lt;-- you can add this to the compiler config line.
  </code></pre>
  
  <p>though still . my problems only started then cause there is not only one error :</p>
  
  <pre><code>Unsafe code may only appear if compiling with /unsafe 
  </code></pre>
  
  <p>but errors all over the place ! </p>
  
  <p>first one for instance : </p>
  
  <pre><code>            public static Dictionary&lt;string, ulong&gt; GetALLIO(Process procToRtrivIO)
              {
                  IO_COUNTERS counters;
                  Dictionary&lt;string, ulong&gt; retCountIoDict = new Dictionary&lt;string, ulong&gt;();
  
              ---&gt;&gt;   GetProcessIoCounters(System.Diagnostics.Process.GetCurrentProcess().Handle, out counters);
  
  
                  retCountIoDict.Add("ReadOperationCount", counters.ReadOperationCount);
                  retCountIoDict.Add("WriteOperationCount", counters.WriteOperationCount);
  </code></pre>
  
  <p>the error is for that line calling GetProcesIoCounters()</p>
  
  <p>Error   3   Argument 1: cannot convert from 'System.IntPtr' to 'System.IntPtr*' g:\RobDevI5-Raid-0\Documents\Visual Studio 2010\WebSites\WebSite2\App_Code\CsExtensions.cs  416 42  g:...\WebSite2\</p>
  
  <p>UPDAT - Code That seem to work though i could not verify it is using unsafe properly</p>
  
  <p>signeture with unsafe</p>
  
  <pre><code>    [DllImport("psapi.dll", SetLastError = true)]
      unsafe static extern bool GetProcessMemoryInfo(IntPtr* hProcess, out PROCESS_MEMORY_COUNTERS Memcounters, int size);
  </code></pre>
  
  <p>code with my "unsafe"</p>
  
  <pre><code>        public static class IO
          {
              public static unsafe Dictionary&lt;string, ulong&gt; GetALLIO(Process procToRtrivIO)
              {
                  IO_COUNTERS counters;
                  Dictionary&lt;string, ulong&gt; retCountIoDict = new Dictionary&lt;string, ulong&gt;();
                  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.";
  
              }
          }
  </code></pre>