bovender framework
C# framework that implements MVVM and more
Pinvoke.cs
1 /* Pinvoke.cs
2  * part of Daniel's XL Toolbox NG
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 using System;
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Linq;
22 using System.Runtime.InteropServices;
23 using System.Text;
24 
25 namespace Bovender.Unmanaged
26 {
30  public static class Pinvoke
31  {
32  #region Public methods
33 
34  public static void OpenClipboard(IntPtr hWndNewOwner)
35  {
36  int attempts = 0;
37  bool opened = Win32_OpenClipboard(hWndNewOwner);
38  while (!opened && attempts < CLIPBOARD_MAX_ATTEMPTS)
39  {
40  attempts++;
41  System.Threading.Thread.Sleep(CLIPBOARD_WAIT_MS * attempts);
42  opened = Win32_OpenClipboard(hWndNewOwner);
43  }
44  if (!opened && !Win32_OpenClipboard(hWndNewOwner))
45  {
46  // Compute total duration: https://en.wikipedia.org/wiki/Triangular_number
47  string s = String.Format(
48  "Unable to get clipboard access; it is still locked by another application even after {0} attempts over {1:0.0} seconds",
49  attempts, CLIPBOARD_WAIT_MS * attempts * (attempts + 1) / 2 / 1000);
50  Logger.Fatal(s);
51  throw new Win32Exception(Marshal.GetLastWin32Error(), s);
52  }
53  }
54 
55  public static void CloseClipboard()
56  {
57  Win32_CloseClipboard();
58  }
59 
60  public static IntPtr GetClipboardData(uint uFormat)
61  {
62  IntPtr result = Win32_GetClipboardData(uFormat);
63  if (result == IntPtr.Zero)
64  {
65  throw new Win32Exception(Marshal.GetLastWin32Error());
66  }
67  return result;
68  }
69 
70  public static IntPtr CopyEnhMetaFile(IntPtr hemfSrc, string lpszFile)
71  {
72  IntPtr result = Win32_CopyEnhMetaFile(hemfSrc, lpszFile);
73  if (result == IntPtr.Zero)
74  {
75  throw new Win32Exception(Marshal.GetLastWin32Error());
76  }
77  return result;
78  }
79 
80  public static void DeleteEnhMetaFile(IntPtr hemf)
81  {
82  Win32_DeleteEnhMetaFile(hemf);
83  }
84 
85  public static string GetColorDirectory()
86  {
87  uint bufSize = 260; // MAX_PATH
88  StringBuilder sb = new StringBuilder((int)bufSize);
89  if (Win32_GetColorDirectory(IntPtr.Zero, sb, ref bufSize))
90  {
91  return sb.ToString();
92  }
93  else
94  {
95  return String.Empty;
96  }
97  }
98 
99  public static IntPtr FindWindow(string className)
100  {
101  return FindWindow(className, null);
102  }
103 
104  #endregion
105 
106  #region Win32 API constants
107 
108  public const uint CF_ENHMETAFILE = 14;
109 
110  #endregion
111 
112  #region Win32 DLL imports
113 
114  [DllImport("user32.dll", EntryPoint = "OpenClipboard", SetLastError = true)]
115  static extern bool Win32_OpenClipboard(IntPtr hWndNewOwner);
116 
117  [DllImport("user32.dll", EntryPoint = "CloseClipboard", SetLastError = true)]
118  static extern bool Win32_CloseClipboard();
119 
120  [DllImport("user32.dll", EntryPoint = "GetClipboardOwner", SetLastError = true)]
121  static extern IntPtr Win32_GetClipboardOwner();
122 
123  [DllImport("user32.dll", EntryPoint = "GetClipboardData", SetLastError = true)]
124  static extern IntPtr Win32_GetClipboardData(uint uFormat);
125 
126  [DllImport("gdi32.dll", EntryPoint = "CopyEnhMetaFile", SetLastError = true)]
127  static extern IntPtr Win32_CopyEnhMetaFile(IntPtr hemfSrc, string lpszFile);
128 
129  [DllImport("gdi32.dll", EntryPoint = "DeleteEnhMetaFile", SetLastError = true)]
130  static extern bool Win32_DeleteEnhMetaFile(IntPtr hemf);
131 
132  [DllImport("mscms.dll", EntryPoint = "GetColorDirectory", SetLastError = true,
133  CharSet = CharSet.Auto, BestFitMapping = false)]
134  static extern bool Win32_GetColorDirectory(IntPtr pMachineName, StringBuilder pBuffer,
135  ref uint pdwSize);
136 
178  // For Windows Mobile, replace user32.dll with coredll.dll
179  [DllImport("user32.dll", SetLastError = true)]
180  static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
181 
182  // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
183  [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
184  static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
185 
186  // You can also call FindWindow(default(string), lpWindowName) or FindWindow((string)null, lpWindowName)
187  #endregion
188 
189  #region Private constants
190 
191  private const int CLIPBOARD_MAX_ATTEMPTS = 5;
192  private const int CLIPBOARD_WAIT_MS = 200;
193 
194  #endregion
195 
196  #region Class logger
197 
198  private static NLog.Logger Logger { get { return _logger.Value; } }
199 
200  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
201 
202  #endregion
203  }
204 }
Wrappers for Win32 API calls.
Definition: Pinvoke.cs:30