-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
140 lines (119 loc) · 4.12 KB
/
Program.cs
File metadata and controls
140 lines (119 loc) · 4.12 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
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace yata
{
/// <summary>
/// Class with program entry point.
/// </summary>
sealed class Program
{
#region Methods
/// <summary>
/// Program entry point.
/// </summary>
[STAThread]
static void Main(string[] args)
{
logfile.CreateLog(); // NOTE: The logfile works in debug-builds only.
// To write a line to the logfile:
// logfile.Log("what you want to know here");
//
// The logfile ought appear in the directory with the executable.
//string st = String.Empty; // debug ->
//foreach (var arg in args)
// st += Environment.NewLine + arg;
//logfile.Log("Main() args= " + st);
//logfile.Log(Environment.NewLine + "--------");
//string a = String.Empty;
//for (int i = 0; i != args.Length; ++i)
// a += ";" + args[i] + ";";
//logfile.Log("Main() args=" + a);
Process proc = null;
if (args.Length != 0) // else start a new instance of Yata
{
string label = Process.GetCurrentProcess().ProcessName;
//int id = current.Id;
//logfile.Log(". current process= " + id + " : " + label + " h= " + current.Handle);
DateTime dt = DateTime.Now, dtTest;
//logfile.Log(". dt= " + dt.Minute + ":" + dt.Second + ":" + dt.Millisecond);
Process[] processes = Process.GetProcesses();
//logfile.Log(". qty processes= " + processes.Length);
foreach (var process in processes)
{
//logfile.Log(". . process= " + process.Id + "\t: " + process.ProcessName);
if (process.MainWindowHandle != IntPtr.Zero // NOTE: 'current' won't have a MainWindow (ie. bypass current proc.)
&& process.ProcessName == label)
{
//logfile.Log(". . . found other Yata process");
//logfile.Log(". . . h= " + process.Handle + " MainWindowHandle= " + process.MainWindowHandle);
// find longest-running instance -> use it
dtTest = process.StartTime;
//logfile.Log(". . . dtTest= " + dtTest.Minute + ":" + dtTest.Second + ":" + dtTest.Millisecond);
if (dtTest < dt)
{
//logfile.Log(". . . . is less");
dt = dtTest;
proc = process;
}
//logfile.Log(". . . dt= " + dt);
//logfile.Log(". . . proc h= " + proc.MainWindowHandle);
}
}
}
if (proc != null)
{
//logfile.Log(". FOUND INSTANCE proc= " + proc.Id + " : " + proc.ProcessName);
// https://www.codeproject.com/Tips/1017834/How-to-Send-Data-from-One-Process-to-Another-in-Cs
IntPtr ptrCopyData = IntPtr.Zero;
try
{
string arg = args[0];
// create the data structure and fill with data
var copyData = new Crap.COPYDATASTRUCT();
copyData.dwData = new IntPtr(Crap.CopyDataStructType); // just a number to identify the data type
copyData.cbData = arg.Length + 1; // one extra byte for the \0 character
copyData.lpData = Marshal.StringToHGlobalAnsi(arg);
// allocate memory for the data and copy
ptrCopyData = Marshal.AllocCoTaskMem(Marshal.SizeOf(copyData));
Marshal.StructureToPtr(copyData, ptrCopyData, false);
// send the message
IntPtr ptrWnd = proc.MainWindowHandle;
Crap.SendMessage(ptrWnd, Crap.WM_COPYDATA, IntPtr.Zero, ptrCopyData);
}
catch (Exception ex)
{
using (var ib = new Infobox(Infobox.Title_excep,
"Failed to send file to Yata.",
ex.ToString(),
InfoboxType.Error))
{
ib.ShowDialog(); // this better not throw ...
}
}
finally
{
// free the allocated memory after execution has returned
if (ptrCopyData != IntPtr.Zero)
Marshal.FreeCoTaskMem(ptrCopyData);
}
}
else
{
if (args.Length != 0)
{
//logfile.Log(". pass arg to new instance");
Yata.PfeLoad = args[0];
}
//else logfile.Log(". no args - start new instance");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); // ie, use GDI aka TextRenderer class. (perhaps, read:
// perhaps depends on the Control that's being drawn)
Application.Run(new Yata());
Yata.PfeLoad = null;
}
}
#endregion Methods
}
}