关于CreatProcess中的dwCreationFlags参数

LLVVVVLL 2007-08-16 12:21:49
BOOL CreateProcess(
LPCTSTR lpApplicationName,
LPTSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCTSTR lpCurrentDirectory,
LPSTARTUPINFO lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);

看有不少程序如此调用:
CreateProcess(null,sbCommand,null,null,false,0,null,null,ref sInfo,ref pInfo)
而且似乎dwCreationFlags可以取0~31之间的任意值,请问这究竟是怎么回事,请哪位详细讲讲

...全文
1434 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lic240 2007-08-16
  • 打赏
  • 举报
回复
楼上几位的解释太对了,0-31的范围是线程的优先级值
hohoxcn 2007-08-16
  • 打赏
  • 举报
回复
线程的优先级范围从0(最低)到31(最高)。产生线程时,并不直接以数值指定其优先级,而是采用两个步骤:第一步是指定“优先等级(Priority Class)”,即指定其所属进程的优先级,由CreateProcess()的参数dsCreationFlags指定。也就是说线程的优先级首先从属于其所属进程的优先级,而进程的优先级在线程函数中是无法改变的。第二步,指定“相对优先级”,即调用SetThreadPriority()函数设定线程优先级水平。其中指定“优先级等级”(即创建进程)时,参数dwCreationFlags共有4个等级,如果不指定,系统默认的是NOMAL_PRIORITY_CLASS。
IDLE_PRIORITY_CLASS 只有在CPU时间将被浪费掉时(空闲时间)才执行,适合于系统监视软件、屏保等。
NORMAL_PRIORITY_CLASS 默认等级。当进程变成前台进程时,线程优先级提升为9,进程变成后台时,降为7
HIGH_PRIORITY_CLASS 为了满足立即反应需要,如当使用者按下Ctrl+Esc时立即把工作管理器调出
REALTIME_PRIORITY_CLASS 几乎不会被一般的应用程序使用。这种等级使用在“如果不在某个时间范围内执行数据就要遗失”的情况下。因此,使用要慎重,如果把这样的等级指定给一般的线程,那么多任务环境可能会瘫痪,因为这个线程有如此高的优先级,其他进程再没有机会执行。
  在事先指定优先级等级的情况下,可以使用SetThreadPriority()函数对线程优先级进行微调,其参数如下:
THREAD_PRIORITY_LOWEST 优先级-2
THREAD_PRIORITY_BELOW_NORMAL 优先级-1
THREAD_PRIORITY_NORMAL 优先级不变
THREAD_PRIORITY_ABOVE_NORMAL 优先级+1
THREAD_PRIORITY_HIGHEST 优先级+2
 除了上述5种微调参数外,还可以用以下两个参数指定线程优先级:
THREAD_PRIORITY_IDLE  如果进程优先级为“realtime”,则线程优先级调整为16,否则为1
THREAD_PRIORITY_BELOW_CRITICAL   如果进程优先级为“realtime”,则线程优先级调整为31,否则为15


网上摘来的,呵呵
hohoxcn 2007-08-16
  • 打赏
  • 举报
回复
我想和优先级有关吧,在Windows95和WindowsNT中,任务优先级共有32级,从0到31
wltg2001 2007-08-16
  • 打赏
  • 举报
回复
这个问题你去问MS吧,除了它没人能回答
LLVVVVLL 2007-08-16
  • 打赏
  • 举报
回复
我是想问dwCreationFlags 为什么可以取0~31之间的值,msdn没有直接说明这点
龙凤呈祥焱 2007-08-16
  • 打赏
  • 举报
回复
不看MSDN的吗??
Process Creation Flags

The following process creation flags are used by the CreateProcess and CreateProcessAsUser functions. They can be specified in any combination, except as noted.



Constant/value Description
CREATE_BREAKAWAY_FROM_JOB
0x01000000 The child processes of a process associated with a job are not associated with the job.
If the calling process is not associated with a job, this constant has no effect. If the calling process is associated with a job, the job must set the JOB_OBJECT_LIMIT_BREAKAWAY_OK limit.

Windows NT and Windows Me/98/95: This value is not supported.
CREATE_DEFAULT_ERROR_MODE
0x04000000 The new process does not inherit the error mode of the calling process. Instead, the new process gets the default error mode.
This feature is particularly useful for multi-threaded shell applications that run with hard errors disabled.

The default behavior is for the new process to inherit the error mode of the caller. Setting this flag changes that default behavior.

CREATE_NEW_CONSOLE
0x00000010 The new process has a new console, instead of inheriting its parent's console (the default). For more information, see Creation of a Console.
This flag cannot be used with CREATE_NO_WINDOW or DETACHED_PROCESS.

CREATE_NEW_PROCESS_GROUP
0x00000200 The new process is the root process of a new process group. The process group includes all processes that are descendants of this root process. The process identifier of the new process group is the same as the process identifier, which is returned in the lpProcessInformation parameter. Process groups are used by the GenerateConsoleCtrlEvent function to enable sending a CTRL+BREAK signal to a group of console processes.
If this flag is specified, CTRL+C signals will be disabled for all processes within the new process group.

