简体   繁体   中英

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

Python and the C++ project are using the COM function.

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

  • refer: 'LoadTestSetup'(COM function) and Dispatch(Initinstance function) is the same thread.

The below is Python Code:

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. 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. You need to create a so called COM server in your MFC app and expose certain endpoints within it.

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 .

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. For that, I recommend 'calling' the MFC app like this:

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

This is hundred times more easy than to create a COM server.

After the "LoadTestSetup"Function is executed, The MFC Program enters the ExitInstance function and the Program is terminated. 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. I don't understand why it goes into the "ExitInstance" fucntion:(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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