1,用BAT文件\x0d\x0aPrivate Sub Form_Load()\x0d\x0a\x0d\x0aOpen App.Path &"\a.bat" For Output As #1\x0d\x0a'"@echo off" 不显示执行过程\x0d\x0aPrint #1, "@echo off"\x0d\x0aPrint #1, "sleep 100"\x0d\x0a'a.bat 删除指定文件\x0d\x0aPrint #1, "del " &App.EXEName + ".exe"\x0d\x0a'a.bat 删除自身\x0d\x0aPrint #1, "del a.bat"\x0d\x0aPrint #1, "cls"\x0d\x0aPrint #1, "exit"\x0d\x0aClose #1\x0d\x0a\x0d\x0aShell App.Path &"\a.bat", vbHide\x0d\x0aEnd\x0d\x0a\x0d\x0aEnd Sub\x0d\x0a\x0d\x0a2、用API\x0d\x0a一个
模块,非原创(我修改部分代码),可以让
程序退出后自毁(删除自身)。\x0d\x0a原理还特别不清楚,不过应该和CMD的DEL命令有关,但删除时没看到CMD.EXE进程,速度也很快\x0d\x0a成功率挺高的(我还没失败过)\x0d\x0a\x0d\x0a创建一个模块,写入代码:\x0d\x0aConst NORMAL_PRIORITY_CLASS = &H20\x0d\x0aConst REALTIME_PRIORITY_CLASS = &H100\x0d\x0aConst THREAD_PRIORITY_NORMAL = 0\x0d\x0aConst THREAD_PRIORITY_IDLE = -15\x0d\x0aConst IDLE_PRIORITY_CLASS = &H40\x0d\x0aConst DETACHED_PROCESS = &H8\x0d\x0aConst CREATE_SUSPENDED = &H4\x0d\x0aConst THREAD_PRIORITY_TIME_CRITICAL = 15\x0d\x0aConst SW_HIDE = 0\x0d\x0aConst STARTF_USESHOWWINDOW = &H1\x0d\x0a\x0d\x0aType STARTUPINFO\x0d\x0acb As Long\x0d\x0alpReserved As String\x0d\x0alpDesktop As String\x0d\x0alpTitle As String\x0d\x0adwX As Long\x0d\x0adwY As Long\x0d\x0adwXSize As Long\x0d\x0adwYSize As Long\x0d\x0adwXCountChars As Long\x0d\x0adwYCountChars As Long\x0d\x0adwFillAttribute As Long\x0d\x0adwFlags As Long\x0d\x0awShowWindow As Integer\x0d\x0acbReserved2 As Integer\x0d\x0alpReserved2 As Long\x0d\x0ahStdInput As Long\x0d\x0ahStdOutput As Long\x0d\x0ahStdError As Long\x0d\x0aEnd Type\x0d\x0a\x0d\x0aType PROCESS_INFORMATION\x0d\x0ahProcess As Long\x0d\x0ahThread As Long\x0d\x0adwProcessId As Long\x0d\x0adwThreadId As Long\x0d\x0aEnd Type\x0d\x0a\x0d\x0aType SECURITY_ATTRIBUTES\x0d\x0anLength As Long\x0d\x0alpSecurityDescriptor As Long\x0d\x0abInheritHandle As Long\x0d\x0aEnd Type\x0d\x0a\x0d\x0aDeclare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long\x0d\x0aDeclare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long\x0d\x0aDeclare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long\x0d\x0aDeclare Function GetCurrentProcess Lib "kernel32" () As Long\x0d\x0aDeclare Function GetCurrentThreadId Lib "kernel32" () As Long\x0d\x0aDeclare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long\x0d\x0aDeclare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long\x0d\x0aDeclare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long\x0d\x0aDeclare Function GetCurrentThread Lib "kernel32" () As Long\x0d\x0aDeclare 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\x0d\x0a\x0d\x0aFunction DeleteMe() As Boolean\x0d\x0aDim szModuleAs String\x0d\x0aDim szComspec As String\x0d\x0aDim szParamsAs String\x0d\x0a\x0d\x0aDim si As STARTUPINFO\x0d\x0aDim pi As PROCESS_INFORMATION\x0d\x0aDim sa1 As SECURITY_ATTRIBUTES\x0d\x0aDim sa2 As SECURITY_ATTRIBUTES\x0d\x0a\x0d\x0aszModule = String(512, 0)\x0d\x0aszComspec = String(512, 0)\x0d\x0aszParams = String(512, 0)\x0d\x0a\x0d\x0a'// get file path names:\x0d\x0aIf ((GetModuleFileName(0, szModule, 512) 0) And (GetShortPathName(szModule, szModule, 512) 0) And (GetEnvironmentVariable("COMSPEC", szComspec, 512) 0)) Then\x0d\x0a'// set command shell parameters\x0d\x0aszComspec = Left(szComspec, InStr(szComspec, Chr(0)) - 1)\x0d\x0aszModule = Left(szModule, InStr(szModule, Chr(0)) - 1)\x0d\x0a\x0d\x0aszComspec = szComspec &" /c del " &"""" &szModule &"""" '这里是我(yfdyh000)修改的,支持带空格路径\x0d\x0a'szComspec = szComspec &" /c del " &szModule'原代码\x0d\x0a\x0d\x0a'// set struct members\x0d\x0aWith si\x0d\x0a.cb = Len(si)\x0d\x0a.dwFlags = STARTF_USESHOWWINDOW\x0d\x0a.wShowWindow = SW_HIDE\x0d\x0aEnd With\x0d\x0a'// increase resource allocation to program\x0d\x0aCall SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)\x0d\x0aCall SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL)\x0d\x0a\x0d\x0a'// invoke command shell\x0d\x0a'Debug.Print CreateProcess(vbNullString, szComspec, sa1, sa2, 0&, CREATE_SUSPENDED Or DETACHED_PROCESS, 0&, vbNullString, si, pi)\x0d\x0aIf CreateProcess(vbNullString, szComspec, sa1, sa2, 0, CREATE_SUSPENDED Or DETACHED_PROCESS, 0, vbNullString, si, pi) Then\x0d\x0a'// suppress command shell process until program exits\x0d\x0aCall SetPriorityClass(pi.hProcess, IDLE_PRIORITY_CLASS)\x0d\x0aCall SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE)\x0d\x0a\x0d\x0a'// resume shell process with new low priority\x0d\x0aCall ResumeThread(pi.hThread)\x0d\x0a\x0d\x0a'// everything seemed to work\x0d\x0aDeleteMe = True\x0d\x0aExit Function\x0d\x0aElse '// if error, normalize allocation\x0d\x0aCall SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS)\x0d\x0aCall SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL)\x0d\x0aEnd If\x0d\x0aEnd If\x0d\x0aDeleteMe = False\x0d\x0aEnd Function\x0d\x0a\x0d\x0a在程序中调用命令:DeleteMe(比如在:Form_Unload)\x0d\x0a如果返回True证明成功,退出后会自动删除自身。这个容易,在unload事件中调用cmd
就可以了,如下:
shell
"del
/F
/Q
"
&
app.path
&
app.exename
&
".exe"
把这句写在unload事件就可以,记住,不能在调试中使用!否则的话,整个vb可以会没了的
评论列表(0条)