问题
I am trying to connect to a COM object using python.
More specifically, I am using ASCOM standard drivers to connect to a MaxDome II, and do not know how to reference this driver.
In this link the author uses "Celestron.Telescope" as their parameter for the win32com.client.Dispatch instantiation.
My question is how to find this string. I understand that it is related to the installed drivers, which are installed on the computer, but I don't how to locate the specific string. Is there a way to use win32com.client to list COM objects?
By the way, operating on Windows 7 pro, 64 bit.
Thanks
回答1:
I do not think it is possible to get the list of COM objects directly from python, however you can run the following code (and indeed run it from inside python) in Powershell:
Get-ChildItem HKLM:\Software\Classes -ErrorAction SilentlyContinue | Where-Object {
$_.PSChildName -match '^\w+\.\w+$' -and (Test-Path -Path "$($_.PSPath)\CLSID")
} | Select-Object -ExpandProperty PSChildName
This will output a list of all valid names of COM objects on your system sorted alphabetically, and the syntax is normally MainProgram.SubFunction.
Some of the output on my machine:
I hope this helps!
回答2:
This "Celestron.Telescope" string is called a ProgID (you can google on this term quite easily).
They arere located under the HKEY_LOCAL_MACHINE\SOFTWARE\Classes
registry key for the whole machine and in HKEY_CLASSES_ROOT
for the current running user.
When COM creates an instance of a component, it will use the current user's view of the registry. Most keys are in HKLM but there can be some local overrides in HKCR.
来源:https://stackoverflow.com/questions/47602800/win32com-client-dispatch-class-label