Winxp总管经常这样问,是否关闭explorer直接生效设置。
一个例子
窗体加入3个按钮,名字看代码就知道
'窗体代码部分
Option Explicit
'打开注册表中的项
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
'创建注册表项
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias _
"RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
'设置注册表项中的值
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Const HKEY_CURRENT_USER = &H80000001
Private Const REG_BINARY As Long = 3
Private Const REG_SZ = 1
Private Const REG_DWORD = 4
Private Sub cmdHideDesktop_Click() '隐藏桌面
Dim hKey As Long
RegOpenKeyEx HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Policies", 0, 0, hKey
RegCreateKey HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Policies\Explorer", hKey
RegSetValueEx hKey, "nodesktop", 0, REG_BINARY, &H1, 4
Updatenow
End Sub
Private Sub cmdShowDesktop_Click() '显示桌面
Dim hKey As Long
Dim bArr(0 To 4) As Byte
RegCreateKey HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Policies\Explorer", hKey
RegDeleteValue hKey, "nodesktop"
Updatenow
End Sub
Private Sub cmdExit_Click()
End
End Sub
模块代码部分
Option Explicit
'获得进程的句柄
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
'终止进程
Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
'创建一个系统快照
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, lProcessID As Long) As Long
'获得系统快照中的第一个进程的信息
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal mSnapShot As Long, uProcess As PROCESSENTRY32) As Long
'获得系统快照中的下一个进程的信息
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal mSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long) '关闭句柄
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 * 260
End Type
Private Const TH32CS_SNAPPROCESS As Long = 2& '快照常量
Private Const PROCESS_ALL_ACCESS = &H1F0FFF 'OpenProcess使用的常量
Dim list3 As Long '要关闭进程的ID
Dim ParentHandle As Long '要关闭进程的句柄
Public Sub Updatenow()
Dim mSnapShot As Long
Dim uProcess As PROCESSENTRY32
Dim ProcessName As String
Dim R As Long
Dim I As Integer
mSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If mSnapShot = 0 Then Exit Sub
uProcess.dwSize = Len(uProcess)
R = ProcessFirst(mSnapShot, uProcess)
Do While R '开始遍历所有进程
I = InStr(1, uProcess.szExeFile, Chr$(0))
ProcessName = Mid$(LCase$(Left$(uProcess.szExeFile, I - 1)), 1, 50)
If ProcessName = "explorer.exe" Then
list3 = uProcess.th32ProcessID
End If
R = ProcessNext(mSnapShot, uProcess)
Loop
Call CloseHandle(mSnapShot)
ParentHandle = OpenProcess(PROCESS_ALL_ACCESS, True, list3)
Call TerminateProcess(ParentHandle, 0) '杀死explorer.exe重新加载
End Sub
[此贴子已经被作者于2006-9-25 22:11:48编辑过]