同样非常简单的程序,CreateProcessA就能执行,CreateProcessW的话总是提示找不到指定的文件.Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessW" _
(ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long调用如下:
ret& = CreateProcess( _
"C:\notepad.exe", _
0&, _
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
cmddir$, _
start, _
proc)
返回值为2,找不到指定的文件.
(ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long调用如下:
ret& = CreateProcess( _
"C:\notepad.exe", _
0&, _
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
cmddir$, _
start, _
proc)
返回值为2,找不到指定的文件.
strconv("C:\notepad.exe",64), _
0&, _
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
cmddir$, _
start, _
proc)
W是所有字符串都使用Unicode。
CreateProcess中的各个参数都要注意,包含相关结构定义。
难道 "C:\notepad.exe"这样的字符串在VB中不是unicode吗?
据我所知,VB本身是unicode编码的.
一会儿试一下.
按照楼上指教的方法,试了一下。
①可以成功弹出notepad②当我想执行"C:\notepad.exe C:\newFolder.txt"这条语句时,
能弹出notepad,但无法打开newFolder.txt.此时无错误值③当把notepad.exe拷到"C:\森",下面时,无法弹出notepad,
Err.LastDllError值为3,"The system cannot find the path specified."请指教。
以下分别为①,②,③的执行代码。
①
ret& = CreateProcess( _
StrConv("C:\notepad.exe", vbUnicode), _
0&, _
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
StrConv("C:\", vbUnicode), _
start, _
proc)②
ret& = CreateProcess( _
StrConv("C:\notepad.exe", vbUnicode), _
StrConv("C:\newFolder.txt", vbUnicode), _
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
StrConv("C:\", vbUnicode), _
start, _
proc)
③
ret& = CreateProcess( _
StrConv("C:\森\notepad.exe", vbUnicode), _
0&, _
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
StrConv("C:\森\", vbUnicode), _
start, _
proc)
ret& = CreateProcess( _
0&, _
StrConv("C:\notepad.exe C:\newFolder.txt", vbUnicode), _
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
VbNullString, _
start, _
proc)
③
ret& = CreateProcess( _
0&, _
StrConv("C:\森\notepad.exe", vbUnicode), _
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
VbNullString, _
start, _
proc)
多谢再次帮助不幸的是,按照楼上②、③的方式执行后还是不行。Err.LastDllError值还是2,提示找不到文件。 :(但在DOS命令行下,
"C:\notepad.exe C:\newFolder.txt"
和
C:\森\notepad.exe
都是能执行的。推断:
我的操作系统不是中文的关系?
倒数第一,第二参数的设置关题?
或是其它原因?
肯请诸位继续关注并回复,多谢。
Private Const READ_BUF_SIZE = 256Private Declare Function CreatePipe Lib "kernel32" ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As Any, _
ByVal nSize As Long) As LongPrivate Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
lpBuffer As Byte, _
ByVal nNumberOfBytesToRead As Long, _
ByRef lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Long) As LongPrivate Declare Function WaitForSingleObject Lib "kernel32" ( _
ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As LongPrivate Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End TypePrivate Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
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 TypePrivate Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessW" _
(ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As LongPrivate Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As LongPrivate Declare Function GetExitCodeProcess Lib "kernel32.dll" ( _
ByVal hProcess As Long, _
lpExitCode As Long) As LongPrivate Const SW_HIDE = 0
Private Const SW_SHOWNORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOW = 5Private Const INFINITE = -1&
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Const STARTF_USESHOWWINDOW = &H1&Private Const ERROR_BROKEN_PIPE = 109Private Const STILL_ACTIVE = &H103&
Private Const CREATE_UNICODE_ENVIRONMENT = &H400&Declare Sub Sleep Lib "kernel32" (ByVal millisec As Long)Private Sub Wait(process As Long)
Dim exitCode As Long
Dim rc As Long
Do
rc = GetExitCodeProcess(process, exitCode)
DoEvents
Sleep 100
Loop While exitCode = STILL_ACTIVE
End Sub
Public Function ExecCmdWithStream(ByVal cmdline$, ByVal cmddir$, ByRef so As String, ByRef eo As String, showWindow As Boolean) As Long Dim proc As PROCESS_INFORMATION, ret As Long, bSuccess As Long
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES, hReadPipe As Long, hWritePipe As Long
Dim hReadPipe2 As Long, hWritePipe2 As Long
Dim i As Integer, retcode As Long
Dim buf As String
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
ret = CreatePipe(hReadPipe, hWritePipe, sa, PIPE_SIZE)
If ret = 0 Then
'ErrorLog Nothing,
ExecCmdWithStream = -1
Exit Function
End If ret = CreatePipe(hReadPipe2, hWritePipe2, sa, PIPE_SIZE)
If ret = 0 Then
'ErrorLog Nothing
ExecCmdWithStream = -1
Exit Function
End If
start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
If showWindow Then
start.wShowWindow = SW_SHOWNORMAL
Else
start.wShowWindow = SW_HIDE
End If
start.hStdOutput = hWritePipe
start.hStdError = hWritePipe2
Debug.Print cmdline
cmdline = StrConv(cmdline, vbUnicode)
cmddir = StrConv(cmddir, vbUnicode)
Debug.Print cmdline
' Start the shelled application:
' ret& = CreateProcess(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, cmddir$, start, proc) ret& = CreateProcess( _
0&, _
StrConv("C:\森\notepad.exe", vbUnicode),_
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
VbNullString, _
start, _
proc) If ret = 0 Then
' Err.LastDllError
ExecCmdWithStream = -1
Exit Function
End If
Wait proc.hProcess
' Set Return Code
ret& = GetExitCodeProcess(proc.hProcess, retcode)
ExecCmdWithStream = retcode
ret& = CloseHandle(proc.hProcess)
ret& = CloseHandle(proc.hThread)
ret& = CloseHandle(hWritePipe)
ret& = CloseHandle(hWritePipe2)
ret& = GetStringFromPipe(hReadPipe, buf)
so = buf
ret& = GetStringFromPipe(hReadPipe2, buf)
eo = bufEnd FunctionPrivate Function GetStringFromPipe(hPipe As Long, ByRef str As String) As Long
Dim bytesread As Long
Dim mybuff(0 To READ_BUF_SIZE - 1) As Byte
Dim bSuccess As Long
Dim buf As String
Dim x As String
buf = ""
Do
Erase mybuff()
bSuccess = ReadFile(hPipe, mybuff(0), READ_BUF_SIZE, bytesread, 0&)
If bSuccess = 0 Then
If err.LastDllError = ERROR_BROKEN_PIPE Then
Exit Do
Else
'ErrorLog Nothing
str = ""
GetStringFromPipe = err.LastDllError
Exit Function
End If
End If
If bytesread = 0 Then
Exit Do
End If
x = StrConv(mybuff, vbUnicode)
x = Replace(x, Chr$(0), "")
buf = buf & Trim(x)
Loop
str = buf
GetStringFromPipe = 0
End Function
0&, _
StrConv("C:\森\notepad.exe", vbUnicode),_
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
VbNullString, _
start, _
proc) 改成
ret& = CreateProcess( _
VbNullString, _
StrConv("C:\森\notepad.exe", vbUnicode),_
0&, _
0&, _
1&, _
CREATE_UNICODE_ENVIRONMENT, _
0&, _
VbNullString, _
start, _
proc)
此时Err.LastDllError值为3,The system cannot find the path specified.是不是中文路径被StrConv转换后,发生了转码之类的问题。
对于记事本进程,这个路径好像应该是“C:\WINDOWS\system32\”吧?
lz试一下看看……