在vb中,怎样让程序退出后删除自身
怎样让vb程序退出之后删除自身,假如我弄了一个按钮,命令是unloadme,然后删除自身vb代码应该怎么写?(请附加上代码解释,并举出几个例子,或者越详细越好,我是个初学...
怎样让vb程序退出之后删除自身,假如我弄了一个按钮,命令是unload me,然后删除自身 vb代码应该怎么写?(请附加上代码解释,并举出几个例子,或者越详细越好,我是个初学者)
可以根据"del " & App.EXEName + ".exe"举个例子吗
我谢了一个"del /q /s /f & App.EXEName=c: + ".\1.exe" 结果不行 展开
可以根据"del " & App.EXEName + ".exe"举个例子吗
我谢了一个"del /q /s /f & App.EXEName=c: + ".\1.exe" 结果不行 展开
4个回答
2015-10-23 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:25897
获赞数:1464986
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。
向TA提问 私信TA
关注
展开全部
1,用BAT文件
Private Sub Form_Load()
Open App.Path & "\a.bat" For Output As #1
'"@echo off" 不显示执行过程
Print #1, "@echo off"
Print #1, "sleep 100"
'a.bat 删除指定文件
Print #1, "del " & App.EXEName + ".exe"
'a.bat 删除自身
Print #1, "del a.bat"
Print #1, "cls"
Print #1, "exit"
Close #1
Shell App.Path & "\a.bat", vbHide
End
End Sub
2、用API
一个模块,非原创(我修改部分代码),可以让程序退出后自毁(删除自身)。
原理还特别不清楚,不过应该和CMD的DEL命令有关,但删除时没看到CMD.EXE进程,速度也很快
成功率挺高的(我还没失败过)
创建一个模块,写入代码:
Const NORMAL_PRIORITY_CLASS = &H20
Const REALTIME_PRIORITY_CLASS = &H100
Const THREAD_PRIORITY_NORMAL = 0
Const THREAD_PRIORITY_IDLE = -15
Const IDLE_PRIORITY_CLASS = &H40
Const DETACHED_PROCESS = &H8
Const CREATE_SUSPENDED = &H4
Const THREAD_PRIORITY_TIME_CRITICAL = 15
Const SW_HIDE = 0
Const STARTF_USESHOWWINDOW = &H1
Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Declare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long
Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
Declare Function GetCurrentThread Lib "kernel32" () As Long
Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Function DeleteMe() As Boolean
Dim szModule As String
Dim szComspec As String
Dim szParams As String
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim sa1 As SECURITY_ATTRIBUTES
Dim sa2 As SECURITY_ATTRIBUTES
szModule = String(512, 0)
szComspec = String(512, 0)
szParams = String(512, 0)
'// get file path names:
If ((GetModuleFileName(0, szModule, 512) <> 0) And (GetShortPathName(szModule, szModule, 512) <> 0) And (GetEnvironmentVariable("COMSPEC", szComspec, 512) <> 0)) Then
'// set command shell parameters
szComspec = Left(szComspec, InStr(szComspec, Chr(0)) - 1)
szModule = Left(szModule, InStr(szModule, Chr(0)) - 1)
szComspec = szComspec & " /c del " & """" & szModule & """" '这里是我(yfdyh000)修改的,支持带空格路径
' szComspec = szComspec & " /c del " & szModule'原代码
'// set struct members
With si
.cb = Len(si)
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = SW_HIDE
End With
'// increase resource allocation to program
Call SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
Call SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL)
'// invoke command shell
'Debug.Print CreateProcess(vbNullString, szComspec, sa1, sa2, 0&, CREATE_SUSPENDED Or DETACHED_PROCESS, 0&, vbNullString, si, pi)
If CreateProcess(vbNullString, szComspec, sa1, sa2, 0, CREATE_SUSPENDED Or DETACHED_PROCESS, 0, vbNullString, si, pi) Then
'// suppress command shell process until program exits
Call SetPriorityClass(pi.hProcess, IDLE_PRIORITY_CLASS)
Call SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE)
'// resume shell process with new low priority
Call ResumeThread(pi.hThread)
'// everything seemed to work
DeleteMe = True
Exit Function
Else '// if error, normalize allocation
Call SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS)
Call SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL)
End If
End If
DeleteMe = False
End Function
在程序中调用命令:DeleteMe(比如在:Form_Unload)
如果返回True证明成功,退出后会自动删除自身。
Private Sub Form_Load()
Open App.Path & "\a.bat" For Output As #1
'"@echo off" 不显示执行过程
Print #1, "@echo off"
Print #1, "sleep 100"
'a.bat 删除指定文件
Print #1, "del " & App.EXEName + ".exe"
'a.bat 删除自身
Print #1, "del a.bat"
Print #1, "cls"
Print #1, "exit"
Close #1
Shell App.Path & "\a.bat", vbHide
End
End Sub
2、用API
一个模块,非原创(我修改部分代码),可以让程序退出后自毁(删除自身)。
原理还特别不清楚,不过应该和CMD的DEL命令有关,但删除时没看到CMD.EXE进程,速度也很快
成功率挺高的(我还没失败过)
创建一个模块,写入代码:
Const NORMAL_PRIORITY_CLASS = &H20
Const REALTIME_PRIORITY_CLASS = &H100
Const THREAD_PRIORITY_NORMAL = 0
Const THREAD_PRIORITY_IDLE = -15
Const IDLE_PRIORITY_CLASS = &H40
Const DETACHED_PROCESS = &H8
Const CREATE_SUSPENDED = &H4
Const THREAD_PRIORITY_TIME_CRITICAL = 15
Const SW_HIDE = 0
Const STARTF_USESHOWWINDOW = &H1
Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Declare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long
Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
Declare Function GetCurrentThread Lib "kernel32" () As Long
Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Function DeleteMe() As Boolean
Dim szModule As String
Dim szComspec As String
Dim szParams As String
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim sa1 As SECURITY_ATTRIBUTES
Dim sa2 As SECURITY_ATTRIBUTES
szModule = String(512, 0)
szComspec = String(512, 0)
szParams = String(512, 0)
'// get file path names:
If ((GetModuleFileName(0, szModule, 512) <> 0) And (GetShortPathName(szModule, szModule, 512) <> 0) And (GetEnvironmentVariable("COMSPEC", szComspec, 512) <> 0)) Then
'// set command shell parameters
szComspec = Left(szComspec, InStr(szComspec, Chr(0)) - 1)
szModule = Left(szModule, InStr(szModule, Chr(0)) - 1)
szComspec = szComspec & " /c del " & """" & szModule & """" '这里是我(yfdyh000)修改的,支持带空格路径
' szComspec = szComspec & " /c del " & szModule'原代码
'// set struct members
With si
.cb = Len(si)
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = SW_HIDE
End With
'// increase resource allocation to program
Call SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
Call SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL)
'// invoke command shell
'Debug.Print CreateProcess(vbNullString, szComspec, sa1, sa2, 0&, CREATE_SUSPENDED Or DETACHED_PROCESS, 0&, vbNullString, si, pi)
If CreateProcess(vbNullString, szComspec, sa1, sa2, 0, CREATE_SUSPENDED Or DETACHED_PROCESS, 0, vbNullString, si, pi) Then
'// suppress command shell process until program exits
Call SetPriorityClass(pi.hProcess, IDLE_PRIORITY_CLASS)
Call SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE)
'// resume shell process with new low priority
Call ResumeThread(pi.hThread)
'// everything seemed to work
DeleteMe = True
Exit Function
Else '// if error, normalize allocation
Call SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS)
Call SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL)
End If
End If
DeleteMe = False
End Function
在程序中调用命令:DeleteMe(比如在:Form_Unload)
如果返回True证明成功,退出后会自动删除自身。
展开全部
Private Sub Form_Load()
Open App.Path & "\1.bat" For Output As #1
'"@echo off" 不显示执行过程
Print #1, "@echo off"
Print #1, "sleep 100"
'1.bat 删除指定文件
Print #1, "del " & App.EXEName + ".exe"
'1.bat 删除自身
Print #1, "del 1.bat"
Print #1, "cls"
Print #1, "exit"
Close #1
'系统、隐藏
SetAttr App.Path & "\1.bat", vbHidden Or vbSystem
End Sub
Private Sub Form_Unload(Cancel As Integer)
SetAttr App.Path & "\1.bat", vbNomal
Shell App.Path & "\1.bat", vbHide
End Sub
Open App.Path & "\1.bat" For Output As #1
'"@echo off" 不显示执行过程
Print #1, "@echo off"
Print #1, "sleep 100"
'1.bat 删除指定文件
Print #1, "del " & App.EXEName + ".exe"
'1.bat 删除自身
Print #1, "del 1.bat"
Print #1, "cls"
Print #1, "exit"
Close #1
'系统、隐藏
SetAttr App.Path & "\1.bat", vbHidden Or vbSystem
End Sub
Private Sub Form_Unload(Cancel As Integer)
SetAttr App.Path & "\1.bat", vbNomal
Shell App.Path & "\1.bat", vbHide
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
vb无法或者说根本无法删除自己,,
但是可以用bat删除
Unload Me
sleep 1 ' 等待1毫秒,避免内存没有释放完全
Shell "cmd /c del /f/q " & App.EXEName & ".exe",0
以上已经是自删代码了 连复制代码都不会 我就没办法了 sleep是api函数 你那样写是肯定错的
但是可以用bat删除
Unload Me
sleep 1 ' 等待1毫秒,避免内存没有释放完全
Shell "cmd /c del /f/q " & App.EXEName & ".exe",0
以上已经是自删代码了 连复制代码都不会 我就没办法了 sleep是api函数 你那样写是肯定错的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Visual Studio 2010 VB代码(.Net Framework)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Shell.Click
Kill("C:\1.exe") 'PathName As String
Close(Me)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Shell.Click
Kill("C:\1.exe") 'PathName As String
Close(Me)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询