Windows 核心编程 -- 读书笔记

CreateProcess函数

一个线程调用CreateProcess时,系统将创建一个进程内核对象,其初始使用计数为1。然后新进程创建一个虚拟地址空间,并将可执行文件(和所有必要的DLL)的代码及数据加载到进程的地址空间。然后,系统为新进程的主线程创建一个线程内核对象。如果系统成功创建了新进程和主线程,CreateProcess将返回TRUE.

 

CreateProcess函数原型:

BOOL CreateProcess(

     PCTSTR pszApplicationName,  // 指定进程使用的可执行文件的名称

     PTSTR  pszCommandLine,       // 传给新进程的命令行字符串。

     PSECURITY_ATTRIBUTES psaProcess,

     PSECURITY_ATTRIBUTES psaThread,

     BOOL bInheritHandles,

     DWORD fdwCreate,

     PVOID pvEnviroment,

     PCTSTR pszCurDir,

     PSTARTUPINFO psiStartInfo,

     PPROCESS_INFORMATION ppiProcInfo);

 

 

    先看pszCommandLine 参数的类型为PTSTR。这意味着CreateProcess期望我们传入的是一个非"常量字符串"的地址。在内部,CreateProcess实际上会修改我们传给它的命令行字符串。但在CreateProcess返回之前,它会将这个字符串还原为原来的形式。

如果命令行字符串包含在文件映像的只读部分,就会引起访问违规。例如,以下代码就会导致访问违规,因为Microsoft的c/c++编译器把“NOTEPAD”字符串放在只读内存中:

STARTUPINFO si = { sizeof(si) };

PROCESS_INFORMATION pi;

CreateProcess(NULL, TEXT("NOTEPAD"), NULL, NULL,FALSE, 0,  NULL, NULL,&si, &pi);

 

解决这个问题的最佳方式是在调用CreateProcess之前,把常量字符复制到一个临时缓冲区,如下所示:

STARTUPINFO si = { sizeof(si) }

PROCESS_INFORMATION pi;

TCHAR szCommandLine[] = TEXT("NOTEPAD");

CreateProcess(NULL, szCommandLine, NULL, NULL, false, 0, NULL, NULL, &si, &pi);

 

顺便提一下, 如果在Windwos Vista 中调用CreateProcess函数的ANSI版本,是不会发生访问违规的, 因为它会为命令行字符串创建一个临时副本。

 

当CreateProcess解析pszCommandLine时,如果可执行文件的名称没有扩展名,就会默认是。exe扩展名。CreateProcess还会按照以下顺序搜索可执行文件。

(1) 主调进程.EXE文件所在目录。

(2) 主调进程的当前目录。

(3) Windwos系统目录,即GetSystemDirectory返回的Systems子文件夹

(4) Windows目录

(5) PATH环境变量中列出的目录。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

 

只要pszApplicationName参数为NULL,就会发生上述问题。当pszApplicationName传进一个字符串地址,并在字符串地址中包含所要运行的可执行文件的名称。在这种情况下,必须指定文件扩展名,系统不会自动假定文件名有一个.exe扩展名。CreateProcess假定文件位于当前目录。如果在当前路径下没找到,就不会再其他任何目录查找文件--调用会以失败告终。

然而,即使在pszApplicationName参数中指定了文件名, CreateProcess也会将pszCommandLine参数中的内容作为新进程的命令行传给它。例如:

TCHAR szPath[] = TEXT("WORDPAD README.TXT");

CreateProcess(TEXT("c://WINDOWS//SYSTEM32//NOTEPAD.EXE"), szPath,...);

你可能感兴趣的:(编程,windows,Microsoft,null,扩展,attributes)