首页
社区
课程
招聘
[翻译]程序:怎么样去启动一个进程
2010-12-18 20:23 12540

[翻译]程序:怎么样去启动一个进程

2010-12-18 20:23
12540
有几种方法去启动一个进程:
'_exec()' 属于 (C运行时函数)
'_spawn()' 也属于 (C 运行时函数)
'WinExec()' (Win32 API)
'ShellExecute()' (Shell API)
'ShellExecuteEx()' (Shell API)
'CreateProcess()' (Win32 API)
'CreateProcessAsUser()' (Win32 API)
'CreateProcessWithLogonW()' (Win32 API)
最常见的是'ShellExecute()', ShellExecuteEx()' and 'CreateProcess()',
注意,'WinExec()'是只兼容于16Windows系统,应该不再使用它。
下面的例子将演示如何运用这3个函数在'notepad.exe'里面显示文本文件'c:\example.txt'。
附加参数可以改为自己的DLL的地址
'ShellExecute()'
代码:     
HINSTANCE hInst = ShellExecute(0,                           
                     "open",                      // 执行的操作
                     "c:\\windows\\notepad.exe",  // 应应用程序的名称
                     "c:\\example.txt",           //附加参数,
                      0,                          //默认目录
                                   SW_SHOW);
    if(reinterpret_cast<int>(hInst) <= 32)
    {
      //无法启动应用程序
      switch(reinterpret_cast<int>(hInst))
      {
        case 0:
          //操作系统内存不足或资源.
          break;
        case ERROR_FILE_NOT_FOUND:
          // 指定文件没有找到,.
          break;
        case ERROR_PATH_NOT_FOUND:
          // 指定路径没有找到.
          break;
        case ERROR_BAD_FORMAT:
          // .EXE文件是无效文件 (不是-Microsoft Win32 .exe 或错误的.exe映像).
          break;
        case SE_ERR_ACCESSDENIED:
          // 操作系统拒绝访问指定文件.
          break;
        case SE_ERR_ASSOCINCOMPLETE:
          // 文件名称不完整或无效.
          break;
        case SE_ERR_DDEBUSY:
          // 动态数据(DDE)处理可能无法完成         
// 因为别的DDE在处理队列中.
          break;
        case SE_ERR_DDEFAIL:
          // DDE 处理失败.
          break;
        case SE_ERR_DDETIMEOUT:
          // DDE 处理可能无法完成请求超时.
          break;
        case SE_ERR_DLLNOTFOUND:
          // 指定动态链接库(DLL) 没有找到.
        case SE_ERR_FNF:
          // 指定文件没有找到.
          break;
        case SE_ERR_NOASSOC:
          // 没有相应的文件具有指定扩展名.
          // 如果你尝试去打印文件那么这个错误将同样是返回
          // not printable.
          break;
        case SE_ERR_OOM:
          // 没有足够的内存来完成操作.
          break;
        case SE_ERR_PNF:
          // 指定路径没有找到.
          break;
        case SE_ERR_SHARE:
          // 发生共享冲突.
          break;
      }
    }
'ShellExecuteEx()'

代码:
    SHELLEXECUTEINFO ExecuteInfo;
    memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
    ExecuteInfo.cbSize       = sizeof(ExecuteInfo);
    ExecuteInfo.fMask        = 0;               
    ExecuteInfo.hwnd         = 0;               
    ExecuteInfo.lpVerb       = "open";        // 执行的操作
    ExecuteInfo.lpFile       = "c:\\windows\\notepad.exe";  // 应用程序名称
   ExecuteInfo.lpParameters = "c:\\example.txt";  // 附加参数
   ExecuteInfo.lpDirectory  = 0;                // 默认的目录
   ExecuteInfo.nShow        = SW_SHOW;
   ExecuteInfo.hInstApp     = 0;
    if(ShellExecuteEx(&ExecuteInfo) == FALSE)
      // 如果不能启动应用程序 -> call 'GetLastError()'

'CreateProcess()'

