Skip to content

cPLI 方法参考

🚀 核心方法

Request

调用外部程序并返回结果。

vb
Public Function Request(ParamArray Data() As Variant) As String

参数:

  • Data() - 可变参数数组,所有参数将使用 Base64 + UTF-8 编码后传递给外部程序

返回: 外部程序的标准输出内容

异常:

  • 404 - 找不到 VBMAN.PLI 文件
  • 超时错误 - 执行时间超过 TimeOut 设置

示例:

vb
Dim PLI As New cPLI
Dim result As String

' 无参数调用
result = PLI.Request()

' 单参数调用
result = PLI.Request("hello")

' 多参数调用
result = PLI.Request("param1", "param2", "param3")

' 传递复杂数据
result = PLI.Request("user", "123", "{\"action\":\"login\")}

⚙️ 配置属性

TimeOut

命令执行超时时间(毫秒)。

vb
Public TimeOut As Long

默认值: 30000 (30 秒)

示例:

vb
Dim PLI As New cPLI

' 设置 10 秒超时
PLI.TimeOut = 10 * 1000

' 设置 5 分钟超时(长时间任务)
PLI.TimeOut = 5 * 60 * 1000

' 设置 1 秒超时(快速检查)
PLI.TimeOut = 1000

Dim result As String
result = PLI.Request("long-task")

📋 完整使用示例

调用 Python 脚本

假设 VBMAN.PLI 是一个 Python 打包的可执行程序:

python
# Python 端代码示例 (VBMAN.PLI)
import sys
import base64

def main():
    # 解码参数
    args = sys.argv[1:]
    decoded_args = [base64.b64decode(arg).decode('utf-8') for arg in args]
    
    # 处理数据
    action = decoded_args[0]
    data = decoded_args[1]
    
    if action == "process":
        result = process_data(data)
        print(result)
    elif action == "analyze":
        result = analyze_data(data)
        # 大结果写入文件
        with open("VBMAN.DAT", "w") as f:
            f.write(result)
        print("VBMAN.DAT")  # 返回文件名
    
def process_data(data):
    # 处理逻辑
    return f"Processed: {data}"

def analyze_data(data):
    # 分析逻辑
    return f"Analysis result for: {data}"

if __name__ == "__main__":
    main()
vb
' VB6 调用端
Private Sub CallPython()
    Dim PLI As New cPLI
    Dim result As String
    
    ' 简单调用
    result = PLI.Request("process", "some data")
    Debug.Print result  ' 输出: Processed: some data
    
    ' 长时间任务
    PLI.TimeOut = 60 * 1000  ' 1分钟
    result = PLI.Request("analyze", "large dataset")
    Debug.Print result  ' 可能返回 "VBMAN.DAT"
End Sub

JSON 数据交换

vb
Private Sub ExchangeJSON()
    Dim PLI As New cPLI
    Dim jsonSend As New cJson
    Dim jsonRecv As New cJson
    
    ' 构造请求数据
    jsonSend.AddItem "action", "calculate"
    jsonSend.AddItem "num1", 100
    jsonSend.AddItem "num2", 200
    
    ' 调用外部程序
    Dim result As String
    result = PLI.Request(jsonSend.Encode())
    
    ' 解析返回结果
    jsonRecv.Decode result
    
    If jsonRecv.GetItem("success") = True Then
        Debug.Print "结果: " & jsonRecv.GetItem("result")
    Else
        Debug.Print "错误: " & jsonRecv.GetItem("error")
    End If
End Sub

错误处理

vb
Private Sub SafeCall()
    Dim PLI As New cPLI
    
    On Error GoTo ErrorHandler
    
    Dim result As String
    result = PLI.Request("risky-operation")
    
    Debug.Print "成功: " & result
    Exit Sub
    
ErrorHandler:
    Select Case Err.Number
        Case 404
            MsgBox "找不到 PLI 程序,请检查安装"
        Case Else
            If Err.Description Like "*超时*" Then
                MsgBox "操作超时,请重试"
            Else
                MsgBox "错误: " & Err.Number & " - " & Err.Description
            End If
    End Select
End Sub

文件处理

vb
Private Sub ProcessFile()
    Dim PLI As New cPLI
    
    ' 传递文件路径
    Dim filePath As String
    filePath = "C:\data\input.csv"
    
    Dim result As String
    result = PLI.Request("convert", filePath, "json")
    
    ' 检查是否返回文件
    If result = "VBMAN.DAT" Then
        ' 读取返回的数据文件
        Dim output As String
        output = ToolsStream.LoadFileAsText(App.Path & "\VBMAN.DAT")
        
        ' 处理完成后删除临时文件
        Kill App.Path & "\VBMAN.DAT"
        
        Debug.Print "处理结果: " & output
    Else
        Debug.Print "直接返回: " & result
    End If
End Sub

🔧 相关组件

  • cSTDIO - 标准输入输出执行组件
  • ToolsBase64 - Base64 编码工具
  • ToolsStream - 文件流操作工具

最后更新: 2026-05-17

VB6及其LOGO版权为微软公司所有