NativeMethods.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Runtime.InteropServices;
 7  
 8  namespace EnvironmentVariables.Win32
 9  {
10      public static class NativeMethods
11      {
12          internal const int HWND_BROADCAST = 0xffff;
13  
14          internal delegate IntPtr WinProc(IntPtr hWnd, WindowMessage msg, IntPtr wParam, IntPtr lParam);
15  
16          [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
17          internal static extern int SendNotifyMessage(IntPtr hWnd, WindowMessage msg, IntPtr wParam, IntPtr lParam);
18  
19          [DllImport("User32.dll")]
20          internal static extern int GetDpiForWindow(IntPtr hwnd);
21  
22          [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
23          internal static extern int SetWindowLong32(IntPtr hWnd, WindowLongIndexFlags nIndex, WinProc newProc);
24  
25          [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
26          internal static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, WindowLongIndexFlags nIndex, WinProc newProc);
27  
28          [DllImport("user32.dll")]
29          internal static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, WindowMessage msg, IntPtr wParam, IntPtr lParam);
30  
31          [Flags]
32          internal enum WindowLongIndexFlags : int
33          {
34              GWL_WNDPROC = -4,
35          }
36  
37          internal enum WindowMessage : int
38          {
39              WM_SETTINGSCHANGED = 0x001A,
40          }
41  
42          internal static IntPtr SetWindowLongPtr(IntPtr hWnd, WindowLongIndexFlags nIndex, WinProc newProc)
43          {
44              if (IntPtr.Size == 8)
45              {
46                  return SetWindowLongPtr64(hWnd, nIndex, newProc);
47              }
48              else
49              {
50                  return new IntPtr(SetWindowLong32(hWnd, nIndex, newProc));
51              }
52          }
53      }
54  }