Exemple #1
0
 public static extern int NtWow64QueryInformationProcess64(SafeProcessHandle hProcess, ProcessInfoClass pic, ref ProcessBasicInformationWow64 pbi, int cb, out int pSize);
Exemple #2
0
 public static extern int NtWow64QueryInformationProcess64(SafeProcessHandle hProcess, ProcessInfoClass pic, ref ProcessBasicInformationWow64 pbi, int cb, out int pSize);
        // Reads native process info from a 64-bit process in the case where this function is executing
        // in a 32-bit process.
        private bool LoadProcessInfoWow64(SafeProcessHandle handle, ProcessAccessFlags flags)
        {
            ulong pebSize = (ulong)MarshalUtility.UnmanagedStructSize<PebWow64>();
              ulong processParamsSize =
              (ulong)MarshalUtility.UnmanagedStructSize<RtlUserProcessParametersWow64>();

              // Read PROCESS_BASIC_INFORMATION up to and including the pointer to PEB structure.
              int processInfoSize =
              MarshalUtility.UnmanagedStructSize<ProcessBasicInformationWow64>();
              ProcessBasicInformationWow64 pbi = new ProcessBasicInformationWow64();
              int result = NativeMethods.NtWow64QueryInformationProcess64(
              handle,
              ProcessInfoClass.BasicInformation,
              ref pbi,
              processInfoSize,
              out processInfoSize);
              if (result != 0)
            return false;

              _parentProcessId = (int)pbi.ParentProcessId;
              Debug.Assert((int)pbi.UniqueProcessId == _processId);

              if (flags.HasFlag(ProcessAccessFlags.VmRead)) {
            IntPtr pebBuffer = IntPtr.Zero;
            IntPtr processParametersBuffer = IntPtr.Zero;
            IntPtr commandLineBuffer = IntPtr.Zero;

            try {
              pebBuffer = Marshal.AllocHGlobal((int)pebSize);
              // Read PEB up to and including the pointer to RTL_USER_PROCESS_PARAMETERS
              // structure.
              result = NativeMethods.NtWow64ReadVirtualMemory64(
              handle,
              pbi.PebBaseAddress,
              pebBuffer,
              pebSize,
              out pebSize);
              if (result != 0)
            return false;
              PebWow64 peb = (PebWow64)Marshal.PtrToStructure(pebBuffer, typeof(PebWow64));
              _isBeingDebugged = peb.IsBeingDebugged;

              processParametersBuffer = Marshal.AllocHGlobal((int)processParamsSize);
              result = NativeMethods.NtWow64ReadVirtualMemory64(
              handle,
              peb.ProcessParameters,
              processParametersBuffer,
              processParamsSize,
              out processParamsSize);
              if (result != 0)
            return false;
              RtlUserProcessParametersWow64 processParameters = (RtlUserProcessParametersWow64)
              Marshal.PtrToStructure(
                  processParametersBuffer,
                  typeof(RtlUserProcessParametersWow64));

              ulong commandLineBufferSize = (ulong)processParameters.CommandLine.MaximumLength;
              commandLineBuffer = Marshal.AllocHGlobal((int)commandLineBufferSize);
              result = NativeMethods.NtWow64ReadVirtualMemory64(
              handle,
              processParameters.CommandLine.Buffer,
              commandLineBuffer,
              commandLineBufferSize,
              out commandLineBufferSize);
              if (result != 0)
            return false;
              _commandLine = Marshal.PtrToStringUni(commandLineBuffer);
            } finally {
              if (pebBuffer != IntPtr.Zero)
            Marshal.FreeHGlobal(pebBuffer);
              if (commandLineBuffer != IntPtr.Zero)
            Marshal.FreeHGlobal(commandLineBuffer);
              if (processParametersBuffer != IntPtr.Zero)
            Marshal.FreeHGlobal(processParametersBuffer);
            }
              }
              return true;
        }