代码:

    STARTUPINFO         siStartupInfo;
    PROCESS_INFORMATION piProcessInfo;
    memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    siStartupInfo.cb = sizeof(siStartupInfo);
    if(CreateProcess("c:\\windows\\notepad.exe", // 应用程序名称
                     " example.txt",    // 附加参数
                     0,
                     0,
                     FALSE,
                     CREATE_DEFAULT_ERROR_MODE,
                     0,
                     0,                  //工作的目录
                     &siStartupInfo,
                     &piProcessInfo) == FALSE)
      //如果不能启动应用程序 -> call 'GetLastError()'
在一般情况下 'ShellExecuteEx()' and 甚至 'CreateProcess()' 提供更多的应用程序的启动和应用程序的终止控制特征,如图所示 在FAQ中'How can I wait until a process ends?'
[Andreas Masur]
来源e: http://www.codeguru.com/forum/showthrea ... did=231233

[培训]《安卓高级研修班(网课)》月薪三万计划,掌 握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
点赞6
打赏
分享
最新回复 (14)
雪    币: 276
活跃值: (709)
能力值: ( LV15,RANK:520 )
在线值:
发帖
回帖
粉丝
邓韬 9 2010-12-18 20:24
2
0
There are several ways to start a process:

'_exec()' family (C run-time library)

'_spawn()' family (C run-time library)

'WinExec()' (Win32 API)

'ShellExecute()' (Shell API)

'ShellExecuteEx()' (Shell API)

'CreateProcess()' (Win32 API)

'CreateProcessAsUser()' (Win32 API)

'CreateProcessWithLogonW()' (Win32 API)

Most common are 'ShellExecute()', ShellExecuteEx()' and 'CreateProcess()'. Note that 'WinExec()' is provided only for compatibility with 16-bit Windows and should not be used any longer. The following examples will show how to display the text file 'c:\example.txt' in 'notepad.exe' using these three functions...

'ShellExecute()'

code:

    HINSTANCE hInst = ShellExecute(0,                           
                                   "open",                      // Operation to perform
                                   "c:\\windows\\notepad.exe",  // Application name
                                   "c:\\example.txt",           // Additional parameters
                                   0,                           // Default directory
                                   SW_SHOW);
    if(reinterpret_cast<int>(hInst) <= 32)
    {
      // Could not start application
      switch(reinterpret_cast<int>(hInst))
      {
        case 0:
          // The operating system is out of memory or resources.
          break;
        case ERROR_FILE_NOT_FOUND:
          // The specified file was not found.
          break;
        case ERROR_PATH_NOT_FOUND:
          // The specified path was not found.
          break;
        case ERROR_BAD_FORMAT:
          // The .exe file is invalid (non-Microsoft Win32 .exe or error in .exe image).
          break;
        case SE_ERR_ACCESSDENIED:
          // The operating system denied access to the specified file.
          break;
        case SE_ERR_ASSOCINCOMPLETE:
          // The file name association is incomplete or invalid.
          break;
        case SE_ERR_DDEBUSY:
          // The Dynamic Data Exchange (DDE) transaction could not be completed because
          // other DDE transactions were being processed.
          break;
        case SE_ERR_DDEFAIL:
          // The DDE transaction failed.
          break;
        case SE_ERR_DDETIMEOUT:
          // The DDE transaction could not be completed because the request timed out.
          break;
        case SE_ERR_DLLNOTFOUND:
          // The specified dynamic-link library (DLL) was not found.
        case SE_ERR_FNF:
          // The specified file was not found.
          break;
        case SE_ERR_NOASSOC:
          // There is no application associated with the given file name extension.
          // This error will also be returned if you attempt to print a file that is
          // not printable.
          break;
        case SE_ERR_OOM:
          // There was not enough memory to complete the operation.
          break;
        case SE_ERR_PNF:
          // The specified path was not found.
          break;
        case SE_ERR_SHARE:
          // A sharing violation occurred.
          break;
      }
    }
  

'ShellExecuteEx()'

code:

    SHELLEXECUTEINFO ExecuteInfo;
   
    memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
   
    ExecuteInfo.cbSize       = sizeof(ExecuteInfo);
    ExecuteInfo.fMask        = 0;               
    ExecuteInfo.hwnd         = 0;               
    ExecuteInfo.lpVerb       = "open";                      // Operation to perform
    ExecuteInfo.lpFile       = "c:\\windows\\notepad.exe";  // Application name
    ExecuteInfo.lpParameters = "c:\\example.txt";           // Additional parameters
    ExecuteInfo.lpDirectory  = 0;                           // Default directory
    ExecuteInfo.nShow        = SW_SHOW;
    ExecuteInfo.hInstApp     = 0;
   
    if(ShellExecuteEx(&ExecuteInfo) == FALSE)
      // Could not start application -> call 'GetLastError()'
  

