EnumProcesses()vs CreateToolhelp32Snapshot() [英] EnumProcesses() vs CreateToolhelp32Snapshot()

查看:548
本文介绍了EnumProcesses()vs CreateToolhelp32Snapshot()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在两个Win32 API函数之间是否存在任何差异,例如EnumProcesses()和 CreateToolhelp32Snapshot(),用于枚举所有活动进程并加载模块。

I was wondering if there are any differences - mostly performance wise - between the two Win32 API functions EnumProcesses() and CreateToolhelp32Snapshot() for enumerating all active processes and loaded modules. Or if one is better than the other to use and why.

推荐答案

我认为它们在性能方面差不多相同(和结果),因为它们都调用相同的底层NT API,虽然CreateToolhelp32Snapshot()可能有一个微小的开销,因为它创建一个节对象,并将所有信息复制到它,而EnumProcesses()/ EnumProcessModules()直接与用户提供缓冲区。

I think they are pretty much the same in terms of performance (and results) as they both call the same underlying NT API, though CreateToolhelp32Snapshot() may have a slight overhead as it creates a section object and copies all the information to it whereas EnumProcesses()/EnumProcessModules() works directly with user-supplied buffers. The difference is probably negligible in real world performance, though.

我稍微偏好EnumProcesses(),因为它是一个更简单的API使用,但CreateToolhelp32Snapshot()返回更多信息,如果你需要它。 EnumProcesses()的唯一缺点是你应该在循环中调用它,因为你可能没有分配足够大的缓冲区; CreateToolhelp32Snapshot()负责缓冲区管理。在实践中,我只是在堆栈上分配足够大的缓冲区来容纳1024个进程ID或模块句柄;到目前为止,我还没有遇到一个系统,其中这些限制甚至远程接近到达。当然,我们说过同样的事情关于MAX_PATH不是很久以前,现在我们遇到了问题...

I slightly prefer EnumProcesses() as it is (IMO) a simpler API to use, but CreateToolhelp32Snapshot() returns more information if you need it. The only downside to EnumProcesses() is that you are supposed to call it in a loop as you may not have allocated a large enough buffer; CreateToolhelp32Snapshot() takes care of the buffer management for you. In practice I just allocate a buffer on the stack large enough to hold 1024 process ids or module handles; so far I have not come across a system where either of these limits was even remotely close to being reached. Of course we said the same thing about MAX_PATH not so long ago and now we are running into problems with that...

这篇关于EnumProcesses()vs CreateToolhelp32Snapshot()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