1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 using System.Collections.Generic;
7 using System.ComponentModel;
9 using System.Runtime.InteropServices;
11 using System.Threading.Tasks;
13 using ChromeDebug.LowLevel;
15 namespace ChromeDebug {
16 internal static class Utility {
17 public static string[] SplitArgs(string unsplitArgumentLine) {
18 if (unsplitArgumentLine == null)
22 IntPtr ptrToSplitArgs;
25 ptrToSplitArgs = NativeMethods.CommandLineToArgvW(unsplitArgumentLine, out numberOfArgs);
27 // CommandLineToArgvW returns NULL upon failure.
28 if (ptrToSplitArgs == IntPtr.Zero)
29 throw new ArgumentException("Unable to split argument.", new Win32Exception());
31 // Make sure the memory ptrToSplitArgs to is freed, even upon failure.
33 splitArgs = new string[numberOfArgs];
35 // ptrToSplitArgs is an array of pointers to null terminated Unicode strings.
36 // Copy each of these strings into our split argument array.
37 for (int i = 0; i < numberOfArgs; i++)
38 splitArgs[i] = Marshal.PtrToStringUni(
39 Marshal.ReadIntPtr(ptrToSplitArgs, i * IntPtr.Size));
44 // Free memory obtained by CommandLineToArgW.
45 NativeMethods.LocalFree(ptrToSplitArgs);
49 public static T ReadUnmanagedStructFromProcess<T>(IntPtr processHandle,
50 IntPtr addressInProcess) {
52 int bytesToRead = Marshal.SizeOf(typeof(T));
53 IntPtr buffer = Marshal.AllocHGlobal(bytesToRead);
54 if (!NativeMethods.ReadProcessMemory(processHandle, addressInProcess, buffer, bytesToRead,
56 throw new Win32Exception();
57 T result = (T)Marshal.PtrToStructure(buffer, typeof(T));
58 Marshal.FreeHGlobal(buffer);
62 public static string ReadStringUniFromProcess(IntPtr processHandle,
63 IntPtr addressInProcess,
66 IntPtr outBuffer = Marshal.AllocHGlobal(NumChars * 2);
68 bool bresult = NativeMethods.ReadProcessMemory(processHandle,
74 throw new Win32Exception();
76 string result = Marshal.PtrToStringUni(outBuffer, bytesRead / 2);
77 Marshal.FreeHGlobal(outBuffer);
81 public static int UnmanagedStructSize<T>() {
82 return Marshal.SizeOf(typeof(T));