尝试调试要使用的修改代码会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.";
}
}