enthought / comtypes

A pure Python, lightweight COM client and server framework, based on the ctypes Python FFI package.
Other
290 stars 97 forks source link

Multiple instances of Autocad #520

Open sindzicat opened 7 months ago

sindzicat commented 7 months ago

Hello!

I know, that if Autocad is already running, we can use comtypes.client.GetActiveObject:

import comtypes.client as cc
app = cc.GetActiveObject("AutoCAD.Application")

But what to do, if multiple instances of Autocad is running?

I found the code to solve this issue, but it's in C#:

[DllImport("ole32.dll")]
static extern int CreateBindCtx(
    uint reserved,
    out IBindCtx ppbc);

[DllImport("ole32.dll")]
public static extern void GetRunningObjectTable(
    int reserved,
    out IRunningObjectTable prot);

// Requires Using System.Runtime.InteropServices.ComTypes
// Get all running instance by querying ROT
private List<object> GetRunningInstances(string[] progIds)
{
    List<string> clsIds = new List<string>();

    // get the app clsid
    foreach (string progId in progIds)
    {
        Type type = Type.GetTypeFromProgID(progId);

        if(type != null)
            clsIds.Add(type.GUID.ToString().ToUpper());
    }

    // get Running Object Table ...
    IRunningObjectTable Rot = null;
    GetRunningObjectTable(0, out Rot);
    if (Rot == null)
        return null;

    // get enumerator for ROT entries
    IEnumMoniker monikerEnumerator = null;
    Rot.EnumRunning(out monikerEnumerator);

    if (monikerEnumerator == null)
        return null;

    monikerEnumerator.Reset();

    List<object> instances = new List<object>();

    IntPtr pNumFetched = new IntPtr();
    IMoniker[] monikers = new IMoniker[1];

    // go through all entries and identifies app instances
    while (monikerEnumerator.Next(1, monikers, pNumFetched) == 0)
    {
        IBindCtx bindCtx;
        CreateBindCtx(0, out bindCtx);
        if (bindCtx == null)
            continue;

        string displayName;
        monikers[0].GetDisplayName(bindCtx, null, out displayName);

        foreach (string clsId in clsIds)
        {
            if (displayName.ToUpper().IndexOf(clsId) > 0)
            {
                object ComObject;
                Rot.GetObject(monikers[0], out ComObject);

                if (ComObject == null)
                    continue;

                instances.Add(ComObject);
                break;
            }
        }
    }

    return instances;
}

void TestROT()
{
    // Look for acad 2009 & 2010 & 2014
    string[] progIds =
    {
        "AutoCAD.Application.17.2",
        "AutoCAD.Application.18",
        "AutoCAD.Application.19.1"
    };

    List<object> instances = GetRunningInstances(progIds);

    foreach (object acadObj in instances)
    {
        try
        {
            // do some stuff ...  
        }
        catch
        {

        }
    }
}

I know it's possible to translate the C# code above to Python, but I don't know C# to do so. Anybody know it to translate, please?

sindzicat commented 7 months ago

I now found the following: https://stackoverflow.com/questions/63562678/how-to-reference-the-com-objects-of-all-the-running-excel-application-instances The same task but for Excel Applications, and on Python.

junkmd commented 7 months ago

Upon examining the C# code, moniker seems to be relevant. CoGetObject functions may be relevant.

Here are the relevant code snippets:

However, as I am not well-versed in C#, I hope that those who proficient in both Python and C# provide further insights.

sindzicat commented 7 months ago

Unfortunately, I have no ideas what displayname should be passed to CoGetObject and where I can get it.

I'm trying now to rewrite the following code from here from pywin32 to comtypes:

from pythoncom import (
  CreateBindCtx         as create_bind_context_com_interface,
  IID_IDispatch         as dispatch_com_interface_iid,
  GetRunningObjectTable as get_running_object_table_com_interface,
)
from win32com.client import (
  Dispatch as dispatch,
)

def get_excel_instances():
  '''
  Returns a list of the running Microsoft Excel application
  instances as component object model (COM) objects.
  '''
  running_object_table_com_interface = get_running_object_table_com_interface()
  bind_context_com_interface = create_bind_context_com_interface()
  excel_application_class_clsid = '{00024500-0000-0000-C000-000000000046}'
  excel_application_clsid = '{000208D5-0000-0000-C000-000000000046}'
  excel_instance_com_objects = []
  for moniker_com_interface in running_object_table_com_interface:
    display_name = moniker_com_interface.GetDisplayName(bind_context_com_interface, None)
    if excel_application_class_clsid not in display_name:
      continue
    unknown_com_interface = running_object_table_com_interface.GetObject(moniker_com_interface)
    dispatch_com_interface = unknown_com_interface.QueryInterface(dispatch_com_interface_iid)
    dispatch_clsid = str(object=dispatch_com_interface.GetTypeInfo().GetTypeAttr().iid)
    if dispatch_clsid != excel_application_clsid:
      continue
    excel_instance_com_object = dispatch(dispatch=dispatch_com_interface)
    excel_instance_com_objects.append(excel_instance_com_object)
  return excel_instance_com_objects

excel_instances = get_excel_instances()
input()

My code:

import ctypes

get_running_object_table = ctypes.oledll.ole32.GetRunningObjectTable

Now I need to pass 2 parameters to get_running_object_table function: number 0 and IRunningObjectTable (docs), but I have no ideas, what I need to do to create IRunningObjectTable...

junkmd commented 7 months ago

The IRunningObjectTable interface is published in the Microsoft win32metadata repository. https://github.com/microsoft/win32metadata/blob/d1702f3d2b32b31fe2ffbe684ae6cff96e3a39b0/generation/WinSDK/RecompiledIdlHeaders/um/ObjIdl.Idl#L394-L440

