列出所有程序的问题可以从微软的Knowledge Base的文章:“Q192986 SAMPLE: MODLIST.EXE Shows How to Enumerate Processes and Modules”下载例子MODLIST.EXE。该例子运行的效果如下图所示。

该例子的工作原理是这样的:在Windows 95/98/Me下利用ToolHelp32函数。该例子共使用了以下几个函数:
CreateToolhelp32Snapshot()
Process32First()
Process32Next()
Module32First()
Module32Next()
在Windows NT, Windows 2000下使用PSAPI.DLL中的函数。该例子使用了以下几个函数:
EnumProcesses()
EnumProcessModules()
GetModuleFileNameExA()
强行关闭程序的问题可以参考微软的Knowledge Base的文章:“Q178893 HOWTO: Terminate an Application "Cleanly" in Win32”。
假定你用的是vc:用terminateprocess(终止进程函数)详查msdn。
上述两者的意见都对,但太笼统,补充一二。在pb中列举当前所有活动进程的示例。
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
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;否则有几个在运行则返回几
4、如在vb中调用,则比在pb中调用更简单。

该例子的工作原理是这样的:在Windows 95/98/Me下利用ToolHelp32函数。该例子共使用了以下几个函数:
CreateToolhelp32Snapshot()
Process32First()
Process32Next()
Module32First()
Module32Next()
在Windows NT, Windows 2000下使用PSAPI.DLL中的函数。该例子使用了以下几个函数:
EnumProcesses()
EnumProcessModules()
GetModuleFileNameExA()
强行关闭程序的问题可以参考微软的Knowledge Base的文章:“Q178893 HOWTO: Terminate an Application "Cleanly" in Win32”。
假定你用的是vc:用terminateprocess(终止进程函数)详查msdn。
上述两者的意见都对,但太笼统,补充一二。在pb中列举当前所有活动进程的示例。
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
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;否则有几个在运行则返回几
4、如在vb中调用,则比在pb中调用更简单。