Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessId As Long dwThreadId As Long End TypePrivate 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 Byte hStdInput As Long hStdOutput As Long hStdError As Long End TypePrivate Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As LongPrivate Sub cmdRun_Click() Dim lp As PROCESS_INFORMATION Dim si As STARTUPINFO si.cb = Len(si) CreateProcess vbNullString, "D:\AOEII\age2_x1.exe", ByVal 0&, ByVal 0&, 0, &H80, ByVal 0&, vbNullString, si, lp End Sub
这个代码是API的示例'添加 Command1 CommonDialog1Const INFINITE = &HFFFF Const STARTF_USESHOWWINDOW = &H1 Private Enum enSW SW_HIDE = 0 SW_NORMAL = 1 SW_MAXIMIZE = 3 SW_MINIMIZE = 6 End Enum Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessId As Long dwThreadId As Long End Type Private 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 Byte hStdInput As Long hStdOutput As Long hStdError As Long End Type Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type Private Enum enPriority_Class NORMAL_PRIORITY_CLASS = &H20 IDLE_PRIORITY_CLASS = &H40 HIGH_PRIORITY_CLASS = &H80 End Enum Private 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 Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Sub Command1_Click() On Error GoTo errhandler CommonDialog1.DialogTitle = "请选择一个文件" CommonDialog1.CancelError = True CommonDialog1.Filter = "可执行文件(*.exe)|*.exe" CommonDialog1.ShowOpen SuperShell CommonDialog1.FileName, Left$(CommonDialog1.FileName, Len(CommonDialog1.FileName) - Len(CommonDialog1.FileTitle)), 0, SW_NORMAL, HIGH_PRIORITY_CLASS errhandler: If Err > 0 Then Exit Sub End SubPrivate Function SuperShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean Dim pclass As Long Dim sinfo As STARTUPINFO Dim pinfo As PROCESS_INFORMATION Dim sec1 As SECURITY_ATTRIBUTES Dim sec2 As SECURITY_ATTRIBUTES sec1.nLength = Len(sec1) sec2.nLength = Len(sec2) sinfo.cb = Len(sinfo) sinfo.dwFlags = STARTF_USESHOWWINDOW sinfo.wShowWindow = start_size pclass = Priority_Class If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, 0&, WorkDir, sinfo, pinfo) Then WaitForSingleObject pinfo.hProcess, dwMilliseconds SuperShell = True Else SuperShell = False End If End Function
把Command1改为下面代码Private Sub Command1_Click() Dim aa$, jj% aa = "d:\age2_x1.exe" jj = InStrRev(aa, "\") SuperShell aa, Left(aa, Len(Mid(aa, 1, jj))), 0, SW_NORMAL, HIGH_PRIORITY_CLASS End Sub
哈哈,当然还要啊, 我是说把 Private Sub Command1_Click() 这事件的代码改掉,其它不动.
//http://www.m5home.com/bbs/dispbbs.asp?boardID=10&ID=265&page=1找到里面的"API三把枪",不少API有中文说明,有些直接有例子.
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End TypePrivate 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 Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End TypePrivate Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As LongPrivate Sub cmdRun_Click()
Dim lp As PROCESS_INFORMATION
Dim si As STARTUPINFO
si.cb = Len(si)
CreateProcess vbNullString, "D:\AOEII\age2_x1.exe", ByVal 0&, ByVal 0&, 0, &H80, ByVal 0&, vbNullString, si, lp
End Sub
上面的那些代码好用吗?我怎么不好用?
顺便问一下,打开“帝国时代”这类游戏为什么用shell语句打不开,一点击游戏就会出错,是不是用别的语句呀?
你是通常是怎么打开“帝国时代”的?是否是快捷方式?如是,看看运行参数
Const STARTF_USESHOWWINDOW = &H1
Private Enum enSW
SW_HIDE = 0
SW_NORMAL = 1
SW_MAXIMIZE = 3
SW_MINIMIZE = 6
End Enum
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private 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 Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Enum enPriority_Class
NORMAL_PRIORITY_CLASS = &H20
IDLE_PRIORITY_CLASS = &H40
HIGH_PRIORITY_CLASS = &H80
End Enum
Private 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
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Sub Command1_Click()
On Error GoTo errhandler
CommonDialog1.DialogTitle = "请选择一个文件"
CommonDialog1.CancelError = True
CommonDialog1.Filter = "可执行文件(*.exe)|*.exe"
CommonDialog1.ShowOpen
SuperShell CommonDialog1.FileName, Left$(CommonDialog1.FileName, Len(CommonDialog1.FileName) - Len(CommonDialog1.FileTitle)), 0, SW_NORMAL, HIGH_PRIORITY_CLASS
errhandler:
If Err > 0 Then Exit Sub
End SubPrivate Function SuperShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
Dim pclass As Long
Dim sinfo As STARTUPINFO
Dim pinfo As PROCESS_INFORMATION
Dim sec1 As SECURITY_ATTRIBUTES
Dim sec2 As SECURITY_ATTRIBUTES
sec1.nLength = Len(sec1)
sec2.nLength = Len(sec2)
sinfo.cb = Len(sinfo)
sinfo.dwFlags = STARTF_USESHOWWINDOW
sinfo.wShowWindow = start_size
pclass = Priority_Class
If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, 0&, WorkDir, sinfo, pinfo) Then
WaitForSingleObject pinfo.hProcess, dwMilliseconds
SuperShell = True
Else
SuperShell = False
End If
End Function
//呵呵~~如果你是程序员,在这里这样问问题一定受到BS而且,在CSDN被人BS是很正常的,你能顶住这些,然后在CSDN混上个几个月个把年的,一定能学有所成~~:)扯远了~~嘿嘿.关于你的问题,主要原因在于初始目录的问题吧.楼上的Left$(CommonDialog1.FileName, Len(CommonDialog1.FileName) - Len(CommonDialog1.FileTitle))这一句就是取这个目录的.按这样调用就应该会OK~~顶住呀,嘿嘿~~
Dim aa$, jj%
aa = "d:\age2_x1.exe"
jj = InStrRev(aa, "\")
SuperShell aa, Left(aa, Len(Mid(aa, 1, jj))), 0, SW_NORMAL, HIGH_PRIORITY_CLASS
End Sub