模块中 Option ExplicitPublic Const PROCESS_TERMINATE = &H1Public 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 TypePublic Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessId As Long dwThreadId As Long End TypeDeclare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Long, lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPublic Processinfo As PROCESS_INFORMATION Public PathName As StringPublic Sub FindOther() Dim r As Long r = OpenProcess(PROCESS_TERMINATE, ByVal 1, ByVal Processinfo.dwProcessId) If r = 0 Then Dim StartInfo As STARTUPINFO
End If CloseHandle r End Sub窗体中 Option ExplicitPrivate Sub Form_Load() PathName = App.Path If Right(PathName, 1) <> "\" Then PathName = PathName & "\" End SubPrivate Sub Timer1_Timer() FindOther End Sub'我想实现的目的就是想让SB.exe不被关掉 '我发现使用这种方法,要是SB.exe比较简单还行,要是稍微复杂点就不行了,会使SB.exe无法正常运行!但是直接运SB.exe又很正常! '我想可能是我那个创建进程的代码有点问题 '说实话那个语句我不是很明白,基本是把书上的代码经过自己理解后改过来的,所以想请各位高手帮忙指导一下。
有时候的确会经常遇到编译好后的程序不能正常运行特别在其他电脑上。
Option ExplicitPublic Const PROCESS_TERMINATE = &H1Public 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 TypePublic Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End TypeDeclare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Long, lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPublic Processinfo As PROCESS_INFORMATION
Public PathName As StringPublic Sub FindOther()
Dim r As Long
r = OpenProcess(PROCESS_TERMINATE, ByVal 1, ByVal Processinfo.dwProcessId)
If r = 0 Then
Dim StartInfo As STARTUPINFO
CreateProcess PathName & "SB.exe", vbNullString, ByVal 0, ByVal 0, ByVal 1, &H20, 0, vbNullString, StartInfo, Processinfo
CloseHandle Processinfo.hProcess
CloseHandle Processinfo.hThread
End If
CloseHandle r
End Sub窗体中
Option ExplicitPrivate Sub Form_Load()
PathName = App.Path
If Right(PathName, 1) <> "\" Then PathName = PathName & "\"
End SubPrivate Sub Timer1_Timer()
FindOther
End Sub'我想实现的目的就是想让SB.exe不被关掉
'我发现使用这种方法,要是SB.exe比较简单还行,要是稍微复杂点就不行了,会使SB.exe无法正常运行!但是直接运SB.exe又很正常!
'我想可能是我那个创建进程的代码有点问题
'说实话那个语句我不是很明白,基本是把书上的代码经过自己理解后改过来的,所以想请各位高手帮忙指导一下。
不知道各位高手还能不能从我的代码中找出错误,必将感激不尽!