Listing 2 P/Invoke declarations for accessing DDE
using System; using System.Runtime.InteropServices; namespace Jitsu.Dde { internal sealed class Win32 { private Win32() {} // suppress default public ctor for static class // // Managed wrappers for Win32 functions public static ushort GlobalAddAtom(string s) { ushort r = PInvoke.GlobalAddAtom(s); if (r == 0) throw new System.ComponentModel.Win32Exception(); return r; } public static void UnpackDDElParam( int msg, IntPtr lParam, out IntPtr loword, out IntPtr hiword) { bool b = PInvoke.UnpackDDElParam(msg,lParam,out loword, out hiword); if (!b) throw new DdeException("UnpackDDElParam failed."); } public static void FreeDDElParam(int msg, IntPtr lParam) { bool b = PInvoke.FreeDDElParam(msg,lParam); if (!b) throw new DdeException("FreeDDElParam failed."); } public static void PostMessage( IntPtr hwnd, Message msg, IntPtr wParam, IntPtr lParam) { bool b = PInvoke.PostMessage(hwnd,(int)msg,wParam,lParam); if (!b) throw new System.ComponentModel.Win32Exception(); } // // Nested class for extern Win32 declarations [SuppressUnmanagedCodeSecurity] public sealed class Pinvoke { private PInvoke() {} // suppress default public ctor for static class [DllImport("User32.dll")] public static extern bool IsWindowUnicode(IntPtr hwnd); [DllImport("Kernel32.dll", SetLastError=true)] public static extern ushort GlobalAddAtom(string s); [DllImport("Kernel32.dll")] public static extern void GlobalDeleteAtom(ushort atom); [DllImport("User32.dll")] public static extern IntPtr PackDDElParam( int msg, IntPtr loword, IntPtr hiword); [DllImport("User32.dll", SetLastError=false)] public static extern bool UnpackDDElParam( int msg, IntPtr lParam, out IntPtr pLo, out IntPtr pHi); [DllImport("User32.dll", SetLastError=false)] public static extern bool FreeDDElParam(int msg, IntPtr lParam); [DllImport("User32.dll")] public static extern IntPtr SendMessage( IntPtr hwnd,int msg, IntPtr wParam, IntPtr lParam); [DllImport("User32.dll", SetLastError=true)] public static extern bool PostMessage( IntPtr hwnd,int msg, IntPtr wParam, IntPtr lParam); } // // Supporting types public enum Message { DdeInitiate = 0x03E0, DdeAck = 0x03E4, DdeExecute = 0x03E8, DdeTerminate = 0x03E1 } } }