カメヲラボ

主にプログラミングとお勉強全般について書いてます

俺用Tips C#(4)

なんだか複雑になってきたので、すべてC#で書くことにした。まず、APIを叩く時のリファレンス。
http://blog.joycode.com/ghj/archive/2004/05/08/20891.aspx
C#で調べていると結構中国語のサイトがでてくるのだけど、中国人はC#が好きなのか?

あと、メモリ測定に必要なAPI
以下コードベタ張り

        [DllImport("kernel32.dll")]
        private static extern uint GetCurrentThreadId();
        private delegate int EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpProcessId);
        [DllImport("user32.dll")]
        private static extern int GetClassName(IntPtr hWnd, [Out]StringBuilder lpClassName, int nMaxCoutn);
        [DllImport("user32.dll")]
        private static extern bool IsWindowEnabled(IntPtr hWnd);
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        [StructLayout(LayoutKind.Sequential)]
        public class SECURITY_ATTRIBUTES
        {
            public int nLength;
            public string lpSecurityDescriptor;
            public bool bInheritHandle;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct STARTUPINFO
        {
            public int cb;
            public string lpReserved;
            public string lpDesktop;
            public int lpTitle;
            public int dwX;
            public int dwY;
            public int dwXSize;
            public int dwYSize;
            public int dwXCountChars;
            public int dwYCountChars;
            public int dwFillAttribute;
            public int dwFlags;
            public int wShowWindow;
            public int cbReserved2;
            public byte lpReserved2;
            public IntPtr hStdInput;
            public IntPtr hStdOutput;
            public IntPtr hStdError;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct PROCESS_INFORMATION
        {
            public IntPtr hProcess;
            public IntPtr hThread;
            public int dwProcessId;
            public int dwThreadId;
        }

        [DllImport("kernel32.dll")]
        private extern static
            Int32 WaitForSingleObject(IntPtr Handle,
            Int32 Wait);


        [DllImport("kernel32.dll")]
        private static extern bool CreateProcess(
           string lpApplicationName,
           string lpCommandLine,
           IntPtr lpProcessAttributes,
           IntPtr lpThreadAttributes,
           bool bInheritHandles,
           uint dwCreationFlags,
           IntPtr lpEnvironment,
           string lpCurrentDirectory,
           ref STARTUPINFO lpStartupInfo,
           ref PROCESS_INFORMATION lpProcessInformation);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern int CloseHandle(int hObject);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern int ResumeThread(int hThread);

        private static uint CREATE_SUSPENDED = 0x00000004;
        private static uint NORMAL_PRIORITY_CLASS = 0x00000020;

        [StructLayout(LayoutKind.Sequential)]
        public class PROCESS_MEMORY_COUNTERS
        {
            public int cb;
            public int PageFaultCount;
            public int PeakWorkingSetSize;
            public int WorkingSetSize;
            public int QuotaPeakPagedPoolUsage;
            public int QuotaPagedPoolUsage;
            public int QuotaPeakNonPagedPoolUsage;
            public int QuotaNonPagedPoolUsage;
            public int PagefileUsage;
            public int PeakPagefileUsage;
        }
        [DllImport("psapi.dll")]
        public static extern int GetProcessMemoryInfo(
          int hProcess,
          [Out] PROCESS_MEMORY_COUNTERS counters,
          int size
          );