Skip to content

Commit

Permalink
Add launch process info
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlaw1979 committed Apr 19, 2023
1 parent fe52e71 commit 7412d28
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
74 changes: 74 additions & 0 deletions nmf-view/ProcessInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// Modified from https://github.com/PowerShell/PowerShell/blob/3dc95ced87d38c347a9fc3a222eb4c52eaad4615/src/System.Management.Automation/engine/ProcessCodeMethods.cs
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace nmf_view
{
/// <summary>
/// Helper functions for process info.
/// </summary>
public static class ProcessInfo
{
[StructLayout(LayoutKind.Sequential)]
internal class PROCESS_BASIC_INFORMATION
{
public int ExitStatus = 0;
public IntPtr PebBaseAddress = (IntPtr)0;
public IntPtr AffinityMask = (IntPtr)0;
public int BasePriority = 0;
public IntPtr UniqueProcessId = (IntPtr)0;
public IntPtr InheritedFromUniqueProcessId = (IntPtr)0;
}

[DllImport("ntdll.dll", SetLastError = true)]
internal static extern int NtQueryInformationProcess(IntPtr processHandle, int query, PROCESS_BASIC_INFORMATION info, int size, int[] returnedSize);

private const int InvalidProcessId = -1;

internal static Process GetParent(this Process process)
{
try
{
var pid = GetParentPid(process);
if (pid == InvalidProcessId)
{
return null;
}

var candidate = Process.GetProcessById(pid);

// if the candidate was started later than process, the pid has been recycled
return candidate.StartTime > process.StartTime ? null : candidate;
}
catch (Exception)
{
return null;
}
}

/// <summary>
/// CodeMethod for getting the parent process of a process.
/// </summary>
/// <param name="process">The process whose parent should be returned</param>
/// <returns>The parent process, or null if the parent is no longer running.</returns>
public static object GetParentProcess(Process process)
{
return process?.GetParent();
}

/// <summary>
/// Returns the parent id of a process or -1 if it fails.
/// </summary>
/// <param name="process"></param>
/// <returns>The pid of the parent process.</returns>
internal static int GetParentPid(Process process)
{
PROCESS_BASIC_INFORMATION pbi = new PROCESS_BASIC_INFORMATION();
int status = NtQueryInformationProcess(process.Handle, 0, pbi, (int)Marshal.SizeOf(pbi), null);
return status != 0 ? InvalidProcessId : pbi.InheritedFromUniqueProcessId.ToInt32();
}
}
}
5 changes: 4 additions & 1 deletion nmf-view/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("1.0.6.0")]
[assembly: AssemblyVersion("1.0.7.0")]

// v1.0.7
// Display the invoking process' name and PID

// v1.0.6
// Add support for 'logstderr' filename token
Expand Down
16 changes: 11 additions & 5 deletions nmf-view/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ struct app_state
public Stream strmToApp;
public Stream strmFromExt;
public Stream strmToExt;
public Process procParent;
}

static app_state oSettings;
Expand Down Expand Up @@ -508,8 +509,15 @@ private void CloseLogfile()

private void frmMain_Load(object sender, EventArgs e)
{
Trace.WriteLine("NMF-View was started with the command line: " + Environment.CommandLine);
Console.Error.WriteLine("****\n**** NMF-View was started with the command line: " + Environment.CommandLine + "\n****");
oSettings.procParent = ProcessInfo.GetParent(Process.GetCurrentProcess());

{
string sStartupInfo = $"NMF-View was started with the command line: {Environment.CommandLine} by " +
((null == oSettings.procParent) ? " an unknown parent." : oSettings.procParent.ProcessName + "(" + oSettings.procParent.Id.ToString() + ")");
Trace.WriteLine(sStartupInfo);
Console.Error.WriteLine($"****\n**** {sStartupInfo}\n****");
}

// Configure default options.
clbOptions.SetItemChecked(2, true); // Propagate closures
clbOptions.SetItemChecked(3, true); // Record bodies
Expand All @@ -523,7 +531,7 @@ private void frmMain_Load(object sender, EventArgs e)
if (sCurrentExe.Contains(".immortal.")) clbOptions.SetItemChecked(5, true);

string sExtraInfo = $" [{Path.GetFileName(Application.ExecutablePath)}:{Process.GetCurrentProcess().Id}{(Utilities.IsUserAnAdmin() ? " Elevated" : String.Empty)}]";
log($"I am{sExtraInfo}");
log($"I am{sExtraInfo}, launched by [{((null != oSettings.procParent) ? (oSettings.procParent.ProcessName + ':' + oSettings.procParent.Id) : "unknown")}].");
lblVersion.Text = $"v{Application.ProductVersion} [{((8 == IntPtr.Size) ? "64" : "32")}-bit]";
Text += sExtraInfo; // Append extra info to form caption.

Expand Down Expand Up @@ -559,7 +567,6 @@ private void frmMain_Load(object sender, EventArgs e)
log($"parent-window: {oSettings.iParentWindow:x8} {((oSettings.iParentWindow==0)?"(Background Script)":String.Empty)}");
}
}

oSettings.sExtensionID = oSettings.sExtensionID.Replace("chrome-extension://", String.Empty).TrimEnd('/');
toolTip1.SetToolTip(pbExt, $"Connected to {oSettings.sExtensionID}.\nDouble-click to disconnect.");
toolTip1.SetToolTip(pbApp, $"Click to set the ClientHandler to another instance of this app.");
Expand Down Expand Up @@ -754,7 +761,6 @@ private void lnkGithub_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
{
Process.Start("https://github.com/ericlaw1979/NativeMessagingDebugger");
}

private void lnkDocs_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("https://github.com/ericlaw1979/NativeMessagingDebugger/blob/main/DOCS.md");
Expand Down
1 change: 1 addition & 0 deletions nmf-view/nmf-view.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<SubType>Component</SubType>
</Compile>
<Compile Include="JSON.cs" />
<Compile Include="ProcessInfo.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RegisteredHosts.cs" />
Expand Down

0 comments on commit 7412d28

Please sign in to comment.