简体   繁体   English

如何将 Python 与我的 MFC 程序同步。(我使用 Pywin32 Lib)

[英]How to synchronize Python with my MFC program.(I using Pywin32 Lib)

Python and the C++ project are using the COM function. Python 和 C++ 项目正在使用 COM ZC1C425268E683854D14AB5074C17A。

When I was execute Dispatch Function in Python (ex: win32com.client.Dispath), the program enter the Initinstance Function of MFC. When I was execute Dispatch Function in Python (ex: win32com.client.Dispath), the program enter the Initinstance Function of MFC.

Expect motion: Execution of 'LoadTestSetup'function after 'Dispatch'function is completed Problem: while execution 'Dispatch'Func, 'LoadTestSetup'Func is executed预期动作:在“Dispatch”函数完成后执行“LoadTestSetup”函数问题:在执行“Dispatch”Func 时,“LoadTestSetup”Func 被执行

  • refer: 'LoadTestSetup'(COM function) and Dispatch(Initinstance function) is the same thread.参考:'LoadTestSetup'(COM 函数)和 Dispatch(Initinstance 函数)是同一个线程。

The below is Python Code:下面是Python代码:

from win32api
import win32com.client
Tool = win32com.client.Dispatch("Tool Registry Number")
Tool.LoadTestSetup("D:\\HJ\")

The usual syntax to get access via COM is "Object.Name", which "Tool Registry Number" lacks.通过 COM 访问的常用语法是“Object.Name”,“Tool Registry Number”缺少该语法。 It is generally not a good idea to call the InitInstance member of the MFC app directly (if I understand you correctly) and it's not possible from COM anyway.直接调用 MFC 应用程序的InitInstance成员通常不是一个好主意(如果我理解正确的话),而且无论如何都不能从 COM 调用。 You need to create a so called COM server in your MFC app and expose certain endpoints within it.您需要在 MFC 应用程序中创建一个所谓的 COM服务器并在其中公开某些端点。

There are to many unknown variables in your question to be able to answer it more specific than to recommend to learn more about the underlying architecture, eg http://timgolden.me.uk/pywin32-docs/html/com/win32com/HTML/QuickStartClientCom.html or for COM basics https://docs.microsoft.com/en-us/windows/win32/com/guide .您的问题中有很多未知变量能够更具体地回答它而不是推荐更多地了解底层架构,例如http://timgolden.me.uk/pywin32-docs/html/com/win32com/HTML /QuickStartClientCom.html或 COM 基础知识https://docs.microsoft.com/en-us/windows/win32/com/guide

As you want to call InitInstance (if I understand you correctly), I suppose what you really want is to execute the whole MFC application from the start.当你想调用InitInstance (如果我理解正确的话),我想你真正想要的是从一开始就执行整个 MFC 应用程序。 For that, I recommend 'calling' the MFC app like this:为此,我建议像这样“调用” MFC 应用程序:

subprocess.run(os.path.join("D:", "HJ", "Tool Registry Number.exe")

This is hundred times more easy than to create a COM server.这比创建 COM 服务器容易一百倍。

After the "LoadTestSetup"Function is executed, The MFC Program enters the ExitInstance function and the Program is terminated.执行“LoadTestSetup”函数后,MFC程序进入ExitInstance function,程序终止。 Howver, if AfxMessage(~) is added at the bottom of the "InitInstance" function of the MFC Program, "ExitInstance" is not executed and the program is not terminated.但是,如果在MFC程序的“InitInstance”function的底部添加AfxMessage(~),则“ExitInstance”不执行,程序不终止。 I don't understand why it goes into the "ExitInstance" fucntion:(我不明白为什么它会进入“ExitInstance”功能:(

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

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