Skip to content

Commit 78b40b1

Browse files
author
Eric Lawrence
committed
Add immortality
Aimed to address #10, unfortunately it doesn't quite work because Chrome already has a handle with the TERMINATE right.
1 parent b9b5211 commit 78b40b1

File tree

4 files changed

+245
-110
lines changed

4 files changed

+245
-110
lines changed

nmf-view/Program.cs

+3-50
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,19 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Runtime.InteropServices;
43
using System.Windows.Forms;
54

65
namespace nmf_view
76
{
87
static class Program
98
{
10-
11-
9+
[DllImport("user32.dll", SetLastError = true)]
10+
static extern bool SetProcessDPIAware();
1211
private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs ue)
1312
{
1413
Exception unhandledException = (Exception)ue.ExceptionObject;
15-
ReportException(unhandledException);
14+
Utilities.ReportException(unhandledException);
1615
}
1716

18-
internal static void ReportException(Exception eX)
19-
{
20-
string sTitle = "Sorry, you may have found an error...";
21-
ReportException(eX, sTitle, null);
22-
}
23-
public static void ReportException(Exception eX, string sTitle, string sCallerMessage)
24-
{
25-
Trace.WriteLine("******ReportException()******\n" + eX.Message + "\n" + eX.StackTrace + "\n" + eX.InnerException);
26-
27-
if ((eX is System.Threading.ThreadAbortException)) // TODO: What about ObjectDisposedException?
28-
{
29-
return;
30-
}
31-
32-
string sMessage;
33-
if (eX is OutOfMemoryException)
34-
{
35-
sTitle = "Insufficient Memory Address Space";
36-
sMessage = "An out-of-memory exception was encountered.\nGC Total Allocated: " + GC.GetTotalMemory(false).ToString("N0") + " bytes.";
37-
}
38-
else
39-
{
40-
if (String.IsNullOrEmpty(sCallerMessage))
41-
{
42-
sMessage = "NMF-View encountered an unexpected problem. If you believe this is a bug, please copy this message by hitting CTRL+C, and submit a issue report on GitHub";
43-
}
44-
else
45-
{
46-
sMessage = sCallerMessage;
47-
}
48-
}
49-
50-
MessageBox.Show(
51-
sMessage + "\n\n" +
52-
eX.Message + "\n\n" +
53-
"Type: " + eX.GetType().ToString() + "\n" +
54-
"Source: " + eX.Source + "\n" +
55-
eX.StackTrace + "\n\n" +
56-
eX.InnerException + "\n" +
57-
"NMF-View v" + Application.ProductVersion + ((8 == IntPtr.Size) ? " (x64 " : " (x86 ") + Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") + ") [.NET " + Environment.Version + " on " + Environment.OSVersion.VersionString + "] ",
58-
sTitle);
59-
}
60-
61-
[DllImport("user32.dll", SetLastError = true)]
62-
static extern bool SetProcessDPIAware();
63-
6417
[STAThread]
6518
static void Main()
6619
{

nmf-view/Utilities.cs

+119
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System;
33
using System.Diagnostics;
44
using System.Runtime.InteropServices;
5+
using System.Security.AccessControl;
6+
using System.Security.Principal;
57
using System.Windows.Forms;
68

79
namespace nmf_view
@@ -12,6 +14,26 @@ class Utilities
1214
[return: MarshalAs(UnmanagedType.Bool)]
1315
public static extern bool IsUserAnAdmin();
1416

17+
public static bool DenyProcessTermination()
18+
{
19+
try
20+
{
21+
var hCurrentProcess = Process.GetCurrentProcess().SafeHandle;
22+
var processSecurity = new ProcessSecurity(hCurrentProcess);
23+
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
24+
// Create a rule to deny process termination.
25+
ProcessAccessRule rule = new ProcessAccessRule(sid, ProcessAccessRights.Terminate, false,
26+
InheritanceFlags.None, PropagationFlags.None, AccessControlType.Deny);
27+
processSecurity.AddAccessRule(rule);
28+
processSecurity.SaveChanges(hCurrentProcess);
29+
}
30+
catch (Exception eX)
31+
{
32+
ReportException(eX);
33+
return false;
34+
}
35+
return true;
36+
}
1537
internal static void CopyToClipboard(string s)
1638
{
1739
DataObject data = new DataObject();
@@ -47,5 +69,102 @@ internal static void OpenExplorerTo(string manifestFilename)
4769
/* User may abort */
4870
}
4971
}
72+
73+
internal static void ReportException(Exception eX)
74+
{
75+
string sTitle = "Sorry, you may have found an error...";
76+
ReportException(eX, sTitle, null);
77+
}
78+
public static void ReportException(Exception eX, string sTitle, string sCallerMessage)
79+
{
80+
Trace.WriteLine("******ReportException()******\n" + eX.Message + "\n" + eX.StackTrace + "\n" + eX.InnerException);
81+
82+
if ((eX is System.Threading.ThreadAbortException)) // TODO: What about ObjectDisposedException?
83+
{
84+
return;
85+
}
86+
87+
string sMessage;
88+
if (eX is OutOfMemoryException)
89+
{
90+
sTitle = "Insufficient Memory Address Space";
91+
sMessage = "An out-of-memory exception was encountered.\nGC Total Allocated: " + GC.GetTotalMemory(false).ToString("N0") + " bytes.";
92+
}
93+
else
94+
{
95+
if (String.IsNullOrEmpty(sCallerMessage))
96+
{
97+
sMessage = "NMF-View encountered an unexpected problem. If you believe this is a bug, please copy this message by hitting CTRL+C, and submit a issue report on GitHub";
98+
}
99+
else
100+
{
101+
sMessage = sCallerMessage;
102+
}
103+
}
104+
105+
MessageBox.Show(
106+
sMessage + "\n\n" +
107+
eX.Message + "\n\n" +
108+
"Type: " + eX.GetType().ToString() + "\n" +
109+
"Source: " + eX.Source + "\n" +
110+
eX.StackTrace + "\n\n" +
111+
eX.InnerException + "\n" +
112+
"NMF-View v" + Application.ProductVersion + ((8 == IntPtr.Size) ? " (x64 " : " (x86 ") + Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") + ") [.NET " + Environment.Version + " on " + Environment.OSVersion.VersionString + "] ",
113+
sTitle);
114+
}
115+
}
116+
internal class ProcessSecurity : NativeObjectSecurity
117+
{
118+
public ProcessSecurity(SafeHandle processHandle)
119+
: base(false, ResourceType.KernelObject, processHandle, AccessControlSections.Access) { }
120+
121+
public void AddAccessRule(ProcessAccessRule rule)
122+
{
123+
base.AddAccessRule(rule);
124+
}
125+
126+
public void SaveChanges(SafeHandle processHandle)
127+
{
128+
Persist(processHandle, AccessControlSections.Access);
129+
}
130+
131+
public override Type AccessRightType
132+
{
133+
get { return typeof(ProcessAccessRights); }
134+
}
135+
136+
public override AccessRule AccessRuleFactory(System.Security.Principal.IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
137+
{
138+
return new ProcessAccessRule(identityReference, (ProcessAccessRights)accessMask, isInherited, inheritanceFlags, propagationFlags, type);
139+
}
140+
141+
public override Type AccessRuleType
142+
{
143+
get { return typeof(ProcessAccessRule); }
144+
}
145+
146+
public override AuditRule AuditRuleFactory(System.Security.Principal.IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags,
147+
PropagationFlags propagationFlags, AuditFlags flags)
148+
{
149+
throw new NotImplementedException();
150+
}
151+
152+
public override Type AuditRuleType
153+
{
154+
get { throw new NotImplementedException(); }
155+
}
156+
}
157+
158+
internal class ProcessAccessRule : AccessRule
159+
{
160+
public ProcessAccessRule(IdentityReference identityReference, ProcessAccessRights accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
161+
: base(identityReference, (int)accessMask, isInherited, inheritanceFlags, propagationFlags, type) { }
162+
163+
public ProcessAccessRights ProcessAccessRights { get { return (ProcessAccessRights)AccessMask; } }
164+
}
165+
[Flags]
166+
internal enum ProcessAccessRights
167+
{
168+
Terminate = 1
50169
}
51170
}

nmf-view/frmMain.Designer.cs

+11-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)