-
Notifications
You must be signed in to change notification settings - Fork 4
/
XProcessMessages.cs
181 lines (155 loc) · 6.45 KB
/
XProcessMessages.cs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
namespace KMZ_Viewer
{
public class ProcDataExchange
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindow(HandleRef hWnd);
/// <summary>
/// Handle used to send the message to all windows
/// </summary>
public static IntPtr HWND_BROADCAST = new IntPtr(0xffff);
/// <summary>
/// An application sends the WM_COPYDATA message to pass data to another application.
/// </summary>
public static uint WM_COPYDATA = 0x004A;
/// <summary>
/// Contains data to be passed to another application by the WM_COPYDATA message.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
/// <summary>
/// User defined data to be passed to the receiving application.
/// </summary>
public IntPtr dwData;
/// <summary>
/// The size, in bytes, of the data pointed to by the lpData member.
/// </summary>
public int cbData;
/// <summary>
/// The data to be passed to the receiving application. This member can be IntPtr.Zero.
/// </summary>
public IntPtr lpData;
public int DataType
{
get
{
return (int)dwData;
}
set
{
dwData = new IntPtr(value);
}
}
public int DataLength
{
get
{
return cbData;
}
set
{
cbData = DataLength;
}
}
public string DataUTF8String
{
get
{
byte[] arr = new byte[this.cbData];
Marshal.Copy(this.lpData, arr, 0, arr.Length);
return System.Text.Encoding.UTF8.GetString(arr);
}
set
{
byte[] arr = System.Text.Encoding.UTF8.GetBytes(value);
this.cbData = arr.Length;
this.lpData = Marshal.AllocHGlobal(arr.Length);
Marshal.Copy(arr, 0, this.lpData, arr.Length);
}
}
public string DataString
{
get
{
byte[] arr = new byte[this.cbData];
Marshal.Copy(this.lpData, arr, 0, arr.Length);
return System.Text.Encoding.GetEncoding(1251).GetString(arr);
}
set
{
byte[] arr = System.Text.Encoding.GetEncoding(1251).GetBytes(value);
this.cbData = arr.Length;
this.lpData = Marshal.AllocHGlobal(arr.Length);
Marshal.Copy(arr, 0, this.lpData, arr.Length);
}
}
public byte[] Data
{
get
{
byte[] arr = new byte[this.cbData];
Marshal.Copy(this.lpData, arr, 0, arr.Length);
return arr;
}
set
{
this.cbData = value.Length;
this.lpData = Marshal.AllocHGlobal(value.Length);
Marshal.Copy(value, 0, this.lpData, value.Length);
}
}
}
/// <summary>
/// Sends the specified message to a window or windows.
/// </summary>
/// <param name="hWnd">A handle to the window whose window procedure will receive the message.
/// If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all top-level
/// windows in the system.</param>
/// <param name="Msg">The message to be sent.</param>
/// <param name="wParam">Additional message-specific information.</param>
/// <param name="lParam">Additional message-specific information.</param>
/// <returns>The return value specifies the result of the message processing;
/// it depends on the message sent.</returns>
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
public static void SendData(IntPtr destHandle, IntPtr srcHandle, int dataType, string data)
{
byte[] arr = System.Text.Encoding.UTF8.GetBytes(data);
COPYDATASTRUCT copyData = new COPYDATASTRUCT();
copyData.dwData = new IntPtr(dataType);
copyData.DataUTF8String = data;
IntPtr ptrCopyData = Marshal.AllocCoTaskMem(Marshal.SizeOf(copyData));
Marshal.StructureToPtr(copyData, ptrCopyData, false);
SendMessage(destHandle, WM_COPYDATA, srcHandle, ptrCopyData);
}
public static void SendData(IntPtr destHandle, IntPtr srcHandle, int dataType, byte[] data)
{
COPYDATASTRUCT copyData = new COPYDATASTRUCT();
copyData.dwData = new IntPtr(dataType);
copyData.Data = data;
IntPtr ptrCopyData = Marshal.AllocCoTaskMem(Marshal.SizeOf(copyData));
Marshal.StructureToPtr(copyData, ptrCopyData, false);
SendMessage(destHandle, WM_COPYDATA, srcHandle, ptrCopyData);
}
}
}