【问题标题】:Excel Mac OS libc.dylib popen returns no dataExcel Mac OS libc.dylib popen 不返回数据
【发布时间】:2021-01-31 06:45:42
【问题描述】:

我正在使用来自here 的 Excel vba execShell 脚本在 Excel for Mac 中使用 curl 从 Web 服务器中提取数据。它在我自己的 Mac(以及我一直在测试的许多其他 Mac)上运行良好。

但是,在朋友 Mac(相同(最新)版本的 Mac OS 和 Excel 365)上,它会失败。似乎在他的 Mac 上 execShell 返回空字符串。

我简化了脚本,只运行echo 命令并体验相同的行为。这是我正在运行的测试脚本

Private Declare PtrSafe Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As LongPtr
Private Declare PtrSafe Function pclose Lib "libc.dylib" (ByVal file As LongPtr) As Long
Private Declare PtrSafe Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long
Private Declare PtrSafe Function feof Lib "libc.dylib" (ByVal file As LongPtr) As LongPtr

Function execShell(ByVal command As String, Optional ByRef exitCode As Long) As String
    Dim file As LongPtr
    file = popen(command, "r")

    If file = 0 Then
        Exit Function
    End If

    While feof(file) = 0
        Dim chunk As String
        Dim read As Long
        chunk = Space(50)
        read = fread(chunk, 1, Len(chunk) - 1, file)
        If read > 0 Then
            chunk = Left$(chunk, read)
            execShell = execShell & chunk
        End If
    Wend

    exitCode = pclose(file)
End Function

Sub test_command()
    Dim errorCode As Long
    result = execShell("echo Value_returned", errorCode)
    msgbox(result & " " & errorCode)
    
End Sub

在我的 Mac 上,test_command 显示

Value_returned 
0 

在他的 Mac 上只显示

0

result = "" 返回True

任何想法可能导致这种行为?

任何不需要更新我的朋友操作系统的解决方法?

非常感谢。

【问题讨论】:

    标签: excel vba macos popen libc


    【解决方案1】:

    dugsmith 对这个post 的第二次回复在 64 位 Mac 上对我来说一直有效,并且可能对你和你的朋友也有效。此处重复的代码完全归功于其他人!

    Private Declare PtrSafe Function web_popen Lib "libc.dylib" Alias "popen" (ByVal command As String, ByVal mode As String) As LongPtr
    Private Declare PtrSafe Function web_pclose Lib "libc.dylib" Alias "pclose" (ByVal file As LongPtr) As Long
    Private Declare PtrSafe Function web_fread Lib "libc.dylib" Alias "fread" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long
    Private Declare PtrSafe Function web_feof Lib "libc.dylib" Alias "feof" (ByVal file As LongPtr) As LongPtr
    
    Public Function executeInShell(web_Command As String) As String
    
        Dim web_File As LongPtr
        Dim web_Chunk As String
        Dim web_Read As Long
    
        On Error GoTo web_Cleanup
    
        web_File = web_popen(web_Command, "r")
    
        If web_File = 0 Then
            Exit Function
        End If
    
        Do While web_feof(web_File) = 0
            web_Chunk = VBA.Space$(50)
            web_Read = web_fread(web_Chunk, 1, Len(web_Chunk) - 1, web_File)
            If web_Read > 0 Then
                web_Chunk = VBA.Left$(web_Chunk, web_Read)
                executeInShell = executeInShell & web_Chunk
            End If
        Loop
    
    web_Cleanup:
    
        web_pclose (web_File)
    
    End Function
    
    
    Function getHTTP(sUrl As String, sQuery As String) As String
        Dim sCmd As String
        Dim sResult As String
        Dim lExitCode As Long
        sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
        sResult = executeInShell(sCmd)
        getHTTP = sResult
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      相关资源
      最近更新 更多