2011-05-05 28 views
8

Tôi đang cố gắng lấy kích thước bộ nhớ cache của tệp hệ thống hiện tại như được hiển thị bên dưới. Tuy nhiên khi tôi chạy mã này không có gì được trả lại, bất cứ ai có thể nhìn thấy nơi tôi đang đi sai? FYI liên kết là GetSystemFileCache.C# lấy kích thước bộ nhớ cache của tệp hệ thống

[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] 
    public static extern bool GetSystemFileCacheSize(
     ref IntPtr lpMinimumFileCacheSize, 
     ref IntPtr lpMaximumFileCacheSize, 
     ref IntPtr lpFlags 
     ); 

    static void Main(string[] args) 
    {    
     IntPtr lpMinimumFileCacheSize = IntPtr.Zero; 
     IntPtr lpMaximumFileCacheSize = IntPtr.Zero; 
     IntPtr lpFlags = IntPtr.Zero; 

     bool b = GetSystemFileCacheSize(ref lpMinimumFileCacheSize, ref lpMaximumFileCacheSize, ref lpFlags); 
    } 
+4

Tôi ngạc nhiên không có một ví dụ về http://pinvoke.net cho chức năng đó. (vào menu phụ Kernel32.dll). Nếu bạn nhận được nó làm việc, xin vui lòng thêm một ví dụ cho trang web đó (nó là một wiki). –

+0

Bạn đã thử gọi GetLastWin32Error sau đó để nhận lỗi? –

+0

xin lỗi - Tôi là một dope hoàn chỉnh, tôi đã có nền tảng đích vẫn được đặt thành X86. Bây giờ tôi cảm thấy xấu hổ. Sau khi thiết lập cho bất kỳ CPU nó hoạt động tốt. – Johnv2020

Trả lời

5
 bool b = GetSystemFileCacheSize(ref lpMinimumFileCacheSize, ref lpMaximumFileCacheSize, ref lpFlags); 

     Console.WriteLine(lpMinimumFileCacheSize); 
     Console.WriteLine(lpMaximumFileCacheSize); 

trình tốt cho tôi.

Output:

1048576 
2143289344 

Windows 7 Pro x32

+0

a) Những con số nào (xấp xỉ)? và b) 32 | 64 bit OS? –

+0

OK, tôi đã nhận 0 0 trên Win7 x64. Và b == false –

+0

@Henk, tôi có x64 giành chiến thắng 7, sau đó tôi sẽ thử đoạn mã của bạn trên đó. –

2

Đoạn mã này hoạt động: (Windows Seven x86) enter image description here

2

Bởi vì vấn đề chúng ta đang gặp phải liên quan đến MS KB 976618 và do không muốn chạy mã nhị phân từ các bên thứ ba không xác định trên các máy chủ sản xuất, tôi bị buộc phải sử dụng ngôn ngữ C#. Bởi vì tôi là một newb tại C# nó đã cho tôi một thời gian nhưng cuối cùng tôi đã tìm ra các bit phụ xung quanh đoạn mã người đã được đăng. Vì vậy, đầy đủ ms visual studio C# console chương trình của tôi rằng biên dịch và chạy là:

using System; 
using System.Runtime.InteropServices; 

class Program 
{ 
    [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] 
    public static extern bool GetSystemFileCacheSize(
     ref IntPtr lpMinimumFileCacheSize, 
     ref IntPtr lpMaximumFileCacheSize, 
     ref IntPtr lpFlags 
     ); 

    static void Main(string[] args) 
    { 
     IntPtr lpMinimumFileCacheSize = IntPtr.Zero; 
     IntPtr lpMaximumFileCacheSize = IntPtr.Zero; 
     IntPtr lpFlags = IntPtr.Zero; 

     bool b = GetSystemFileCacheSize(ref lpMinimumFileCacheSize, ref lpMaximumFileCacheSize, ref lpFlags); 

     Console.WriteLine(b); 
     Console.WriteLine(lpMinimumFileCacheSize); 
     Console.WriteLine(lpMaximumFileCacheSize); 
     Console.WriteLine(lpFlags); 
    } 
} 

và trên Windows 7 x64 máy tính của tôi nó ra

 
True 
1048576 
1099511627776 
0 

và không, đó không phải là máy chủ sản xuất của chúng tôi.

và sau đó trong PowerShell đây là

$source = @" 
using System; 
using System.Runtime.InteropServices; 

namespace MyTools 
{ 
    public static class cache 
    { 

     [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] 
     public static extern bool GetSystemFileCacheSize(
      ref IntPtr lpMinimumFileCacheSize, 
      ref IntPtr lpMaximumFileCacheSize, 
      ref IntPtr lpFlags 
      ); 

     public static bool Get(ref IntPtr a, ref IntPtr c, ref IntPtr d) 
     { 
      IntPtr lpMinimumFileCacheSize = IntPtr.Zero; 
      IntPtr lpMaximumFileCacheSize = IntPtr.Zero; 
      IntPtr lpFlags = IntPtr.Zero; 

      bool b = GetSystemFileCacheSize(ref lpMinimumFileCacheSize, ref lpMaximumFileCacheSize, ref lpFlags); 

      a = lpMinimumFileCacheSize; 
      c = lpMaximumFileCacheSize; 
      d = lpFlags; 
      return b; 
     } 
    } 
} 
"@ 

Add-Type -TypeDefinition $source -Language CSharp 

# Init variables 
$SFCMin = 0 
$SFCMax = 0 
$SFCFlags = 0 
$b = [MyTools.cache]::Get([ref]$SFCMin, [ref]$SFCMax, [ref]$SFCFlags) 

#typecast values so we can do some math with them 
$SFCMin = [long]$SFCMin 
$SFCMax = [long]$SFCMax 
$SFCFlags = [long]$SFCFlags 

write-output "Return values from GetSystemFileCacheSize are: " 
write-output "Function Result : $b" 
write-output "   Min : $SFCMin" 
write-output ("   Max : $SFCMax (" + $SFCMax/1024/1024/1024 + " GiB)") 
write-output "   Flags : $SFCFlags" 

Bước tiếp theo: SetSystemFileCacheSize, tôi đã viết một kịch bản PowerShell cho điều này và đặt nó trên https://serverfault.com/questions/325277/windows-server-2008-r2-metafile-ram-usage/527466#527466

Các vấn đề liên quan