using System; using System.Runtime.InteropServices; namespace DelftTools.Utils.Interop { /// /// Manages registration of external (windows) libraries /// public class OcxHelper { /// /// Registers ocx with windows /// /// /// public static bool Register(string ocxPath) { return (Register(ocxPath, true)); } /// /// Deregisters ocx from windows /// /// /// public static bool UnRegister(string ocxPath) { return (Register(ocxPath, false)); } [DllImport("kernel32.dll")] private static extern bool FreeLibrary(IntPtr hModule); [DllImport("kernel32.dll")] private static extern IntPtr LoadLibrary(string lpFilename); [DllImport("kernel32.dll")] private static extern UIntPtr GetProcAddress(IntPtr hModule, string lpProcName); [DllImport("user32.dll")] private static extern IntPtr CallWindowProc(UIntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam); private static bool Register(string ocxPath, bool register) { IntPtr lb = IntPtr.Zero; UIntPtr pa; try { lb = LoadLibrary(ocxPath); if (register) { pa = GetProcAddress(lb, "DllRegisterServer"); } else { pa = GetProcAddress(lb, "DllUnregisterServer"); } if (CallWindowProc(pa, IntPtr.Zero, 0, UIntPtr.Zero, IntPtr.Zero) == IntPtr.Zero) { return (true); } else { return (false); } } catch (Exception e) { throw (e); } finally { FreeLibrary(lb); } } } }