-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
127 lines (115 loc) · 4.2 KB
/
Program.cs
File metadata and controls
127 lines (115 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace EXIF_Remover
{
internal static class Program
{
private const string MutexName = "EXIF_Remover_SingleInstance_v1";
private const int WM_COPYDATA = 0x004A;
private const string MainWindowTitle = "EXIF Remover"; // must match Form1.Text
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var argv = (args?.Length > 0 ? args : Environment.GetCommandLineArgs()?.Skip(1)?.ToArray())
?? Array.Empty<string>();
// Accept both files and directories from shell/context menu
var startupFiles = argv.Where(p => File.Exists(p) || Directory.Exists(p)).ToArray();
using (var mutex = new Mutex(true, MutexName, out bool isOwner))
{
if (isOwner)
{
Application.Run(new Form1(startupFiles));
}
else
{
if (startupFiles.Length > 0)
{
var payload = string.Join("\n", startupFiles);
var hwnd = FindPrimaryWindow();
if (hwnd != IntPtr.Zero)
{
SendString(hwnd, payload);
}
}
// exit immediately
}
}
}
private static IntPtr FindPrimaryWindow()
{
var hwnd = Native.FindWindow(null, MainWindowTitle);
if (hwnd != IntPtr.Zero) return hwnd;
try
{
var exeName = Path.GetFileNameWithoutExtension(Application.ExecutablePath);
var procs = Process.GetProcessesByName(exeName);
foreach (var p in procs)
{
if (p.Id == Process.GetCurrentProcess().Id) continue;
if (p.MainWindowHandle != IntPtr.Zero) return p.MainWindowHandle;
}
}
catch
{
}
return IntPtr.Zero;
}
private static void SendString(IntPtr hwnd, string text)
{
var managed = (text ?? string.Empty) + "\0";
var hMem = Marshal.StringToHGlobalUni(managed);
try
{
var cds = new Native.COPYDATASTRUCT
{
dwData = IntPtr.Zero,
cbData = managed.Length * 2,
lpData = hMem
};
Native.SendMessage(hwnd, WM_COPYDATA, IntPtr.Zero, ref cds);
}
finally
{
if (hMem != IntPtr.Zero) Marshal.FreeHGlobal(hMem);
}
}
/*
private static void TryAppendLog(string message)
{
try
{
var root = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"EXIFRemover");
var path = Path.Combine(root, "EXIF_Remover.forwarder.log");
Directory.CreateDirectory(root);
File.AppendAllText(path, $"[{DateTime.Now:O}] {message}{Environment.NewLine}");
}
catch
{
}
}
*/
private static class Native
{
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
}
}
}