SingleInstaceManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6. namespace PaintDotNet.SystemLayer
  7. {
  8. /// <summary>
  9. /// Provides a way to manage and communicate between instances of an application
  10. /// in the same user session.
  11. /// </summary>
  12. public sealed class SingleInstanceManager : IDisposable
  13. {
  14. private const int mappingSize = 8; // sizeof(int64)
  15. private string mappingName;
  16. private Form window = null;
  17. private IntPtr hWnd = IntPtr.Zero;
  18. private IntPtr hFileMapping;
  19. private List<string> pendingInstanceMessages = new List<string>();
  20. private bool isFirstInstance;
  21. public bool IsFirstInstance
  22. {
  23. get
  24. {
  25. return this.isFirstInstance;
  26. }
  27. }
  28. public bool AreMessagesPending
  29. {
  30. get
  31. {
  32. lock (this.pendingInstanceMessages)
  33. {
  34. return (this.pendingInstanceMessages.Count > 0);
  35. }
  36. }
  37. }
  38. public void SetWindow(Form newWindow)
  39. {
  40. if (this.window != null)
  41. {
  42. UnregisterWindow();
  43. }
  44. RegisterWindow(newWindow);
  45. }
  46. private void UnregisterWindow()
  47. {
  48. if (this.window != null)
  49. {
  50. this.window.HandleCreated -= new EventHandler(Window_HandleCreated);
  51. this.window.HandleDestroyed -= new EventHandler(Window_HandleDestroyed);
  52. this.window.Disposed -= new EventHandler(Window_Disposed);
  53. WriteHandleValueToMappedFile(IntPtr.Zero);
  54. this.hWnd = IntPtr.Zero;
  55. this.window = null;
  56. }
  57. }
  58. private void RegisterWindow(Form newWindow)
  59. {
  60. this.window = newWindow;
  61. if (this.window != null)
  62. {
  63. this.window.HandleCreated += new EventHandler(Window_HandleCreated);
  64. this.window.HandleDestroyed += new EventHandler(Window_HandleDestroyed);
  65. this.window.Disposed += new EventHandler(Window_Disposed);
  66. if (this.window.IsHandleCreated)
  67. {
  68. this.hWnd = this.window.Handle;
  69. WriteHandleValueToMappedFile(this.hWnd);
  70. }
  71. }
  72. GC.KeepAlive(newWindow);
  73. }
  74. private void Window_Disposed(object sender, EventArgs e)
  75. {
  76. UnregisterWindow();
  77. }
  78. private void Window_HandleDestroyed(object sender, EventArgs e)
  79. {
  80. UnregisterWindow();
  81. }
  82. private void Window_HandleCreated(object sender, EventArgs e)
  83. {
  84. this.hWnd = this.window.Handle;
  85. WriteHandleValueToMappedFile(this.hWnd);
  86. GC.KeepAlive(this.window);
  87. }
  88. public string[] GetPendingInstanceMessages()
  89. {
  90. string[] messages;
  91. lock (this.pendingInstanceMessages)
  92. {
  93. messages = this.pendingInstanceMessages.ToArray();
  94. this.pendingInstanceMessages.Clear();
  95. }
  96. return messages;
  97. }
  98. public event EventHandler InstanceMessageReceived;
  99. private void OnInstanceMessageReceived()
  100. {
  101. if (InstanceMessageReceived != null)
  102. {
  103. InstanceMessageReceived(this, EventArgs.Empty);
  104. }
  105. }
  106. public void SendInstanceMessage(string text)
  107. {
  108. SendInstanceMessage(text, 1);
  109. }
  110. public void SendInstanceMessage(string text, int timeoutSeconds)
  111. {
  112. IntPtr ourHwnd = IntPtr.Zero;
  113. DateTime now = DateTime.Now;
  114. DateTime timeoutTime = DateTime.Now + new TimeSpan(0, 0, 0, timeoutSeconds);
  115. while (ourHwnd == IntPtr.Zero && now < timeoutTime)
  116. {
  117. ourHwnd = ReadHandleFromFromMappedFile();
  118. now = DateTime.Now;
  119. if (ourHwnd == IntPtr.Zero)
  120. {
  121. System.Threading.Thread.Sleep(100);
  122. }
  123. }
  124. if (ourHwnd != IntPtr.Zero)
  125. {
  126. NativeStructs.COPYDATASTRUCT copyDataStruct = new NativeStructs.COPYDATASTRUCT();
  127. IntPtr szText = IntPtr.Zero;
  128. try
  129. {
  130. unsafe
  131. {
  132. szText = Marshal.StringToCoTaskMemUni(text);
  133. copyDataStruct.dwData = UIntPtr.Zero;
  134. copyDataStruct.lpData = szText;
  135. copyDataStruct.cbData = (uint)(2 * (1 + text.Length));
  136. IntPtr lParam = new IntPtr((void*)&copyDataStruct);
  137. SafeNativeMethods.SendMessageW(ourHwnd, NativeConstants.WM_COPYDATA, this.hWnd, lParam);
  138. }
  139. }
  140. finally
  141. {
  142. if (szText != IntPtr.Zero)
  143. {
  144. Marshal.FreeCoTaskMem(szText);
  145. szText = IntPtr.Zero;
  146. }
  147. }
  148. }
  149. }
  150. public void FocusFirstInstance()
  151. {
  152. IntPtr ourHwnd = this.ReadHandleFromFromMappedFile();
  153. if (ourHwnd != IntPtr.Zero)
  154. {
  155. if (SafeNativeMethods.IsIconic(ourHwnd))
  156. {
  157. SafeNativeMethods.ShowWindow(ourHwnd, NativeConstants.SW_RESTORE);
  158. }
  159. SafeNativeMethods.SetForegroundWindow(ourHwnd);
  160. }
  161. }
  162. public void FilterMessage(ref Message m)
  163. {
  164. if (m.Msg == NativeConstants.WM_COPYDATA)
  165. {
  166. unsafe
  167. {
  168. NativeStructs.COPYDATASTRUCT* pCopyDataStruct = (NativeStructs.COPYDATASTRUCT*)m.LParam.ToPointer();
  169. string message = Marshal.PtrToStringUni(pCopyDataStruct->lpData);
  170. lock (this.pendingInstanceMessages)
  171. {
  172. this.pendingInstanceMessages.Add(message);
  173. }
  174. OnInstanceMessageReceived();
  175. }
  176. }
  177. }
  178. public SingleInstanceManager(string moniker)
  179. {
  180. int error = NativeConstants.ERROR_SUCCESS;
  181. if (moniker.IndexOf('\\') != -1)
  182. {
  183. throw new ArgumentException("moniker must not have a backslash character");
  184. }
  185. this.mappingName = "Local\\" + moniker;
  186. this.hFileMapping = SafeNativeMethods.CreateFileMappingW(
  187. NativeConstants.INVALID_HANDLE_VALUE,
  188. IntPtr.Zero,
  189. NativeConstants.PAGE_READWRITE | NativeConstants.SEC_COMMIT,
  190. 0,
  191. mappingSize,
  192. mappingName);
  193. error = Marshal.GetLastWin32Error();
  194. if (this.hFileMapping == IntPtr.Zero)
  195. {
  196. throw new Win32Exception(error, "CreateFileMappingW() returned NULL (" + error.ToString() + ")");
  197. }
  198. this.isFirstInstance = (error != NativeConstants.ERROR_ALREADY_EXISTS);
  199. }
  200. private void WriteHandleValueToMappedFile(IntPtr hValue)
  201. {
  202. int error = NativeConstants.ERROR_SUCCESS;
  203. bool bResult = true;
  204. IntPtr lpData = SafeNativeMethods.MapViewOfFile(
  205. this.hFileMapping,
  206. NativeConstants.FILE_MAP_WRITE,
  207. 0,
  208. 0,
  209. new UIntPtr((uint)mappingSize));
  210. error = Marshal.GetLastWin32Error();
  211. if (lpData == IntPtr.Zero)
  212. {
  213. throw new Win32Exception(error, "MapViewOfFile() returned NULL (" + error + ")");
  214. }
  215. long int64 = hValue.ToInt64();
  216. byte[] int64Bytes = new byte[(int)mappingSize];
  217. for (int i = 0; i < mappingSize; ++i)
  218. {
  219. int64Bytes[i] = (byte)((int64 >> (i * 8)) & 0xff);
  220. }
  221. Marshal.Copy(int64Bytes, 0, lpData, mappingSize);
  222. bResult = SafeNativeMethods.UnmapViewOfFile(lpData);
  223. error = Marshal.GetLastWin32Error();
  224. if (!bResult)
  225. {
  226. throw new Win32Exception(error, "UnmapViewOfFile() returned FALSE (" + error + ")");
  227. }
  228. }
  229. private IntPtr ReadHandleFromFromMappedFile()
  230. {
  231. int error = NativeConstants.ERROR_SUCCESS;
  232. IntPtr lpData = SafeNativeMethods.MapViewOfFile(
  233. this.hFileMapping,
  234. NativeConstants.FILE_MAP_READ,
  235. 0,
  236. 0,
  237. new UIntPtr((uint)mappingSize));
  238. error = Marshal.GetLastWin32Error();
  239. if (lpData == IntPtr.Zero)
  240. {
  241. throw new Win32Exception(error, "MapViewOfFile() returned NULL (" + error + ")");
  242. }
  243. byte[] int64Bytes = new byte[(int)mappingSize];
  244. Marshal.Copy(lpData, int64Bytes, 0, mappingSize);
  245. long int64 = 0;
  246. for (int i = 0; i < mappingSize; ++i)
  247. {
  248. int64 += (long)(int64Bytes[i] << (i * 8));
  249. }
  250. bool bResult = SafeNativeMethods.UnmapViewOfFile(lpData);
  251. error = Marshal.GetLastWin32Error();
  252. if (!bResult)
  253. {
  254. throw new Win32Exception(error, "UnmapViewOfFile() returned FALSE (" + error + ")");
  255. }
  256. IntPtr hValue = new IntPtr(int64);
  257. return hValue;
  258. }
  259. ~SingleInstanceManager()
  260. {
  261. Dispose(false);
  262. }
  263. public void Dispose()
  264. {
  265. Dispose(true);
  266. GC.SuppressFinalize(this);
  267. }
  268. private void Dispose(bool disposing)
  269. {
  270. if (disposing)
  271. {
  272. UnregisterWindow();
  273. }
  274. if (this.hFileMapping != IntPtr.Zero)
  275. {
  276. SafeNativeMethods.CloseHandle(this.hFileMapping);
  277. this.hFileMapping = IntPtr.Zero;
  278. }
  279. }
  280. }
  281. }