Windows Server 2003: This flag is ignored if specified with CREATE_NEW_CONSOLE.
CREATE_NO_WINDOW
0x08000000 The process is a console application that is run without a console window. This flag is valid only when starting a console application.
This flag cannot be used with CREATE_NEW_CONSOLE or DETACHED_PROCESS or when starting an MS-DOS-based application.

Windows Me/98/95: This value is not supported.
CREATE_PRESERVE_CODE_AUTHZ_LEVEL
0x02000000 Allows the caller to execute a child process that bypasses the process restrictions that would normally be applied automatically to the process.
Windows 2000/NT and Windows Me/98/95: This value is not supported.
CREATE_SEPARATE_WOW_VDM
0x00000800 This flag is valid only when starting a 16-bit Windows-based application. If set, the new process runs in a private Virtual DOS Machine (VDM). By default, all 16-bit Windows-based applications run as threads in a single, shared VDM. The advantage of running separately is that a crash only terminates the single VDM; any other programs running in distinct VDMs continue to function normally. Also, 16-bit Windows-based applications that are run in separate VDMs have separate input queues. That means that if one application stops responding momentarily, applications in separate VDMs continue to receive input. The disadvantage of running separately is that it takes significantly more memory to do so. You should use this flag only if the user requests that 16-bit applications should run in their own VDM.
Windows Me/98/95: This value is not supported.
CREATE_SHARED_WOW_VDM
0x00001000 The flag is valid only when starting a 16-bit Windows-based application. If the DefaultSeparateVDM switch in the Windows section of WIN.INI is TRUE, this flag overrides the switch. The new process is run in the shared Virtual DOS Machine.
Windows Me/98/95: This value is not supported.
CREATE_SUSPENDED
0x00000004 The primary thread of the new process is created in a suspended state, and does not run until the ResumeThread function is called.
CREATE_UNICODE_ENVIRONMENT
0x00000400 If this flag is set, the environment block pointed to by lpEnvironment uses Unicode characters. Otherwise, the environment block uses ANSI characters.
Windows Me/98/95: This value is not supported.
DEBUG_ONLY_THIS_PROCESS
0x00000002 The calling thread starts and debugs the new process. It can receive all related debug events using the WaitForDebugEvent function.
DEBUG_PROCESS
0x00000001 The calling thread starts and debugs the new process and all any child processes of the new process that are created with DEBUG_PROCESS. It can receive all related debug events using the WaitForDebugEvent function.
If this flag is combined with DEBUG_ONLY_THIS_PROCESS, the caller debugs only the new process.

Windows Me/98/95: This flag is not valid if the new process is a 16-bit application.
DETACHED_PROCESS
0x00000008 For console processes, the new process does not inherit its parent's console (the default). The new process can call the AllocConsole function at a later time to create a console. For more information, see Creation of a Console.
This value cannot be used with CREATE_NEW_CONSOLE or CREATE_NO_WINDOW.

一剑 2007-08-16
  • 打赏
  • 举报
回复
dwCreationFlags参数

用于标识标志,以便用于规定如何来创建新进程。如果将标志逐位用O R 操作符组合起来的话,就可以设定多个标志。

• E B U G _ P R O C E S S 标志用于告诉系统,父进程想要调试子进程和子进程将来生成的任何进程。本标志还告诉系统,当任何子进程(被调试进程)中发生某些事件时,将情况通知父进程(这时是调试程序)。

• D E B U G _ O N LY _ T H I S _ P R O C E S S 标志与D E B U G _ P R O C E S S 标志相类似,差别在于,调试程序只被告知紧靠父进程的子进程中发生的特定事件。如果子进程生成了别的进程,那么将不通知调试程序在这些别的进程中发生的事件。

• C R E AT E _ S U S P E N D E D 标志可导致新进程被创建,但是,它的主线程则被挂起。这使得父进程能够修改子进程的地址空间中的内存,改变子进程的主线程的优先级,或者在进程有机会执行任何代码之前将进程添加给一个作业。一旦父进程修改了子进程,父进程将允许子进程通过调用R e s u m e T h r e a d 函数来执行代码(第7 章将作详细介绍)。

• D E TA C H E D _ P R O C E S S 标志用于阻止基于C U I 的进程对它的父进程的控制台窗口的访问,并告诉系统将它的输出发送到新的控制台窗口。如果基于C U I 的进程是由另一个基于C U I的进程创建的,那么按照默认设置,新进程将使用父进程的控制台窗口(当通过命令外壳程序来运行C 编译器时,新控制台窗口并不创建,它的输出将被附加在现有控制台窗口的底部)。通过设定本标志,新进程将把它的输出发送到一个新控制台窗口。

• C R E AT E _ N E W _ C O N S O L E 标志负责告诉系统,为新进程创建一个新控制台窗口。如果同时设定C R E AT E _ N E W _ C O N S O L E 和D E TA C H E D _ P R O C E S S 标志,就会产生一个错误。

