函数 ExecCmd,以前在 access 2007 中工作,但在 access 2013(64 位)中不再工作

Posted

技术标签:

【中文标题】函数 ExecCmd,以前在 access 2007 中工作,但在 access 2013(64 位)中不再工作【英文标题】:Function ExecCmd, used to work in access 2007 but no more in access 2013 (64bit) 【发布时间】:2014-01-01 11:40:12 【问题描述】:

我曾经能够在我的 access 2007 db 中使用以下功能运行运行外部程序(如 exiftool 或 image magick)的命令行。 我迁移到了 2013 年的访问,经过一些代码修改后,数据库可以正常工作,除了这个函数 ExecCmd。当我使用它时,我没有收到任何错误,但没有任何反应。

谁能帮忙?要么告诉我哪里出了问题,要么提出更好的方法来做同样的事情。

Public Const SEE_MASK_DOENVSUBST As Long = &H200
Public Const SEE_MASK_IDLIST As Long = &H4
Public Const SEE_MASK_NOCLOSEPROCESS As Long = &H40
Public Const SW_HIDE As Long = 0
Public Const SW_SHOW As Long = 5
Public Const WAIT_TIMEOUT As Long = 258&

Public Type SHELLEXECUTEINFOA
    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
Public Declare PtrSafe Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Public Declare PtrSafe Function GetExitCodeProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByRef lpExitCode As Long) As Long
Public Declare PtrSafe Function ShellExecuteEx Lib "shell32.dll" (ByRef lpExecInfo As SHELLEXECUTEINFOA) As Long
Public Declare PtrSafe Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long



Public Function ExecCmd(ByVal vsCmdLine As String, Optional ByRef vsParameters As String, Optional ByRef vsCurrentDirectory As String = vbNullString, Optional ByVal vnShowCmd As Long = SW_SHOW, Optional ByVal vnTimeOut As Long = 200) As Long
    Dim lpShellExInfo As SHELLEXECUTEINFOA
        With lpShellExInfo
            .cbSize = Len(lpShellExInfo)
            .lpDirectory = vsCurrentDirectory
            .lpVerb = "open"
            .lpFile = vsCmdLine
            .lpParameters = vsParameters
            .nShow = vnShowCmd
            .fMask = SEE_MASK_DOENVSUBST Or SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_IDLIST
        End With

        If ShellExecuteEx(lpShellExInfo) Then
            Do While WaitForSingleObject(lpShellExInfo.hProcess, vnTimeOut) = WAIT_TIMEOUT
                DoEvents
            Loop

            GetExitCodeProcess lpShellExInfo.hProcess, ExecCmd
            CloseHandle lpShellExInfo.hProcess
        Else
            ExecCmd = vbError
        End If
    End Function

我找到了另一个类似的功能,但第一个更好,因为它能够运行隐藏的命令。 这有效:

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 PtrSafe Function WaitForSingleObject Lib "kernel32" (ByVal _
 hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare PtrSafe Function CreateProcessA Lib "kernel32" (ByVal _
 lpApplicationName As Long, 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 Long, _
 lpStartupInfo As STARTUPINFO, lpProcessInformation As _
 PROCESS_INFORMATION) As Long

Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal _
 hObject As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

Public Sub ExecCmd(cmdline As String)
 Dim proc As PROCESS_INFORMATION
 Dim start As STARTUPINFO
 Dim ReturnValue As Integer

 ' Initialize the STARTUPINFO structure:
 start.cb = Len(start)

 ' Start the shelled application:
 ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
 NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

 ' Wait for the shelled application to finish:
 Do
 ReturnValue = WaitForSingleObject(proc.hProcess, 0)
 DoEvents
 Loop Until ReturnValue <> 258

 ReturnValue = CloseHandle(proc.hProcess)
End Sub

【问题讨论】:

【参考方案1】:

我能够使用一个简单的测试用例重现您的问题。 VBA 程序...

Sub test()
    Dim r As Variant
    r = ExecCmd("cscript.exe", "C:\Users\Public\Documents\foo.vbs", "", 0)
End Sub

...在 32 位 Access 2013 下运行良好,但在 64 位 Access 2013 下静默失败。但是,以下代码似乎在 64 位 Access 2013 下运行:

Sub test2()
    Dim sh As Object  ' WshShell
    Set sh = CreateObject("WScript.Shell")
    sh.Run "cscript.exe C:\Users\Public\Documents\foo.vbs", 0
    Set sh = Nothing
End Sub

有关详细信息,请参阅

Run Method (Windows Script Host)

【讨论】:

【参考方案2】:

问题已解决:64 位 API 调用不同。 下面的代码有效:appli 启动,代码等待它完成,然后再继续。 最重要的是:一个参数控制应用程序窗口的可见性:对于运行一批后台命令行进程而不会污染显示或焦点非常有用。

感谢您的帮助!

Private Const STARTF_USESHOWWINDOW& = &H1
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

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 PtrSafe Function WaitForSingleObject Lib "kernel32" (ByVal _
    hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Private Declare PtrSafe Function CreateProcessA Lib "kernel32" (ByVal _
    lpApplicationName As Long, 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 Long, _
    lpStartupInfo As STARTUPINFO, lpProcessInformation As _
    PROCESS_INFORMATION) As Long

Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal _
    hObject As Long) As Long

Public Sub ShellWait(Pathname As String, Optional WindowStyle As Long)
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim ret As Long
    ' Initialize the STARTUPINFO structure:
    With start
        .cb = Len(start)
        If Not IsMissing(WindowStyle) Then
            .dwFlags = STARTF_USESHOWWINDOW
            .wShowWindow = WindowStyle
        End If
    End With
    ' Start the shelled application:
    ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
            NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    ' Wait for the shelled application to finish:
    ret& = WaitForSingleObject(proc.hProcess, INFINITE)
    ret& = CloseHandle(proc.hProcess)
End Sub

【讨论】:

以上是关于函数 ExecCmd,以前在 access 2007 中工作,但在 access 2013(64 位)中不再工作的主要内容,如果未能解决你的问题,请参考以下文章

将 MS Access 2000 转换为 2010

MS Access Runtime 2010 在安装了以前版本的 Access/Office 的系统上能否正常运行?

线程 1:EXC_BAD_ACCESS(代码=1,地址=0x200)

如何创建将传递查询导出为 XML 文件的 VBA 函数(用于 Access 2010)?

Access 2013 - 无法打开使用以前版本的应用程序创建的数据库

nginx access.log中, 200 状态码 后面的那个数字是啥意思