[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern SafeProcessHandle OpenProcess(int access, bool inherit, int processId);
using (var processHandler = SafeProcessHandle.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processId))
{
if (!processHandler.IsInvalid)
{
int needed = 0;
IntPtr[] hMods = new IntPtr[1024];
var strBuilder = new StringBuilder(1024);
var handle2 = new GCHandle();
try
{
if (!GetModuleFileNameEx(processHandler, IntPtr.Zero, strBuilder, (uint)(strBuilder.Capacity)))
return;
fnFileFound(strBuilder.ToString());
handle2 = GCHandle.Alloc(hMods, GCHandleType.Pinned);
var uiSize = (uint)(Marshal.SizeOf(typeof(IntPtr)) * (hMods.Length));
var flag = EnumProcessModulesEx(processHandler, handle2.AddrOfPinnedObject(), uiSize, ref needed, LIST_MODULES_ALL);
if (flag != 0)
{
var uiTotalNumberofModules = needed / (Marshal.SizeOf(typeof(IntPtr)));
for (var i = 0; i < uiTotalNumberofModules; i++)
{
GetModuleFileNameEx(processHandler, hMods[i], strBuilder, (uint)(strBuilder.Capacity));
fnFileFound(strBuilder.ToString());
if (!Active)
break;
}
}
}
finally
{
if (handle2.IsAllocated)
handle2.Free();
}
}
}
...
if ((uint)e.HResult == 0x80070002) actualException = "Ошибка : Не удается найти файл - " + ((FileNotFoundException)e).FileName + ".";
else if ((uint)e.HResult == 0x80070057) actualException = "Ошибка : Неверно задан путь к файлу или вы ссылаетесь на устройство нефайлового типа.";
else if ((uint)e.HResult == 0x80070003) actualException = "Ошибка : Неверно задан путь к файлу.";
else if ((uint)e.HResult == 0x80030003) actualException = "Ошибка : Неверно задан путь к файлу.";
...
var process = new Process
{
StartInfo = psi
};
process.OutputDataReceived += (sender, args) => Display(args.Data);
process.ErrorDataReceived += (sender, args) => Display(args.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
var exited = process.WaitForExit(0);