By referring to this, it should be possible to define a subclass that inherits from IUnknown.

Moreover, when searching GitHub with IRunningObjectTable, it was possible to find numerous codebases that seem to be useful as references.

junkmd commented 6 months ago

Is there an update on this issue?

sindzicat commented 6 months ago

Sorry, your last answer was still difficult for me. A few days ago I found this article how to use ctypes to work with Win32 API. After this excellent article, I have a much better understanding of your answer.

Now I decided to learn a basics of C programming language, because I still have a poor C knowledge to use ctypes and comtypes efficiently. I steel not sure how to model IRunningObjectTable with ctypes/comtypes, because this object is an interface, not usual C structure. This object is looked like C structure of functions, but I'm not sure about this. Also, as an example, I don't know yet, what two stars means (**ppenumMoniker).

As for now using this answer from StackOverflow is the best way to solve this problem, so we can close this issue. Alternatively, you could create a function in comtypes that finds all instances. Anyway, it's interesting topic how to solve this using ctypes/comtypes and I hope I'll be able to do this in the future.

junkmd commented 6 months ago

Your attempt is wonderful.

I'm not familiar with C language either, and these are unknown territories for me. However, if comtypes provides the statically defined IRunningObjectTable interface and implements GetRunningObjectTable in a Python-friendly manner, I think that would be fantastic.

If you could write a description of the usecase and tests, I can make resources to review your PR.

For sharing difficulties and technical consultations, I think we should keep this issue open.

junkmd commented 1 month ago

Recently, I had the opportunity to read the Japanese translations of "Inside COM" by Dale Rogerson, "Essential COM" by Don Box, and "Inside OLE" by Kraig Brockschmidt, which are available in public libraries in Japan. These books provided overviews and usage instructions for IRunningObject, IMoniker, and IBindCtx.

I also realized that the wrapper module generated by GetModule('msvidctl.dll') contains definitions for these interfaces.

By combining these interfaces with helper functions from ctypes.oledll.ole32, it is possible to implement a function that retrieves running objects.

from ctypes import byref, oledll, POINTER
from typing import Iterator, Sequence, Type, TypeVar

from comtypes import GUID
from comtypes.automation import IDispatch
from comtypes.client import GetModule

GetModule("msvidctl.dll")
from comtypes.gen.MSVidCtlLib import IBindCtx, IMoniker, IRunningObjectTable

def GetRunningObjectTable() -> IRunningObjectTable:
    """Returns a pointer to the IRunningObjectTable interface on the local
    running object table (ROT).

    https://learn.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-getrunningobjecttable
    """
    rot = POINTER(IRunningObjectTable)()
    # The first parameter is reserved and must be 0.
    oledll.ole32.GetRunningObjectTable(0, byref(rot))
    return rot  # type: ignore

def CreateBindCtx() -> IBindCtx:
    """Returns a pointer to an implementation of IBindCtx (a bind context object).
    This object stores information about a particular moniker-binding operation.

    https://learn.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-createbindctx
    """
    bctx = POINTER(IBindCtx)()
    # The first parameter is reserved and must be 0.
    oledll.ole32.CreateBindCtx(0, byref(bctx))
    return bctx  # type: ignore

def _iterate_moniker(rot: IRunningObjectTable) -> Iterator[IMoniker]:
    enum_running = rot.EnumRunning()
    while True:
        item, fetched = enum_running.RemoteNext(1)
        if fetched:
            yield item
        else:
            break

_T_IDispatch = TypeVar("_T_IDispatch", bound=IDispatch)

def get_running_objects(
    reg_clsid: GUID, interface: Type[_T_IDispatch]
) -> Sequence[_T_IDispatch]:
    objs = []
    rot = GetRunningObjectTable()
    bctx = CreateBindCtx()
    for moniker in _iterate_moniker(rot):
        name = moniker.GetDisplayName(bctx, None)
        if str(reg_clsid) not in name:
            continue
        disp: IDispatch = rot.GetObject(moniker).QueryInterface(IDispatch)
        if interface._iid_ != disp.GetTypeInfo(0).GetTypeAttr().guid:
            continue
        objs.append(disp.QueryInterface(interface))
    return objs

With a test like the one below, we can verify that the running Excel application can be retrieved.

import unittest

from comtypes.client import GetModule

GetModule(("{00020813-0000-0000-C000-000000000046}",))  # Excel libUUID
from comtypes.gen import Excel

class Test(unittest.TestCase):
    def test(self):
        coclass = Excel.Application
        interface = Excel._Application
        objs = get_running_objects(coclass._reg_clsid_, interface)
        self.assertTrue(bool(objs))
        self.assertTrue(all(isinstance(o, interface) for o in objs))
junkmd commented 1 month ago

@sindzicat
If you're still interested in this issue and the target you want to operate on is ZWCad as it was in #516, could you try running the equivalent of the test below?

from pathlib import Path
import unittest

from comtypes.client import GetModule

zwcad_tlib_path: Path = Path('C:/Program Files/Common Files/ZWSoft Shared/zwcad21.tlb')
GetModule(str(zwcad_tlib_path))  # Generate the module or ensure its existence.
from comtypes.gen import ZWCAD  # importing statically to analyze static typing

class Test(unittest.TestCase):
    def test(self):
        coclass = ZWCAD.ZcadApplication
        interface = ZWCAD.IZcadApplication
        objs = get_running_objects(coclass._reg_clsid_, interface)
        self.assertTrue(bool(objs))
        self.assertTrue(all(isinstance(o, interface) for o in objs))