下面是个例子
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Sub Form_Load()
'end this process
ExitProcess GetExitCodeProcess(GetCurrentProcess, 0)
End Sub
Option Explicit
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Private Declare Function VirtualProtect Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long
Private Sub Command3_Click()
If SetExitCode(Text1.Text) = 0 Then
MsgBox "设置退出代码true"
Else
MsgBox "设置退出代码false"
End If
End Sub
'函数返回0成功. 1失败, exitcode应该小于&H80
Private Function SetExitCode(ByVal exitcode As Byte) As Long
Static patchAddr As Long
SetExitCode = 1
If patchAddr = 0 Then
Dim tmp As Long, hModuleVB As Long, searchLen As Long
Dim searchs1(3) As Byte, searchs2(7) As Byte, strobj(3) As Byte
Dim searchStr As String
Dim pehead As Long, oldprotect As Long
searchLen = &H10000
hModuleVB = GetModuleHandle("msvbvm60.dll")
If hModuleVB = 0 Then Exit Function
CopyMemory VarPtr(pehead), hModuleVB, 4
tmp = GetModuleHandle("Kernel32.dll")
If tmp = 0 Then Exit Function
tmp = GetProcAddress(tmp, "ExitProcess")
If tmp = 0 Then Exit Function
CopyMemory VarPtr(searchs1(0)), VarPtr(tmp), 4
'set the str point to image head, modify pehead to searchlen
VirtualProtect hModuleVB, 4, &H40, oldprotect
CopyMemory VarPtr(pehead), VarPtr(searchLen), 1
CopyMemory VarPtr(strobj(0)), VarPtr(searchStr), 4
hModuleVB = hModuleVB + 4
CopyMemory VarPtr(searchStr), VarPtr(hModuleVB), 4
hModuleVB = hModuleVB - 4
'restore the str point and the pehead
CopyMemory hModuleVB, VarPtr(pehead), 4
CopyMemory VarPtr(searchStr), VarPtr(strobj(0)), 4
VirtualProtect hModuleVB, 4, oldprotect, oldprotect
If tmp = 0 Then Exit Function
tmp = tmp - 1 + 4 + hModuleVB
'restore the str point and the pehead
CopyMemory VarPtr(searchStr), VarPtr(strobj(0)), 4
CopyMemory hModuleVB, VarPtr(pehead), 4
VirtualProtect hModuleVB, 4, oldprotect, oldprotect
If tmp = 0 Then Exit Function
patchAddr = tmp - 1 + 4 + hModuleVB + 1
End If
If exitcode >= &H80 Then Exit Function
VirtualProtect patchAddr, 1, &H40, oldprotect
CopyMemory patchAddr, VarPtr(exitcode), 1
VirtualProtect patchAddr, 1, oldprotect, oldprotect
SetExitCode = 0
End Function
他没有一个象VC里的exitprocess(N)这样的函数吗?就是程序执行完成,我让他返回一个0或1
VOID ExitProcess(
UINT uExitCode // exit code for all threads
);
Parameters
uExitCode
Specifies the exit code for the process, and for all threads that are terminated as a result of this call. Use the GetExitCodeProcess function to retrieve the process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.
Return Values
This function does not return a value.