如何将该程序转换为在 64 位机器上运行?

发布于 2024-11-14 06:41:51 字数 3837 浏览 2 评论 0原文

该代码必须在 64 位机器上运行,但目前还不能。我需要修复什么才能使该脚本正常工作?

Option Explicit

''' *************************************************************************
''' Module Constant Declaractions Follow
''' *************************************************************************
''' Constant for the dwDesiredAccess parameter of the OpenProcess API function.
Private Const PROCESS_QUERY_INFORMATION As Long = &H400
''' Constant for the lpExitCode parameter of the GetExitCodeProcess API function.
Private Const STILL_ACTIVE As Long = &H103


''' *************************************************************************
''' Module Variable Declaractions Follow
''' *************************************************************************
''' It's critical for the shell and wait procedure to trap for errors, but I
''' didn't want that to distract from the example, so I'm employing a very
''' rudimentary error handling scheme here. This variable is used to pass error
''' messages between procedures.
Public gszErrMsg As String


''' *************************************************************************
''' Module DLL Declaractions Follow
''' *************************************************************************
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long


Public Sub ShellAndWait()

    On Error GoTo ErrorHandler

    ''' Clear the error mesaage variable.
    gszErrMsg = vbNullString
    If Not bShellAndWait("java TimeTable " & Environ("Username"), vbNormalFocus) Then Err.Raise 9999

    Exit Sub

ErrorHandler:
    ''' If we ran into any errors this will explain what they are.
    MsgBox gszErrMsg, vbCritical, "Shell and Wait Demo"
End Sub


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Comments:   Shells out to the specified command line and waits for it to
'''             complete. The Shell function runs asynchronously, so you must
'''             run it using this function if you need to do something with
'''             its output or wait for it to finish before continuing.
'''
''' Arguments:  szCommandLine   [in] The command line to execute using Shell.
'''             iWindowState    [in] (Optional) The window state parameter to
'''                             pass to the Shell function. Default = vbHide.
'''
''' Returns:    Boolean         True on success, False on error.
'''
''' Date        Developer       Action
''' --------------------------------------------------------------------------
''' 05/19/05    Rob Bovey       Created
'''
Private Function bShellAndWait(ByVal szCommandLine As String, Optional ByVal iWindowState As Integer = vbHide) As Boolean

    Dim lTaskID As Long
    Dim lProcess As Long
    Dim lExitCode As Long
    Dim lResult As Long

    On Error GoTo ErrorHandler

    ''' Run the Shell function.
    lTaskID = Shell(szCommandLine, iWindowState)

    ''' Check for errors.
    If lTaskID = 0 Then Err.Raise 9999, , "Shell function error."

    ''' Get the process handle from the task ID returned by Shell.
    lProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, lTaskID)

    ''' Check for errors.
    If lProcess = 0 Then Err.Raise 9999, , "Unable to open Shell process handle."

    ''' 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(lProcess, lExitCode)
        DoEvents
    Loop While lExitCode = STILL_ACTIVE

    bShellAndWait = True
    Exit Function

ErrorHandler:
    gszErrMsg = Err.Description
    bShellAndWait = False
End Function

This code has to work on a 64 bit machine and it currently does not. What do I need to fix inorder for this script to work?

Option Explicit

''' *************************************************************************
''' Module Constant Declaractions Follow
''' *************************************************************************
''' Constant for the dwDesiredAccess parameter of the OpenProcess API function.
Private Const PROCESS_QUERY_INFORMATION As Long = &H400
''' Constant for the lpExitCode parameter of the GetExitCodeProcess API function.
Private Const STILL_ACTIVE As Long = &H103


''' *************************************************************************
''' Module Variable Declaractions Follow
''' *************************************************************************
''' It's critical for the shell and wait procedure to trap for errors, but I
''' didn't want that to distract from the example, so I'm employing a very
''' rudimentary error handling scheme here. This variable is used to pass error
''' messages between procedures.
Public gszErrMsg As String


''' *************************************************************************
''' Module DLL Declaractions Follow
''' *************************************************************************
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long


Public Sub ShellAndWait()

    On Error GoTo ErrorHandler

    ''' Clear the error mesaage variable.
    gszErrMsg = vbNullString
    If Not bShellAndWait("java TimeTable " & Environ("Username"), vbNormalFocus) Then Err.Raise 9999

    Exit Sub

ErrorHandler:
    ''' If we ran into any errors this will explain what they are.
    MsgBox gszErrMsg, vbCritical, "Shell and Wait Demo"
End Sub


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Comments:   Shells out to the specified command line and waits for it to
'''             complete. The Shell function runs asynchronously, so you must
'''             run it using this function if you need to do something with
'''             its output or wait for it to finish before continuing.
'''
''' Arguments:  szCommandLine   [in] The command line to execute using Shell.
'''             iWindowState    [in] (Optional) The window state parameter to
'''                             pass to the Shell function. Default = vbHide.
'''
''' Returns:    Boolean         True on success, False on error.
'''
''' Date        Developer       Action
''' --------------------------------------------------------------------------
''' 05/19/05    Rob Bovey       Created
'''
Private Function bShellAndWait(ByVal szCommandLine As String, Optional ByVal iWindowState As Integer = vbHide) As Boolean

    Dim lTaskID As Long
    Dim lProcess As Long
    Dim lExitCode As Long
    Dim lResult As Long

    On Error GoTo ErrorHandler

    ''' Run the Shell function.
    lTaskID = Shell(szCommandLine, iWindowState)

    ''' Check for errors.
    If lTaskID = 0 Then Err.Raise 9999, , "Shell function error."

    ''' Get the process handle from the task ID returned by Shell.
    lProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, lTaskID)

    ''' Check for errors.
    If lProcess = 0 Then Err.Raise 9999, , "Unable to open Shell process handle."

    ''' 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(lProcess, lExitCode)
        DoEvents
    Loop While lExitCode = STILL_ACTIVE

    bShellAndWait = True
    Exit Function

ErrorHandler:
    gszErrMsg = Err.Description
    bShellAndWait = False
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

八巷 2024-11-21 06:41:51

将其更改

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

为这样,它将在 32 位和 64 位上编译

#If Win64 Then
    Private Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
#Else
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
#End If

Change this

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

to this and it will compile on both 32-bit and 64-bit

#If Win64 Then
    Private Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
#Else
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
#End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文