简体   繁体   English

VBA 和 Python 之间的 IPC

[英]IPC between VBA and Python

I am facing the following problem: In our company we use a software whose GUI was programmed with MS Access/VBA.我面临以下问题:在我们公司,我们使用的软件的 GUI 是用 MS Access/VBA 编程的。 Now a part of the business logic should be moved to Python, but the MS Access part should remain.现在应该将部分业务逻辑移至 Python,但仍应保留 MS Access 部分。 The following scenario is now implemented and works: The user types in a string in Access, this string is read out in VBA and by means of a command line the Python script is called and the string is given to the script as a command line argument.现在实现并工作了以下场景:用户在 Access 中键入一个字符串,该字符串在 VBA 中读出,并通过命令行调用 Python 脚本并将该字符串作为命令行参数提供给脚本. Python in turn connects to a vendor's database, uses the passed string as a parameter, and stores the result in our MS SQL database. Python 反过来连接到供应商的数据库,使用传递的字符串作为参数,并将结果存储在我们的 MS SQL 数据库中。 The vendor offers a Python API for its database, hence the necessary intermediate step via Python.供应商为其数据库提供了 Python API,因此通过 Python 进行了必要的中间步骤。 This happens several times a day and starting the script or interpreter takes around 3 seconds each time.这种情况每天发生几次,每次启动脚本或解释器大约需要 3 秒。 This takes too long.这需要太长时间。 The following is not desired: convert Python scripts to a web server or reprogram the GUI using Python.以下是不需要的:将 Python 脚本转换为 Web 服务器或使用 Python 重新编程 GUI。

Sub CallPython()


Dim PythonExe As String, PythonScript As String, PythonArgs As String, PythonOutput As String
Dim PythonCommand As String
Dim objShell As Object

PythonExe = """C:\Program Files\Python37\python.exe"""
PythonScript = """[path_to_our_script]\insert_article.py"""
PythonArgs = "-id 123456"

Set objShell = VBA.CreateObject("Wscript.Shell")

PythonCommand = PythonExe & " " & PythonScript & " " & PythonArgs 
'MsgBox PythonCommand
objShell.Run PythonCommand


End Sub

I have seen the following page regarding IPC techniques, but I don't have much experience in this area, so I can't say much about the complexity.我看过以下关于IPC技术的页面,但我在这方面没有太多经验,所以我不能说太多复杂性。 Does anyone have experience with the above scenario and can share their knowledge on smarter solutions?有没有人有上述场景的经验并且可以分享他们对更智能解决方案的知识?

This subject is really very broad and complex.这个主题确实非常广泛和复杂。

I've done this myself using bidirectional direct communication between R and Access via named pipes, which are handled very similar to files on the Python (or R) side.我自己通过命名管道使用 R 和 Access 之间的双向直接通信完成了这项工作,这些命名管道的处理方式与 Python(或 R)端的文件非常相似。 However, the Access side needs many API declarations to set up the pipes, and for my case, peek at progress so we can asynchronously report progress without locking up the application.但是,Access 端需要许多 API 声明来设置管道,就我而言,查看进度,以便我们可以异步报告进度而不会锁定应用程序。

The basics on named pipes can be found here:命名管道的基础知识可以在这里找到:

https://docs.microsoft.com/en-us/windows/win32/ipc/multithreaded-pipe-server https://docs.microsoft.com/en-us/windows/win32/ipc/multithreaded-pipe-server

and here:和这里:

https://docs.microsoft.com/en-us/windows/win32/ipc/named-pipe-client https://docs.microsoft.com/en-us/windows/win32/ipc/named-pipe-client

Named pipe servers can be single-threaded if there's only one client (your Python application) so you can ignore most of the multithreading stuff.如果只有一个客户端(您的 Python 应用程序),命名管道服务器可以是单线程的,因此您可以忽略大多数多线程内容。

The declarations I needed in VBA are:我在 VBA 中需要的声明是:

Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As LongPtr
    bInheritHandle As Long
End Type
 
Private Type PROCESS_INFORMATION
    hProcess As LongPtr
    hThread As LongPtr
    dwProcessId As Long
    dwThreadId As Long
End Type
 
Private Type STARTUPINFO
    cb As Long
    lpReserved As LongPtr
    lpDesktop As LongPtr
    lpTitle As LongPtr
    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 LongPtr
    hStdInput As LongPtr
    hStdOutput As LongPtr
    hStdError As LongPtr
End Type
 
Private Const STARTF_USESHOWWINDOW  As Long = &H1
Private Const STARTF_USESTDHANDLES  As Long = &H100
Private Const SW_HIDE               As Long = 0&
Private Const ERROR_SUCCESS As Long = 0
Private Const STILL_ACTIVE As Long = 259
Private Const PIPE_TYPE_BYTE As Long = 0
Private Const PIPE_ACCESS_INBOUND = 1
Private Const PIPE_ACCESS_OUTBOUND = 2
Private Const PIPE_ACCESS_DUPLEX As Long = 3
Private Const PIPE_WAIT As Long = 0
Private Const PIPE_NOWAIT As Long = 1
Private Const PIPE_ACCEPT_REMOTE_CLIENTS As Long = 0
Private Const ERROR_PIPE_CONNECTED = 535
Private Const ERROR_PIPE_LISTENING = 536

