I'm trying to get the current system file cache size as shown below. However when I 开发者_JAVA百科run this code nothing gets returned, can anybody see where I'm going wrong ? FYI the link is GetSystemFileCache.
[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);
}
bool b = GetSystemFileCacheSize(ref lpMinimumFileCacheSize, ref lpMaximumFileCacheSize, ref lpFlags);
Console.WriteLine(lpMinimumFileCacheSize);
Console.WriteLine(lpMaximumFileCacheSize);
Works fine for me.
Output:
1048576
2143289344
Windows 7 Pro x32
This code snippet works: (Windows Seven x86)
Because of the problem we are encountering relating to MS KB 976618 and due to not wanting to run binary code from unknown 3rd parties on production servers I'm being forced in to the c# language. Because I'm a newb at c# it took me a while but I finally figured out the extra bits around the code snippets people have been posting. So my full ms visual studio c# console program that compiles and runs is:
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);
}
}
and on my Windows 7 x64 PC it outputs
True 1048576 1099511627776 0
and no, that's not our production server.
and then in powershell this is
$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"
Next step: SetSystemFileCacheSize, I've written a powershell script for this and put it on https://serverfault.com/questions/325277/windows-server-2008-r2-metafile-ram-usage/527466#527466
精彩评论