Exemplo n.º 1
0
        /// <summary>
        /// 初期化を行います。
        /// </summary>
        /// <param name="hookType">インストールするフック手順のタイプ。</param>
        protected virtual void Initialize(int hookType, Delegate callback, Type callNextHook)
        {
            // エミッター生成
            emitter = new UnmanagedFunction("user32.dll");

            // Hook
            this.callNextHook = emitter.CreateFunction(callNextHook);

            // コールバック関数
            @delegate = GCHandle.Alloc(callback);

            // モジュールポインタ
            Module = IntPtr.Zero;
            Handle = API.SetWindowsHookEx(hookType, Marshal.GetFunctionPointerForDelegate(callback), Module, 0);
        }
        public static void GetCurrentProcessId()
        {
            var kernel32 = NativeLibrary.Load("kernel32.dll");

            NotEqual(IntPtr.Zero, kernel32);
            try
            {
                var getCurrentProcessId = NativeLibrary.GetExport(kernel32, "GetCurrentProcessId");
                NotEqual(IntPtr.Zero, getCurrentProcessId);
                using var currentProcess = Process.GetCurrentProcess();
                Equal(currentProcess.Id, UnmanagedFunction <int> .StdCall(getCurrentProcessId));
            }
            finally
            {
                NativeLibrary.Free(kernel32);
            }
        }