Rank: Advanced Member
Groups: Approved
Joined: 9/13/2018(UTC) Posts: 54 Thanks: 18 times Was thanked: 18 time(s) in 13 post(s)
|
Hi I wrote this code for applications that should not be closed or should not be closed by accident. Nothing special, but it's something.
Plugin dll for new window: CloseWindowExceptionFormLibrary Unlock dll before start. Thanks to Rob i can finally create plugins.
The exceptions are stored in the txt file. When the file is created, please convert it to "UTF-8", characters other than English may not be recognized. You can choose which parameters will be stored, such as Exe name, Title and Class name. But be careful what you put in the file. The less data you provide, the more applications may not be closed. For example, if you only save Exe name like "explorer.exe" then all applications from the explorer will not be closed.
Remember, an empty string is still a value for correctly recognizing windows.
Close App (main):Code:var wnd = sp.WindowFromPoint(sp.GetCurrentMousePoint(), true);
var lines = File.ReadAllLines("C:\\S+Net\\AppCloseExceptionInfo.txt"); // file for exceptions
var t_tmp = []; // for names in line (file)
var s_noName = "nameNotUsed"; // if you uncheck/disable a name (Add Exception), "s_noName" will replace that name from the line
var s_exe = s_noName;
var s_title = s_noName;
var s_class = s_noName;
var b_exe = false;
var b_title = false;
var b_class = false;
var b_found = false;
// COMPARING
for (i = 0; i < lines.Length; i++) {
t_tmp = lines[i].split(";");
if (lines[i].trim()) { // do not work with an empty line
// SEPARATE NAMES FROM LINE/FILE
for (j = 0; j < t_tmp.length; j++) {
if (/E\*/.exec(t_tmp[j])) s_exe = t_tmp[j].slice(2,t_tmp[j].length).trim();
if (/T\*/.exec(t_tmp[j])) s_title = t_tmp[j].slice(2,t_tmp[j].length).trim();
if (/C\*/.exec(t_tmp[j])) s_class = t_tmp[j].slice(2,t_tmp[j].length).trim();
}
// CHECK WHICH NAMES MATCH
if (s_exe != s_noName) { // exe / if name is enabled
// I'm not use "Process.MainModule.ModuleName" because can create problem like this: "unable to enumerate the process modules"
if (s_exe == wnd.Process.ProcessName+".exe") b_exe = true; // check if the current name is in line
} else { // if name is disabled let "b_exe" to set as "true" by any app
b_exe = true;
}
if (s_title != s_noName) { // title
if (s_title == wnd.Title) b_title = true;
} else {
b_title = true;
}
if (s_class != s_noName) { // class
var error = null;
try { // catch any error
wnd.ClassName;
} catch(err) {
error = err.message;
b_class = true; // if any error appear, set as true
}
if (!error) if (s_class == wnd.ClassName) b_class = true;
} else {
b_class = true;
}
// FOUND APP
if (b_exe && b_title && b_class) { // all bool's must be "true" to correct recognize app
b_found = true;
break;
}
// RESET
s_exe = s_noName; s_title = s_noName; s_class = s_noName;
b_exe = false; b_title = false; b_class = false;
}
}
// CLOSE APP
if (!b_found) {
wnd.SendClose();
}
Close App Add Exception To File:Code:var wnd = sp.WindowFromPoint(sp.GetCurrentMousePoint(), true);
var f_path = "C:\\S+Net\\AppCloseExceptionInfo.txt"; // file for exceptions
var s_newLine = ""; // names to save
var s_displayMsg = ""; // str to display in "displayText" function
var b_found = false;
var s_exe = wnd.Process.ProcessName+".exe";
var s_title = wnd.Title;
var s_class = "";
var error = null;
try {
wnd.ClassName;
} catch(err) {
error = err.message;
}
if (!error) s_class = wnd.ClassName;
// SHOW DIALOG BOX
var form = new CloseWindowExceptionLibrary().GetForm();
form.Text = "Close Window Exception";
form.label1.Text = "Exe:";
form.label2.Text = "Title:";
form.label3.Text = "Class:";
form.groupBox1.Text = "Choose parameters to save:";
form.button1.Text = "Ok";
form.button2.Text = "Cancel";
form.checkBox1.Text = s_exe;
form.checkBox2.Text = s_title;
form.checkBox3.Text = s_class;
var result = form.ShowDialog();
if(result.ToString() == "OK") {
// CONCAT NAMES
// "E*" = exe name, "T*" = title, "C*" = class name
if (form.checkBox1.Checked) { // exe
s_newLine = s_newLine + "E*" + s_exe + ";";
s_displayMsg = s_displayMsg + "Exe: " + s_exe;
}
if (form.checkBox2.Checked) { // title
s_newLine = s_newLine + "T*" + s_title + ";";
if (s_displayMsg) s_displayMsg = s_displayMsg + "\n"; // add new line
s_displayMsg = s_displayMsg + "Title: " + s_title;
}
if (form.checkBox3.Checked) { // class
s_newLine = s_newLine + "C*" + s_class + ";";
if (s_displayMsg) s_displayMsg = s_displayMsg + "\n"; // add new line
s_displayMsg = s_displayMsg + "Class: "+s_class;
}
// SEARCH FOR THE SAME APP
if (!File.Exists(f_path)) File.WriteAllText(f_path, ""); // create a file if not exists
var lines = File.ReadAllLines(f_path);
for (i = 0; i < lines.Length; i++) {
if (s_newLine == lines[i].trim()) {
b_found = true;
break;
}
}
// ADD LINE TO FILE
if (!b_found) {
var f = File.AppendText(f_path);
f.WriteLine(s_newLine);
f.Close();
displayText("An exception has been added:", s_displayMsg, "green");
} else {
displayText("This exception already exists:", s_displayMsg, "red");
}
} // clicked ok
// DISPLAY TEXT
function displayText(title, msg, cl) {
var info = new DisplayTextInfo();
info.Title = title;
info.TitleAlignment = "Center";
info.Message = msg;
info.MessageAlignment = "Left";
info.Duration = 7000;
info.Opacity = 0.90;
info.Location = "top";
info.TitleFont = new Font("Segoe UI", 12, host.flags(FontStyle.Bold));
info.MessageFont = new Font("Segoe UI Semibold", 12);
info.BackColor = cl;
info.ForeColor = "white";
info.Padding = 15;
info.UsePrimaryScreen = true;
sp.DisplayText(info);
}
Close App Remove Exception From File (by simply run the file):Code:sp.RunProgram("C:\\Windows\\notepad.exe", "C:\\S+Net\\AppCloseExceptionInfo.txt", "", "", false, true, false);
Edited by user Monday, June 17, 2019 9:07:03 PM(UTC)
| Reason: An updated dll file, if all items are unchecked, the OK button will be disabled and "Close App
|