-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
52 lines (47 loc) · 1.92 KB
/
Program.cs
File metadata and controls
52 lines (47 loc) · 1.92 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
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ImageCropper;
internal static class Program
{
[STAThread]
private static void Main(string[] args)
{ // GUID erhält man durch Eingabe in Developer PowerShell: [Guid]::NewGuid()
var mutexName = @"Local\ImageCropperRunningMutex"; // Local Mutex to allow multiple users on same machine
using Mutex singleMutex = new(true, mutexName, out var isNewInstance);
if (isNewInstance)
{
ApplicationConfiguration.Initialize();
var bildPfad = args.Length > 0 ? args[0] : null;
Application.Run(new Main(bildPfad));
}
else
{
using var otherProcess = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).FirstOrDefault(static p => p.Id != Environment.ProcessId);
if (otherProcess?.MainWindowHandle is { } handle && handle != IntPtr.Zero)
{
Utils.ShowWindow(handle, Utils.SW_RESTORE);
Utils.SetForegroundWindow(handle);
if (args.Length > 0)
{
var argument = args[0];
var lpData = Marshal.StringToHGlobalUni(argument);
try
{
var cds = new Utils.COPYDATASTRUCT
{
dwData = 1,
cbData = (argument.Length + 1) * 2, // UTF-16: 2 Bytes pro Zeichen + Null-Terminator
lpData = lpData
};
Utils.SendMessage(handle, Utils.WM_COPYDATA, IntPtr.Zero, ref cds);
}
finally
{
Marshal.FreeHGlobal(lpData);
}
}
}
}
GC.KeepAlive(singleMutex); // Ensure the mutex is not released prematurely
}
}