Excel VBA中的ShellExecuteEx崩溃
自Windows更新发生后,对ShellExecuteEx(sExecuteInfo)的API调用崩溃,说明: 0x75F7A529(shell32.dll)访问冲突处未处理的异常 读取位置0x686903 我不知道这里怎么了,你能帮我吗 定义:Excel VBA中的ShellExecuteEx崩溃,excel,vba,shellexecuteex,shell32.dll,Excel,Vba,Shellexecuteex,Shell32.dll,自Windows更新发生后,对ShellExecuteEx(sExecuteInfo)的API调用崩溃,说明: 0x75F7A529(shell32.dll)访问冲突处未处理的异常 读取位置0x686903 我不知道这里怎么了,你能帮我吗 定义: Private Type SHELLEXECUTEINFO cbSize As Long fMask As Long Hwnd As Long lpVerb As String lpFile
Private Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
Hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Private Const STILL_ACTIVE As Long = &H103 ' Constant for the lpExitCode parameter of the GetExitCodeProcess API function.
Private Declare PtrSafe Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpExecInfo As SHELLEXECUTEINFO) As Long
代码:
使用微软推荐的方法。
通过不同的API调用推荐方法,我可以确认它工作可靠。我在这里发布代码模块(采用本文中的代码)
以下VBA模块使用API调用CreateProcessA()
,等待应用程序完成并返回ERRORLEVEL代码作为结果:
Option Explicit
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 Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (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
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Public Function ExecCmd(CommandLine As String) As Long
Dim proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO
Dim ret As Long
' Initialize the STARTUPINFO structure:
Start.cb = Len(Start)
' Start the shelled application:
ret& = CreateProcessA(vbNullString, CommandLine, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, Start, proc)
' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, ret&)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = ret&
End Function
如果将所有代码存储为例如ShellExecModule
,则可以将其称为
Dim errorLevelValue As Long
errorLevelValue = ShellExecModule.ExecCmd(CommandLine)
Len(sExecuteInfo)返回什么?sExecuteInfo.cbSize=60 Office 32位或64位的可能副本?另外,请您留下一句话,为什么您决定使用
ShellExecuteEx
而不是ShellExecuteEx
?@miroxlav:特别是ShellExecuteExA
——VBA不是Unicode吗?谢谢您提供的信息,我确实应用了它,它工作得很顺利。
Dim errorLevelValue As Long
errorLevelValue = ShellExecModule.ExecCmd(CommandLine)