首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel中的ShellExecuteEx崩溃

Excel中的ShellExecuteEx崩溃
EN

Stack Overflow用户
提问于 2015-09-01 14:22:10
回答 1查看 2.4K关注 0票数 3

自从Windows更新发生后,对ShellExecuteEx(sExecuteInfo)的API调用就会崩溃,它说:

0x75F7A529 ( Access 32.dll)处的未处理异常访问冲突读取位置0x68686903

我不知道这里出了什么问题,你能帮帮我吗?

定义:

代码语言:javascript
复制
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

代码:

代码语言:javascript
复制
Private Function ShellAndWait(ByVal szProgram As String, Optional ByVal szOptions As String, Optional ByVal iWindowState As Integer = vbHide) As Boolean

Dim lTaskID As Long
Dim lReturn As Long
Dim lExitCode As Long
Dim lResult As Long
Dim bShellAndWait As Boolean
Dim hInstance As Object
Dim lPriority As Long

On Error GoTo ErrorHandler

Dim sExecuteInfo As SHELLEXECUTEINFO
sExecuteInfo.cbSize = Len(sExecuteInfo)
sExecuteInfo.lpVerb = "open"
sExecuteInfo.lpFile = szProgram
sExecuteInfo.lpParameters = szOptions
sExecuteInfo.nShow = &H7    ' Parameter SW_SHOWMINNOACTIVE,     (0x7)       , displays the window as a minimized window. The active window remains active.
sExecuteInfo.fMask = &H8140 ' Parameter SEE_MASK_NO_CONSOLE     (0x00008000), use to inherit the parent's console for the new process instead of having it create a new console
                            ' Parameter SEE_MASK_NOASYNC        (0x00000100), wait for the execute operation to complete before returning.
                            ' Parameter SEE_MASK_NOCLOSEPROCESS (0x00000040), puts process id back in sExecuteInfo.hProcess
lPriority = &H100000 'PROCESS_MODE_BACKGROUND_BEGIN
lReturn = ShellExecuteEx(sExecuteInfo)
'Loop while the shelled process is still running.
Do
    'lExitCode will be set to STILL_ACTIVE as long as the shelled process is running.
    lResult = GetExitCodeProcess(sExecuteInfo.hProcess, lExitCode)
    DoEvents
    'iCount = iCount + 1
    'Application.StatusBar = Str(iCount) + " iteration waited"
Loop While lExitCode = STILL_ACTIVE
bShellAndWait = True
Exit Function

ErrorHandler:
sErrMsg = Err.Description
bShellAndWait = False
End Function
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-02 08:33:20

微软推荐的使用方式。

MSDN文章“确定炮制过程何时结束”通过不同的API调用推荐方法,我可以确认它是可靠工作的。我在这里张贴代码模块(从本文中的代码中采用)。

以下VBA模块使用API调用CreateProcessA(),等待应用程序完成并返回ERRORLEVEL代码:

代码语言:javascript
复制
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 ),则可以将其称为

代码语言:javascript
复制
Dim errorLevelValue As Long
errorLevelValue = ShellExecModule.ExecCmd(CommandLine)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32334110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档