'CreateProcess()'

code:

    STARTUPINFO         siStartupInfo;
    PROCESS_INFORMATION piProcessInfo;
    memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    siStartupInfo.cb = sizeof(siStartupInfo);
    if(CreateProcess("c:\\windows\\notepad.exe",     // Application name
                     " example.txt",                 // Application arguments
                     0,
                     0,
                     FALSE,
                     CREATE_DEFAULT_ERROR_MODE,
                     0,
                     0,                              // Working directory
                     &siStartupInfo,
                     &piProcessInfo) == FALSE)
      // Could not start application -> call 'GetLastError()'
  

In general 'ShellExecuteEx()' and even 'CreateProcess()' provides more controlling features of the application to start and its termination as shown in the FAQ 'How can I wait until a process ends?'

[Andreas Masur]
雪    币: 287
活跃值: (498)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
dkxzl 1 2010-12-19 19:02
3
0
感觉楼主就像在翻译 MSDN ,楼主牛B,在翻译区上传高质量翻译文章,勇气可嘉
雪    币: 908
活跃值: (4110)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
kagayaki 2010-12-19 19:11
4
0
帮你 D 一下..................

这样也是学习的一种方法(先学基本原理, 高手也是这样来的)
雪    币: 276
活跃值: (709)
能力值: ( LV15,RANK:520 )
在线值:
发帖
回帖
粉丝
邓韬 9 2010-12-19 20:24
5
0
哎!没办法啊,小弟没上大学的资质,只有这样才能超过你们啊。各位大哥现在大几,多少岁?我现在18岁.9月1号满的,等我到你们那么大时我相信一定可以超过你们哈哈
雪    币: 120
活跃值: (55)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
牛皮哄哄 2010-12-19 20:52
6
0
你的翻译按字面来的啊,Chinese版English?你那边到底什么情况啊那么古怪?
多翻翻老鸟的帖子,学加解密不一定非要从翻译开始嘛!年龄推算后 倒是挺有戏
雪    币: 276
活跃值: (709)
能力值: ( LV15,RANK:520 )
在线值:
发帖
回帖
粉丝
邓韬 9 2010-12-20 20:00
7
0
哈哈!谢谢教育,记住了
雪    币: 2743
活跃值: (1049)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
zysyyz 1 2010-12-20 20:54
8
0
推荐《薄冰英语》,个人觉得还不错……想当初上高中那会儿,一天要背30个单词,英语周报一周一份翻烂为止……不一定要读大学才学得好英语,我高中时的英语水平看懂一部英文的计算机著作就差不多OK了(当然牛津词典要准备好——)——只要你肯吃苦的话,英文水平一定能提高
雪    币: 2743
活跃值: (1049)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
zysyyz 1 2010-12-20 21:06
9
0
想了一下,还是推荐你去学阿拉伯语把——呵呵,没错,我就是有私心,到过很多阿拉伯语网站,有很多很好的资源,苦于自己目不识丁……
雪    币: 216
活跃值: (144)
能力值: ( LV10,RANK:160 )
在线值:
发帖
回帖
粉丝
zouzhiyong 3 2010-12-20 22:00
10
0
支持LZ,其实根本没有年龄的界限~~~,都是对技术报有热情的~~
雪    币: 78
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
whypro 2010-12-22 19:00
11
0
苦海慈航度大千
雪    币: 45
活跃值: (25)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
dwboy 2011-2-23 16:15
12
0
LS的dkxzl和你一样大,超越去吧
雪    币: 220
活跃值: (15)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
广海混沌 2011-2-23 16:25
13
0
呵呵 我得把这些记住才行。
雪    币: 179
活跃值: (134)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
forget 2011-12-12 15:58
14
0
windows 核心编程。楼主年轻就是优式啊。
雪    币: 51
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
zhaohtao 2011-12-12 16:58
15
0
年龄永远说明不了什么,学无先后,达者为师!
游客
登录 | 注册 方可回帖
返回