forked from pascalabcnet/pascalabcnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (110 loc) · 4.83 KB
/
Program.cs
File metadata and controls
120 lines (110 loc) · 4.83 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
// Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace VisualPascalABC
{
struct COPYDATASTRUCT
{
public IntPtr dwData;
public uint cbData;
public string lpData;
}
static class VisualPascalABCProgram
{
/// <summary>
/// The main entry point for the application.
/// </summary>
public static string[] CommandLineArgs;
public static Form1 MainForm;
public static Process CurrentProcess;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
[DllImport("User32.dll")]
static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("User32.dll")]
static extern int SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll")]
static extern int IsZoomed(IntPtr hWnd);
[DllImport("User32.dll")]
static extern int IsIconic(IntPtr hWnd);
[STAThread]
static void Main(string[] args)
{
try
{
Process cur_proc = Process.GetCurrentProcess();
CurrentProcess = cur_proc;
Process[] p = Process.GetProcessesByName(cur_proc.ProcessName);
IntPtr handle = IntPtr.Zero;
if (p.Length > 1)
{
for (int i = 0; i < p.Length; i++)
if (string.Compare(System.IO.Path.GetDirectoryName(p[i].MainModule.FileName), System.IO.Path.GetDirectoryName(cur_proc.MainModule.FileName), true) == 0 && p[i].Id != cur_proc.Id)
{
handle = p[i].MainWindowHandle;
/*if (handle == IntPtr.Zero)
{
System.Threading.Thread.Sleep(5000);
p[i].Refresh();
if (!p[i].HasExited)
handle = p[i].MainWindowHandle;
}*/
if (handle != IntPtr.Zero)
{
SetForegroundWindow(handle);
if (IsZoomed(handle) != 0)
ShowWindow(handle, 3/*SW_MAXIMIZE*/);
else
if (IsIconic(handle) != 0)
ShowWindow(handle, 9/*SW_RESTORE*/);
else
ShowWindow(handle, 5/*SW_SHOW*/);
if (args.Length > 0)
{
//for (int j = 0; j < args.Length; j++)
{
COPYDATASTRUCT ds = new COPYDATASTRUCT();
IntPtr str_ptr = System.Runtime.InteropServices.Marshal.StringToHGlobalUni(args[0]);
ds.dwData = (IntPtr)32;//flag, zadaem sami, chtoby process znal, kakoe eto konkretno soobshenie
ds.cbData = (uint)(args[0].Length + 1);
ds.lpData = args[0];//str_ptr;
IntPtr ds_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(ds));
Marshal.StructureToPtr(ds, ds_ptr, false);
SendMessage(handle, 74/*WM_COPYDATA*/, cur_proc.Handle, ds_ptr);
}
}
return;
}
return;
}
}
}
catch
{
}
try
{
if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware();
CommandLineArgs = args;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new Form1();
Application.Run(MainForm);
}
catch (Exception e)
{
// Ýòî óæàñíî! Íàäî îáðàáàòûâàòü âñå èñêëþ÷åíèÿ ðàíüøå - ÷òîáû îáîëî÷êà íå çàêðûâàëàñü!
MessageBox.Show(e.ToString());
}
}
}
public class MyForm : Form
{
}
}