Private Declare PtrSafe Function CreatePipe Lib "kernel32" (ByRef hReadPipe As LongPtr, ByRef hWritePipe As LongPtr, ByVal lpPipeAttributes As LongPtr, ByVal nSize As Long) As Long
Private Declare PtrSafe Function CreateProcess Lib "kernel32" Alias "CreateProcessW" (ByVal lpApplicationName As LongPtr, ByVal lpCommandLine As LongPtr, ByVal lpProcessAttributes As LongPtr, ByVal lpThreadAttributes As LongPtr, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As LongPtr, ByVal lpCurrentDirectory As LongPtr, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare PtrSafe Function ReadFile Lib "kernel32" (ByVal hFile As LongPtr, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As LongPtr) As Long
Private Declare PtrSafe Function WriteFile Lib "kernel32" (ByVal hFile As LongPtr, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, ByRef nNumberOfBytesWritten As Long, ByVal lpOverlapped As LongPtr) As Long
Private Declare PtrSafe Function FlushFileBuffers Lib "kernel32" (ByVal hFile As LongPtr) As Long
Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As LongPtr) As Long

Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As LongPtr, ByRef lpExitCode As Long) As Long
Private Declare PtrSafe Function TerminateProcess Lib "kernel32" (ByVal hProcess As LongPtr, ByVal uExitCode As Long) As Long

Private Declare PtrSafe Function CreateNamedPipeW Lib "kernel32" (ByVal lpName As LongPtr, ByVal dwOpenMode As Long, ByVal dwPipeMode As Long, ByVal nMaxInstances As Long, ByVal nOutBufferSize As Long, ByVal nInBufferSize As Long, ByVal nDefaultTimeOut As Long, lpSecurityAttributes As Any) As LongPtr
Private Declare PtrSafe Function ConnectNamedPipe Lib "kernel32" (ByVal hNamedPipe As LongPtr, lpOverlapped As Any) As Long
Private Declare PtrSafe Function DisconnectNamedPipe Lib "kernel32" (ByVal hNamedPipe As LongPtr) As Long
Private Declare PtrSafe Function PeekNamedPipe Lib "kernel32" (ByVal hNamedPipe As LongPtr, lpBuffer As Any, ByVal nBufferSize As Long, ByRef lpBytesRead As Long, ByRef lpTotalBytesAvail As Long, ByRef lpBytesLeftThisMessage As Long) As Long

And the essential stuff on the VBA part is: VBA 部分的基本内容是:

  1. Create a named pipe via CreateNamedPipeW with PIPE_ACCESS_OUTBOUND (or two, if you want input and output, one in and one out)通过CreateNamedPipeW使用PIPE_ACCESS_OUTBOUND创建一个命名管道(或者两个,如果你想要输入和输出,一进一出)
  2. Spawn a listener process (your Python process) via CreateProcess so you can get its ID通过CreateProcess生成一个侦听器进程(您的 Python 进程),以便您获取其 ID
  3. When sending a command, check if the process is alive via GetExitCode , connect to the pipe via ConnectNamedPipe , write to the pipe using WriteFile , then FlushFileBuffers , release the file handle via CloseHandle , then disconnect from the pipe using DisconnectNamedPipe发送命令时,通过GetExitCode检查进程是否处于活动状态,通过ConnectNamedPipe连接到管道,使用WriteFile写入管道,然后FlushFileBuffers ,通过CloseHandle释放文件句柄,然后使用DisconnectNamedPipe断开与管道的连接

From the Python process, in a loop, open up the pipe via open , read and process the message, then open up again via open .在 Python 进程中,在一个循环中,通过open管道,读取并处理消息,然后通过open再次打开。 open should halt until the next message is sent. open应该停止,直到发送下一条消息。

And if you want to use return messages for progress, make sure to use PeekNamedPipe to not halt your Access application if Python is slow or has encountered an error.如果您想使用返回消息来获取进度,请确保在 Python 运行缓慢或遇到错误时使用PeekNamedPipe不会停止您的 Access 应用程序。

You'd probably want to wrap all this in a predeclared self-healing VBA class to keep your Python program active while VBA is active and not have to wait for Python to start up/read your program/etc., all nontrivial stuff.您可能希望将所有这些包装在一个预先声明的自我修复 VBA 类中,以在 VBA 处于活动状态时保持您的 Python 程序处于活动状态,而不必等待 Python 启动/读取您的程序等,所有这些都是不平凡的东西。

In the end, it's much simpler to just use local http since you can use pre-existing tools for sending and receiving http requests.最后,只使用本地 http 会简单得多,因为您可以使用预先存在的工具来发送和接收 http 请求。 But direct IPC between VBA and Python/R/any programming languages that can read named pipes (= read files) can be done.但是可以在 VBA 和 Python/R/任何可以读取命名管道(= 读取文件)的编程语言之间进行直接 IPC。

This, unfortunately, is as far as I can take you.不幸的是,这是我所能带给你的。 Really, reconsider "not webserver".真的,重新考虑“不是网络服务器”。 If the processes are on the same machine the webserver can be firewalled, going for a webserver has some more overhead over named pipes but it's a lot easier.如果进程在同一台机器上,则可以对网络服务器进行防火墙保护,使用网络服务器比命名管道有更多开销,但要容易得多。 I kinda wish I went that route.我有点希望我走那条路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM