Beispiel #1
0
        /// <summary>
        /// ネイティブのDLLメソッドを対象プロセスに実行させる。
        /// </summary>
        /// <param name="processHandle">プロセスのハンドル。</param>
        /// <param name="dllPath">DLL。</param>
        /// <param name="function">関数名称。</param>
        /// <param name="args">引数。</param>
        /// <returns>実行したメソッドの戻り値</returns>
        internal static uint ExecuteRemoteFunction(IntPtr processHandle, string dllPath, string function, string args)
        {
            IntPtr pArgs           = IntPtr.Zero;
            IntPtr hThreadInTarget = IntPtr.Zero;
            uint   resultVlaue     = 0;

            try
            {
                //引数を相手プロセスに書き込む
                List <byte> startInfoTmp = new List <byte>(Encoding.Unicode.GetBytes(args));
                startInfoTmp.Add(0);//null終端を足す
                byte[] startInfo = startInfoTmp.ToArray();
                pArgs = NativeMethods.VirtualAllocEx(processHandle, IntPtr.Zero, new IntPtr(startInfo.Length),
                                                     NativeMethods.AllocationType.Commit, NativeMethods.MemoryProtection.ReadWrite);
                if (pArgs == IntPtr.Zero ||
                    !NativeMethods.WriteProcessMemory(processHandle, pArgs, startInfo, new IntPtr((int)startInfo.Length), IntPtr.Zero))
                {
                    throw new FriendlyOperationException(ResourcesLocal.Instance.ErrorProcessOperation);
                }

                {
                    //実行関数取得
                    IntPtr pFunc = DllInjector.GetTargetProcAddress(processHandle, dllPath, function);
                    if (pFunc == IntPtr.Zero)
                    {
                        throw new FriendlyOperationException(ResourcesLocal.Instance.ErrorFriendlySystem);
                    }

                    //対象プロセスでメソッドを実行
                    IntPtr tid;
                    hThreadInTarget = NativeMethods.CreateRemoteThread(processHandle, IntPtr.Zero, IntPtr.Zero,
                                                                       pFunc, pArgs, 0, out tid);
                    if (hThreadInTarget == IntPtr.Zero)
                    {
                        throw new FriendlyOperationException(ResourcesLocal.Instance.ErrorProcessOperation);
                    }
                    NativeMethods.WaitForSingleObject(hThreadInTarget, NativeMethods.INFINITE);

                    //戻り値を取得
                    if (NativeMethods.GetExitCodeThread(hThreadInTarget, out var ret))
                    {
                        resultVlaue = ret;
                    }
                }
            }
            finally
            {
                if (hThreadInTarget != IntPtr.Zero)
                {
                    NativeMethods.CloseHandle(hThreadInTarget);
                }
                if (pArgs != IntPtr.Zero)
                {
                    NativeMethods.VirtualFreeEx(processHandle, pArgs,
                                                IntPtr.Zero, NativeMethods.FreeType.Release);
                }
            }
            return(resultVlaue);
        }
Beispiel #2
0
        /// <summary>
        /// ネイティブのDLLメソッドを対象プロセスに実行させる。
        /// </summary>
        /// <param name="processHandle">プロセスのハンドル。</param>
        /// <param name="dllPath">DLL。</param>
        /// <param name="function">関数名称。</param>
        /// <param name="args">引数。</param>
        internal static void ExecuteRemoteFunction(IntPtr processHandle, string dllPath, string function, string args)
        {
            IntPtr pArgs             = IntPtr.Zero;
            IntPtr hThreadServerOpen = IntPtr.Zero;

            try
            {
                //受信ウィンドウハンドルを対象プロセス内にメモリを確保して書き込む longを文字列化して書き込む
                List <byte> startInfoTmp = new List <byte>(Encoding.Unicode.GetBytes(args));
                startInfoTmp.Add(0);//null終端を足す
                byte[] startInfo = startInfoTmp.ToArray();
                pArgs = NativeMethods.VirtualAllocEx(processHandle, IntPtr.Zero, new IntPtr(startInfo.Length),
                                                     NativeMethods.AllocationType.Commit, NativeMethods.MemoryProtection.ReadWrite);
                if (pArgs == IntPtr.Zero ||
                    !NativeMethods.WriteProcessMemory(processHandle, pArgs, startInfo, new IntPtr((int)startInfo.Length), IntPtr.Zero))
                {
                    throw new FriendlyOperationException(ResourcesLocal.Instance.ErrorProcessOperation);
                }

                {
                    //実行関数取得
                    IntPtr pFunc = DllInjector.GetTargetProcAddress(processHandle, dllPath, function);
                    if (pFunc == IntPtr.Zero)
                    {
                        throw new FriendlyOperationException(ResourcesLocal.Instance.ErrorFriendlySystem);
                    }

                    //対象プロセスでサーバー開始メソッドを実行
                    IntPtr tid;
                    hThreadServerOpen = NativeMethods.CreateRemoteThread(processHandle, IntPtr.Zero, IntPtr.Zero,
                                                                         pFunc, pArgs, 0, out tid);
                    if (hThreadServerOpen == IntPtr.Zero)
                    {
                        throw new FriendlyOperationException(ResourcesLocal.Instance.ErrorProcessOperation);
                    }
                    NativeMethods.WaitForSingleObject(hThreadServerOpen, NativeMethods.INFINITE);
                }
            }
            finally
            {
                if (hThreadServerOpen != IntPtr.Zero)
                {
                    NativeMethods.CloseHandle(hThreadServerOpen);
                }
                if (pArgs != IntPtr.Zero)
                {
                    NativeMethods.VirtualFreeEx(processHandle, pArgs,
                                                IntPtr.Zero, NativeMethods.FreeType.Release);
                }
            }
        }