VBA API调用在一台计算机上运行时返回值,但在另一台计算机上运行时不返回值

时间:2013-11-19 21:54:32

标签: api vba

我有一个API调用,它从外部列表视图(Java应用程序)返回一个值,该值在我的计算机上运行正常。但是,当我尝试在同事的计算机上运行相同的调用时,它只返回“0”。我们在办公室工作,我们的计算机是相同的 - 相同型号,相同版本的Windows,相同版本的MS Office(使用Excel)。具有API调用的文件驻留在共享网络位置,因此它是完全相同的文件,与我在两台计算机上运行的完全相同的代码。我不是很精通API,所以我可能会遗漏一些简单的东西,但我无法解决它。任何人都可以告诉我为什么代码在一台机器上工作但不在另一台机器上工作?

以下是调用的代码 - 请参阅我关于中途的说明,以显示出现差异的地方:

Public Function GetListviewItem(ByVal hWindow, pRow) As String

    Dim result              As Long
    Dim myItem              As LV_ITEM
    Dim pHandle             As Long
    Dim pStrBufferMemory    As Long
    Dim pMyItemMemory       As Long
    Dim strBuffer()         As Byte
    Dim index               As Long
    Dim tmpstring           As String
    Dim strLength           As Long
    Dim pColumn As Long
    Dim lProcID As Long

    '**********************
    'init the string buffer
    '**********************
    ReDim strBuffer(MAX_LVMSTRING)
    pColumn = 2
    '***********************************************************
    'open a handle to the process and allocate the string buffer
    '***********************************************************
    GetWindowThreadProcessId hWindow, lProcID ' Get the process ID in which the ListView is running
    If lProcID <> 0 Then
        pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, lProcID)
        pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)

        '************************************************************************************
        'initialize the local LV_ITEM structure
        'The myItem.iSubItem member is set to the index of the column that is being retrieved
        '************************************************************************************
        myItem.mask = LVIF_TEXT
        myItem.iItem = pRow
        myItem.iSubitem = pColumn
        myItem.pszText = pStrBufferMemory
        myItem.cchTextMax = MAX_LVMSTRING

        '**********************************************************
        'write the structure into the remote process's memory space
        '**********************************************************
        pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)
        result = WriteProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)

        '*************************************************************
        'send the get the item message and write back the memory space
        '*************************************************************
    'NOTE: THE 'result' VARIABLE IS WHERE I GET VALUES, BUT THE OTHER COMPUTER GETS '0'

        result = SendMessage(hWindow, LVM_GETITEMTEXTA, pRow, ByVal pMyItemMemory)
        result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), MAX_LVMSTRING, 0)
        result = ReadProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)

        '**************************************************
        'turn the byte array into a string and send it back
        '**************************************************
        For index = LBound(strBuffer) To UBound(strBuffer)
            If Chr(strBuffer(index)) = vbNullChar Then Exit For
            tmpstring = tmpstring & Chr(strBuffer(index))
        Next index

        tmpstring = Trim(tmpstring)
        '***********************************************
        'deallocate the memory and close the process handle
        '**************************************************
        result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
        result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)

        result = CloseHandle(pHandle)

        If Len(tmpstring) > 0 Then GetListviewItem = tmpstring
    End If

End Function

感谢任何可能提出一些建议的人。

0 个答案:

没有答案