1d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// found in the LICENSE file.
4d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)using Microsoft.Win32.SafeHandles;
6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)using System;
7d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)using System.Collections.Generic;
8d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)using System.Linq;
9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)using System.Runtime.InteropServices;
10d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)using System.Text;
11d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)using System.Threading.Tasks;
12d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
13d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace ChromeDebug.LowLevel {
14d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  public static class NativeMethods {
15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    [DllImport("kernel32.dll", SetLastError = true)]
16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    [return: MarshalAs(UnmanagedType.Bool)]
17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    public static extern bool ReadProcessMemory(IntPtr hProcess,
18d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                IntPtr lpBaseAddress,
19d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                IntPtr lpBuffer,
20d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                int dwSize,
21d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                out int lpNumberOfBytesRead);
22d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
23d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    [DllImport("ntdll.dll", SetLastError = true)]
24d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    public static extern LowLevelTypes.NTSTATUS NtQueryInformationProcess(
25d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        IntPtr hProcess,
26d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        LowLevelTypes.PROCESSINFOCLASS pic,
27d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        ref LowLevelTypes.PROCESS_BASIC_INFORMATION pbi,
28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        int cb,
29d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        out int pSize);
30d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
31d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    [DllImport("shell32.dll", SetLastError = true)]
32d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    public static extern IntPtr CommandLineToArgvW(
33d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        [MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine,
34d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        out int pNumArgs);
35d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    [DllImport("kernel32.dll", SetLastError = true)]
37d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    public static extern IntPtr LocalFree(IntPtr hMem);
38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
39d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    [DllImport("kernel32.dll", SetLastError = true)]
40d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    public static extern IntPtr OpenProcess(
41d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        LowLevelTypes.ProcessAccessFlags dwDesiredAccess,
42d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
43d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        int dwProcessId);
44d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
45d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall,
46d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        CharSet = CharSet.Unicode)]
47d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    public static extern uint QueryFullProcessImageName(
48d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        IntPtr hProcess,
49d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        [MarshalAs(UnmanagedType.U4)] LowLevelTypes.ProcessQueryImageNameMode flags,
50d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        [Out] StringBuilder lpImageName, ref int size);
51d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
52d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    [DllImport("kernel32.dll", SetLastError = true)]
53d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    [return: MarshalAs(UnmanagedType.Bool)]
54d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    public static extern bool CloseHandle(IntPtr hObject);
55d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
56d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
57d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    public static extern SafeFileHandle CreateFile(string lpFileName,
58d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                   LowLevelTypes.FileAccessFlags dwDesiredAccess,
59d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                   LowLevelTypes.FileShareFlags dwShareMode,
60d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                   IntPtr lpSecurityAttributes,
61d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                   LowLevelTypes.FileCreationDisposition dwDisp,
62d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                   LowLevelTypes.FileFlagsAndAttributes dwFlags,
63d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                                   IntPtr hTemplateFile);
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    public static extern IntPtr SHGetFileInfo(string pszPath,
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                              uint dwFileAttributes,
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                              ref LowLevelTypes.SHFILEINFO psfi,
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                              uint cbFileInfo,
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                              uint uFlags);
71d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
72d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
73