|
--------------- 问题:例程中 显示的进程名均为 当前真实进程名的一半,也就是: 例如 进程一 真实名称为 sqlservr.exe 在程序中显示为第一三五七九等单数字符:slev.x 进程二 真实名为 explorer.exe 在程序中显示为 epoe.x ------------------------------具体代码如下; //在窗中中放置了一个 按钮和一个 listbox控件 // 1、Declare四个Win32Api函数。 Function Long GetCurrentProcessId() Library "kernel32.dll" Function Long CreateToolhelp32Snapshot(Long Flags,Long ProcessId) Library "kernel32.dll" Function Integer Process32First(uLong Snapshot,ref s_Process Process) Library "kernel32.dll" Function Integer Process32Next(uLong Snapshot,ref s_Process Process) Library "kernel32.dll" // 2、定义s_Process结构 unsignedlong structsize unsignedlong usage unsignedlong processid unsignedlong defaultheapid unsignedlong moduleid unsignedlong threads unsignedlong parentprocessid unsignedlong classbase unsignedlong flags character filename[260] //3、按钮调用示例(此函数查找在系统中是否已有当前程序的复本在运行) s_Process lst_Process //进程结构 String ls_FileName[100],ls_CurExeName //最多100个进程,可改进 ulong ln_ProcessID,ln_SameCount,ln_Snapshot,ln_Circle,ln_Count ln_ProcessID = GetCurrentProcessId() //取当前进程的ID if IsNull(ln_ProcessID) or ln_ProcessID<1 then return -1 //出错则返回 ln_Snapshot = CreateToolhelp32Snapshot(2,0) //在堆上创建进程快照 if (ln_Snapshot<1) then return -1 //出错则返回 lst_Process.StructSize = 296 //Win32api的Process结构大小 ln_SameCount = 0 //复本数为0 if Process32First(ln_Snapshot,lst_Process)=0 then return -1//取第一个进程失败则返回 ln_Count = 1 ls_FileName[ln_Count] = lst_Process.FileName //列举的进程名称放入数组 //如列举到的进程ID等于当前进程ID,则知道了当前进程的名称,保存 if lst_Process.ProcessID=ln_ProcessID then ls_CurExeName=lst_Process.FileName do while true //循环取列举的进程名称,放入数组 if Process32Next(ln_Snapshot,lst_Process)=0 then exit //列举完毕 ln_Count = ln_Count + 1 ls_FileName[ln_Count] = lst_Process.FileName if lst_Process.ProcessID=ln_ProcessID then ls_CurExeName=lst_Process.FileName lb_1.additem(ls_filename[ln_count]) //在listbox中显示进程名 loop for ln_Circle=1 to ln_Count //计算系统中有几个同名进程 if ls_CurExeName=ls_FileName[ln_Circle] then ln_SameCount=ln_SameCount+1 next return ln_SameCount //如当前进程无复本在运行,返回1;否则有几个在运行则返回几 |
Holion's Blog http://hi.baidu.com/holion/blog/item/11e6c98ac43444dbfd1f1034.html
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1845770/viewspace-612285/ ,如需转载,请注明出处,否则将追究法律责任。