如果我想做一段结束指定进程的代码,能同时在win98和win2000 XP这样的系统下运行。我该怎么做。反正98下没什么问题已经搞定,但XP下就麻烦大了,好像与系统操作的权限有关。
我对NT内核的系统很陌生感,见过“关机”的程序在这两种系统下的代码就截然不同。求高手指点一二。
我对NT内核的系统很陌生感,见过“关机”的程序在这两种系统下的代码就截然不同。求高手指点一二。
解决方案 »
- 对边界条件要形成条件反射么?
- 按钮图片奇怪丢失!
- 关于TreeView的问题,大虾们请进
- 拜托大家了:vb中如何通过ado提取字段类型/如何增加一个字段??
- 我想在 自定义控件 里实现这个很简单的功能
- 请教高手,语音聊天如何做!有没有思路或简单例子?
- 请问到底如何向VB自编的activexDLL中传属性字符串???????????
- MSHFlexGrid1控件,有固定列和行各一列,现在用鼠标点击固定列中的任意一个单元格会选取当前的整行,问题是,现在要在这项操作中处理一些
- 在VB中,如何使程序总在最前? 马上送分
- 又是MSFlexgrid的问题!!!请大家帮忙~!
- 关于winsock 发送结构体数据的问题
- 本人写的一本有关于计算机的小说,希望各位多多捧场!
但这文章内容中的意思,咋不大明白呢? 译文如下:
在Win32系统下,操作系统有约定在一个进程结束时自动的回收它所占用的资源。这没什么问题,然而,这意味着进程本身有机会将最后的数据流信息传至磁盘,最后的消息传递来终结远程的连接,还有就是意味着进程的DLL库有机会运行它的PROCESS_DETACH代码。这就是为什么一般的情况下人们宁愿选择避免在Windows 95和Windows NT系统下结束应用程序的原因。
还有在win98系统下可以取得创建进程之程序的路径。winXP下路径则没有,为什么 。
win98与winXP的进程标识,模块ID,都不是一样的数值,98下会被显示成负的,还是要分系统区别对待。
还是权限不够,不知怎么提升。那个PROCESS_TERMINATE在MSDN 2002中也找不着。估计是我搜索的方法不对。
Private Const MAX_PATH = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End TypePrivate Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As Any) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As Any) As LongPrivate Const TH32CS_SNAPHEAPLIST = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE = &H8
Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST + TH32CS_SNAPPROCESS + TH32CS_SNAPTHREAD + TH32CS_SNAPMODULE)
Private Const TH32CS_INHERIT = &H80000000
'Terminate Process Functions
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const PROCESS_ALL_ACCESS = 0
'查找进程和终结进程
Dim hSnapshot As Long, lRet As Long, P As PROCESSENTRY32
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
P.dwSize = Len(P)
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, ByVal 0)
If hSnapshot Then
lRet = Process32First(hSnapshot, P)
Do While lRet
If InStr(P.szExeFile, "LANCLT.EXE") <> 0 Then
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, P.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
End If
lRet = Process32Next(hSnapshot, P)
Loop
lRet = CloseHandle(hSnapshot)
End If
后来发现 PROCESS_ALL_ACCESS (值为0)用 1& 代替,在win2000 XP 之下就可以结束进程了。
但系统进程不能终结,比如svchost.exe
ms-help://MS.MSDNQTR.2002JAN.1033/dllproc/prothred_68ab.htm
不懂就过来问:)
将找到的文章重贴出来:'KillProcess - Terminate any applicationPrivate Type LUID
lowpart As Long
highpart As Long
End TypePrivate Type TOKEN_PRIVILEGES
PrivilegeCount As Long
LuidUDT As LUID
Attributes As Long
End TypeConst TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Const PROCESS_ALL_ACCESS = &H1F0FFFPrivate Declare Function GetVersion Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As _
Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle _
As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias _
"LookupPrivilegeValueA" (ByVal lpSystemName As String, _
ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal _
TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
PreviousState As Any, ReturnLength As Any) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As _
Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As _
Long, ByVal uExitCode As Long) As Long' Terminate any application and return an exit code to Windows
' This works under NT/2000, even when the calling process
' doesn't have the privilege to terminate the application
' (for example, this may happen when the process was launched
' by yet another program)
'
' Usage: Dim pID As Long
' pID = Shell("Notepad.Exe", vbNormalFocus)
' '...
' If KillProcess(pID, 0) Then
' MsgBox "Notepad was terminated"
' End IfFunction KillProcess(ByVal hProcessID As Long, Optional ByVal ExitCode As Long) _
As Boolean
Dim hToken As Long
Dim hProcess As Long
Dim tp As TOKEN_PRIVILEGES
' Windows NT/2000 require a special treatment
' to ensure that the calling process has the
' privileges to shut down the system
' under NT the high-order bit (that is, the sign bit)
' of the value retured by GetVersion is cleared
If GetVersion() >= 0 Then
' open the tokens for the current process
' exit if any error
If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or _
TOKEN_QUERY, hToken) = 0 Then
GoTo CleanUp
End If
' retrieves the locally unique identifier (LUID) used
' to locally represent the specified privilege name
' (first argument = "" means the local system)
' Exit if any error
If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
GoTo CleanUp
End If
' complete the TOKEN_PRIVILEGES structure with the # of
' privileges and the desired attribute
tp.PrivilegeCount = 1
tp.Attributes = SE_PRIVILEGE_ENABLED
' try to acquire debug privilege for this process
' exit if error
If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, _
ByVal 0&) = 0 Then
GoTo CleanUp
End If
End If
' now we can finally open the other process
' while having complete access on its attributes
' exit if any error
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
If hProcess Then
' call was successful, so we can kill the application
' set return value for this function
KillProcess = (TerminateProcess(hProcess, ExitCode) <> 0)
' close the process handle
CloseHandle hProcess
End If
If GetVersion() >= 0 Then
' under NT restore original privileges
tp.Attributes = 0
AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
CleanUp:
If hToken Then CloseHandle hToken
End If
End Function
页面:http://www.shizhenyuan.com/bbs/topic.php?forumid=10&filename=f_64
下载:http://www.shizhenyuan.com/bbs/attachment.php?am=0&filename=f_64&forumid=10&article=0
程序并不完善,代码也不规整,但我打算过一段时间再升级了。也可以理解消化一段时间。仔细算算其实里面没多少东西是自己的。
就送给所有喜欢它的人自由使用吧。
再次谢谢大家的帮助。准备结贴了。