想把当前获取到的所有进程名保存到一个数组中
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hProcessSnap <> INVALID_HANDLE_VALUE Then
udtProcessQueue.dwSize = Len(udtProcessQueue)
hNextProcess = Process32First(hProcessSnap, udtProcessQueue)
Do While hNextProcess
Debug.Print LCase(Left(udtProcessQueue.szExeFile, InStr(1&, udtProcessQueue.szExeFile, Chr(0&)) - 1&))
hNextProcess = Process32Next(hProcessSnap, udtProcessQueue)
lngProcessCount = lngProcessCount + 1&
Loop
CloseHandle hProcessSnap
End If
如以上代码所示,难道必须先第一遍Do While循环枚举到所有进程数后,用ReDim Preserve astrProcessList(lngProcessCount)修改动态数组的大小,最后再第二遍Do While循环枚举把所有进程名保存到astrProcessList数组中吗?请问还有没有什么更高效的方法呀?
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hProcessSnap <> INVALID_HANDLE_VALUE Then
udtProcessQueue.dwSize = Len(udtProcessQueue)
hNextProcess = Process32First(hProcessSnap, udtProcessQueue)
Do While hNextProcess
Debug.Print LCase(Left(udtProcessQueue.szExeFile, InStr(1&, udtProcessQueue.szExeFile, Chr(0&)) - 1&))
hNextProcess = Process32Next(hProcessSnap, udtProcessQueue)
lngProcessCount = lngProcessCount + 1&
Loop
CloseHandle hProcessSnap
End If
如以上代码所示,难道必须先第一遍Do While循环枚举到所有进程数后,用ReDim Preserve astrProcessList(lngProcessCount)修改动态数组的大小,最后再第二遍Do While循环枚举把所有进程名保存到astrProcessList数组中吗?请问还有没有什么更高效的方法呀?
解决方案 »
- 如何通过语句 将二级菜单设置为 无效disabled?
- 【发布】源码+详细注释+教程 VB编写游戏修改器
- 新手问题
- 过多的子程序会对程序有影响吗?
- 怎样遍历窗口内的所有控件,并根据需要给某些特定控件的某此属性进行赋值?
- 美伊开战了 :(
- VB二级联动菜单查询语句怎么写?
- 撒分啦,请大家都来推荐优秀的vb站点,推荐者有分~!(***分数还会加***)
- VB中有‘鼠标移动到某控件上’发生的事件编程,那么是否有‘鼠标移开某控件时’的事件编程?
- CommonDialog控件的问题---------->>>>顺便问一下,我怎么只有5分可给了??
- vb怎样实现程序界面窗口在桌面快捷方式图标的下部
- datagrid保存修改
第一种方法:
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hProcessSnap <> INVALID_HANDLE_VALUE Then
udtProcessQueue.dwSize = Len(udtProcessQueue)
hNextProcess = Process32First(hProcessSnap, udtProcessQueue)
Do While hNextProcess
hNextProcess = Process32Next(hProcessSnap, udtProcessQueue)
ReDim Preserve astrProcessList(lngProcessCount)
astrProcessList(lngProcessCount)=Left(udtProcessQueue.szExeFile, InStr(1&, udtProcessQueue.szExeFile, Chr(0&)) - 1&))
lngProcessCount = lngProcessCount + 1&
Loop
CloseHandle hProcessSnap
End If
第二种方法:
1、定义字符型变量S,在Do While枚举到每一个进程时,作一次累加:s=s & 进程名 & ","
2、全部进程枚举完毕,使用astrProcessList=split(s,",")一次性把字符串S转换为数组。
第三种方法:
1、声明:
Private Declare Function EnumProcesses Lib "PSAPI.DLL" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
2、自定义函数:
Public Function GetProcesses() As Long
Dim lngCbNeeded As Long
Dim lngNumElements As Long, lngRet As Long
ReDim lngProcArr(1024)
lngRet = EnumProcesses(lngProcArr(0), 4 * 1024, lngCbNeeded)
lngNumElements = lngCbNeeded / 4
GetProcesses = lngNumElements - 1
End Function
3、获取进程总数时,调用如下:
dim i as integer
i=GetProcesses
redim astrProcessList(i-1)