• C R E AT E _ N O _ W I N D O W 标志用于告诉系统不要为应用程序创建任何控制台窗口。可以使用本标志运行一个没有用户界面的控制台应用程序。

• C R E AT E _ N E W _ P R O C E S S _ G R O U P 标志用于修改用户在按下C t r l + C 或C t r l + B r e a k 键时得到通知的进程列表。如果在用户按下其中的一个组合键时,你拥有若干个正在运行的C U I 进程,那么系统将通知进程组中的所有进程说,用户想要终止当前的操作。当创建一个新的C U I 进程时,如果设定本标志,可以创建一个新进程组。如果该进程组中的一个进程处于活动状态时用户按下C t r l + C 或C t r l _ B r e a k 键,那么系统只通知用户需要这个进程组中的进程。

• C R E AT E _ D E FA U LT _ E R R O R _ M O D E 标志用于告诉系统,新进程不应该继承父进程使用的错误模式(参见本章前面部分中介绍的S e t E r r o r M o d e 函数)。

• C R E AT E _ S E PA R AT E _ W O W _ V D M 标志只能当你在Windows 2000 上运行1 6 位Wi n d o w s 应用程序时使用。它告诉系统创建一个单独的D O S 虚拟机(V D M ),并且在该V D M 中运行1 6 位Wi n d o w s 应用程序。按照默认设置,所有1 6 位Wi n d o w s 应用程序都在单个共享的V D M 中运行。在单独的VDM 中运行应用程序的优点是,如果应用程序崩溃,它只会使单个V D M 停止工作,而在别的V D M 中运行的其他程序仍然可以继续正常运行。另外,在单独的V D M 中运行的1 6 位Wi n d o w s 应用程序有它单独的输入队列。这意味着如果一个应用程序临时挂起,在各个V D M 中的其他应用程序仍然可以继续接收输入信息。运行多个V D M 的缺点是,每个V D M 都要消耗大量的物理存储器。Windows 98 在单个V D M 中运行所有的1 6 位Wi n d o w s 应用程序,不能改变这种情况。

• C R E AT E _ S H A R E D _ W O W _ V D M 标志只能当你在Windows 2000 上运行1 6 位Wi n d o w s 应用程序时使用。按照默认设置,除非设定了C R E AT E _ S E PA R AT E _ W O W _ V D M 标志,否则所有1 6 位Wi n d o w s 应用程序都必须在单个V D M 中运行。但是,通过在注册表中将H K E Y _ L O C A L _ M A C H I N E \ s y s t e m \ C u r r e n t C o n t r o l S e t \ C o n t r o l \ W O W 下的D e f a u l t S e p a r a t e V D M 设置为“y e s ”,就可以改变该默认行为特性。这时,C R E AT E _ S H A R E D _W O W _ V D M 标志就在系统的共享V D M 中运行1 6 位Wi n d o w s 应用程序。

• C R E AT E _ U N I C O D E _ E N V I R O N M E N T 标志用于告诉系统,子进程的环境块应该包含U n i c o d e 字符。按照默认设置,进程的环境块包含的是A N S I 字符串。

• C R E AT E _ F O R C E D O S 标志用于强制系统运行嵌入1 6 位O S / 2 应用程序的M O S - D O S 应用程序。

• C R E AT E _ B R E A K AWAY _ F R O M _ J O B 标志用于使作业中的进程生成一个与作业相关联的新进程

f d w C r e a t e 参数也可以用来设定优先级类。不过用不着这样做,并且对于大多数应用程序来说不应该这样做,因为系统会为新进程赋予一个默认优先级。表4 - 5 显示了各种可能的优先级类别。

表4-5 优先级类别

优先级类别 标志的标识符
空闲 IDLE_PRIORITY_CLASS
低于正常 BELOW_NORMAL_PRIORITY_CLASS
正常 NORMAL_PRIORITY_CLASS
高于正常 ABOVE_NORMAL_PRIORITY_CLASS
高 HIGH_PRIORITY_CLASS
实时 REALTIME_PRIORITY_CLASS

这些优先级类将会影响进程中包含的线程如何相对于其他进程的线程来进行调度。

注意B E L O W _ N O R M A L _ P R I O R I T Y _ C L A S S 和A B O V E _ N O R M A L _ P R I O R I T Y _ C L A S S这两个优先级类在Windows 2000 中是新类,Windows NT 4(或更早的版本)、Windows 95或Windows 98 均不支持这两个类。

hohoxcn 2007-08-16
  • 打赏
  • 举报
回复
msdn上有详细的解释,摘了一些。不过详细的具体说明还是看msdn吧
dwCreationFlags
[in] Specifies additional flags that control the priority class and the creation of the process. The following creation flags can be specified in any combination, except as noted.
The dwCreationFlags parameter also controls the new process's priority class, which is used to determine the scheduling priorities of the process's threads. If none of the following priority class flags is specified, the priority class defaults to NORMAL_PRIORITY_CLASS unless the priority class of the creating process is IDLE_PRIORITY_CLASS or BELOW_NORMAL_PRIORITY_CLASS. In this case, the child process receives the default priority class of the calling process. You can specify one of the following values.

15,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