Ejemplo n.º 1
0
        /// <summary>
        /// 注入程序集到指定进程
        /// </summary>
        /// <param name="processId">进程id</param>
        /// <param name="assembly">程序集路径</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="FileNotFoundException"></exception>
        /// <returns></returns>
        public static bool InjectToProcess(int processId, string assembly)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            if (File.Exists(assembly) == false)
            {
                throw new FileNotFoundException("找不到文件" + assembly);
            }

            var mainDll = Path.GetFullPath("Main.dll");

            if (File.Exists(mainDll) == false)
            {
                throw new FileNotFoundException("找不到文件Main.dll");
            }

            lock (Inject.SyncRoot)
            {
                Extern.SetAssembly(Path.GetFullPath(assembly));
                if (Extern.CreateRemoteThreadWithDll(processId, Path.GetFullPath(mainDll)))
                {
                    Extern.WaitForLoadAssembly(MaxWaitTime);
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建应用进程并注入程序集
        /// </summary>
        /// <param name="fileName">进程文件名</param>
        /// <param name="argument">启动参数</param>
        /// <param name="workDir">工作路径</param>
        /// <param name="assembly">程序集路径</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="FileNotFoundException"></exception>
        /// <returns></returns>
        public static bool InjectToProcess(string fileName, string argument, string workDir, string assembly)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            if (File.Exists(fileName) == false)
            {
                throw new FileNotFoundException("找不到文件" + fileName);
            }

            if (File.Exists(assembly) == false)
            {
                throw new FileNotFoundException("找不到文件" + assembly);
            }

            var mainDll = "Main.dll";

            if (File.Exists(mainDll) == false)
            {
                throw new FileNotFoundException("找不到文件Main.dll");
            }

            lock (Inject.SyncRoot)
            {
                Extern.SetAssembly(Path.GetFullPath(assembly));
                if (Extern.CreateProcessWithDll(fileName, argument, workDir, Path.GetFullPath(mainDll)))
                {
                    Extern.WaitForLoadAssembly(MaxWaitTime);
                    return(true);
                }
            }
            return(false);
        }