enthought / comtypes

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

GUID not working when client.GetActiveObject is None #141

Open theobinomy opened 6 years ago

theobinomy commented 6 years ago

I'm running this script below. When I run it the first time, it should create a new instance of a program. When I run it a second time, it should be attaching to an already running instance. Instead it creates a new instance. The only thing that has changes was a windows update. I have tried running the code with Comtypes version 1.1.3, 1.1.4, and the current master.

Code:

def launcher():
    import comtypes.client
    try:
        #trys to attach to a running instance
        mySapObject = comtypes.client.GetActiveObject("CSI.SAP2000.API.SapObject")
        SapModel = mySapObject.SapModel
        return mySapObject, SapModel
    except OSError:
        #if no running instance, creates new instance
        print('os error')
        helper = comtypes.client.CreateObject('SAP2000v19.Helper')
        helper = helper.QueryInterface(comtypes.gen.SAP2000v19.cHelper)
        ProgramPath = 'C:\Program Files\Computers and Structures\SAP2000 19\SAP2000.exe'
        mySapObject = helper.CreateObject(ProgramPath)
        mySapObject.ApplicationStart()
        SapModel = mySapObject.SapModel
        SapModel.File.NewBlank()
        return mySapObject, SapModel
if __name__ == '__main__':
    a, b=launcher()
    print(a,b)

Run 1: runfile('E:/0_Shortcut-/00.1 Dropbox/Dropbox/SoftwareProjects/sap control system/IEEE Objects/lib/_launcher2v2.py', wdir='E:/0_Shortcut-/00.1 Dropbox/Dropbox/SoftwareProjects/sap control system/IEEE Objects/lib') os error <POINTER(cOAPI) ptr=0xabaeaa0 at cf7c248> <POINTER(cSapModel) ptr=0xabaea20 at cf9f0c8>

Run 2: runfile('E:/0_Shortcut-/00.1 Dropbox/Dropbox/SoftwareProjects/sap control system/IEEE Objects/lib/_launcher2v2.py', wdir='E:/0_Shortcut-/00.1 Dropbox/Dropbox/SoftwareProjects/sap control system/IEEE Objects/lib') os error <POINTER(cOAPI) ptr=0xaba0020 at cf8ce48> <POINTER(cSapModel) ptr=0xabafe20 at e29aec8>

Run 3: comtypes.client.GetActiveObject("CSI.SAP2000.API.SapObject") Traceback (most recent call last):

File "", line 1, in comtypes.client.GetActiveObject("CSI.SAP2000.API.SapObject")

File "E:\pyth\lib\site-packages\comtypes\client__init__.py", line 173, in GetActiveObject clsid = comtypes.GUID.from_progid(progid)

File "E:\pyth\lib\site-packages\comtypes\GUID.py", line 78, in from_progid _CLSIDFromProgID(str(progid), byref(inst))

File "_ctypes/callproc.c", line 918, in GetResult

OSError: [WinError -2147221005] Invalid class string

theobinomy commented 6 years ago

Looking into this some more, the SAP2000v19.py object is in /comtypes/gen, there is nothing comtypes/client. On the first time I run the program I can run a lot of commands using the same object. the control object stick around, so I can continue to issue commands while the initial window is open. When I close it, and reopen it is impossible to issue new commands.

theobinomy commented 6 years ago

I was able to solve my issue, using the solution presented here: https://techtalk.gfi.com/32bit-object-64bit-environment/

If there is a way to incorporate this into the code, it would be great.

krish5451 commented 4 years ago

hey, i am having the issue related this python and sap2000 API getting this types of error i have read that tutorial @theobinomy send but i don't understand how to locate the COM. can anyone please help me to solve this problem

Traceback (most recent call last): File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\ptvsd_launcher.py", line 119, in <module> vspd.debug(filename, port_num, debug_id, debug_options, run_as) File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\debugger.py", line 39, in debug run() File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\__main__.py", line 316, in run_file runpy.run_path(target, run_name='__main__') File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 263, in run_path return _run_module_code(code, init_globals, run_name, File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 96, in _run_module_code _run_code(code, mod_globals, init_globals, File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\krish\source\repos\SAP2000\SAP2000\SAP2000.py", line 75, in <module> helper = comtypes.client.CreateObject('SAP2000v1.helper') File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\comtypes\client\__init__.py", line 250, in CreateObject return _manage(obj, clsid, interface=interface) File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\comtypes\client\__init__.py", line 188, in _manage obj = GetBestInterface(obj) File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\comtypes\client\__init__.py", line 112, in GetBestInterface interface = getattr(mod, itf_name) AttributeError: module 'comtypes.gen.SAP2000v1' has no attribute 'cHelper'

krish5451 commented 4 years ago

I'm running this script below. When I run it the first time, it should create a new instance of a program. When I run it a second time, it should be attaching to an already running instance. Instead it creates a new instance. The only thing that has changes was a windows update. I have tried running the code with Comtypes version 1.1.3, 1.1.4, and the current master.

Code:

def launcher():
    import comtypes.client
    try:
        #trys to attach to a running instance
        mySapObject = comtypes.client.GetActiveObject("CSI.SAP2000.API.SapObject")
        SapModel = mySapObject.SapModel
        return mySapObject, SapModel
    except OSError:
        #if no running instance, creates new instance
        print('os error')
        helper = comtypes.client.CreateObject('SAP2000v19.Helper')
        helper = helper.QueryInterface(comtypes.gen.SAP2000v19.cHelper)
        ProgramPath = 'C:\Program Files\Computers and Structures\SAP2000 19\SAP2000.exe'
        mySapObject = helper.CreateObject(ProgramPath)
        mySapObject.ApplicationStart()
        SapModel = mySapObject.SapModel
        SapModel.File.NewBlank()
        return mySapObject, SapModel
if __name__ == '__main__':
    a, b=launcher()
    print(a,b)

Run 1: runfile('E:/0_Shortcut-/00.1 Dropbox/Dropbox/SoftwareProjects/sap control system/IEEE Objects/lib/_launcher2v2.py', wdir='E:/0_Shortcut-/00.1 Dropbox/Dropbox/SoftwareProjects/sap control system/IEEE Objects/lib') os error <POINTER(cOAPI) ptr=0xabaeaa0 at cf7c248> <POINTER(cSapModel) ptr=0xabaea20 at cf9f0c8>

Run 2: runfile('E:/0_Shortcut-/00.1 Dropbox/Dropbox/SoftwareProjects/sap control system/IEEE Objects/lib/_launcher2v2.py', wdir='E:/0_Shortcut-/00.1 Dropbox/Dropbox/SoftwareProjects/sap control system/IEEE Objects/lib') os error <POINTER(cOAPI) ptr=0xaba0020 at cf8ce48> <POINTER(cSapModel) ptr=0xabafe20 at e29aec8>

Run 3: comtypes.client.GetActiveObject("CSI.SAP2000.API.SapObject") Traceback (most recent call last):

File "", line 1, in comtypes.client.GetActiveObject("CSI.SAP2000.API.SapObject")

File "E:\pyth\lib\site-packages\comtypes\clientinit.py", line 173, in GetActiveObject clsid = comtypes.GUID.from_progid(progid)

File "E:\pyth\lib\site-packages\comtypes\GUID.py", line 78, in from_progid _CLSIDFromProgID(str(progid), byref(inst))

File "_ctypes/callproc.c", line 918, in GetResult

OSError: [WinError -2147221005] Invalid class string

try with helper = comtypes.client.CreateObject('SAP2000v1.Helper') helper = helper.QueryInterface(comtypes.gen.SAP2000v1.cHelper)

jesusqc212 commented 4 years ago

Pude resolver mi problema, usando la solución presentada aquí: https://techtalk.gfi.com/32bit-object-64bit-environment/

Si hay una manera de incorporar esto en el código, sería genial.

Hello, how could you solve it?

elviskatarsis commented 6 months ago

Pude resolver mi problema utilizando la solución presentada aquí: https://techtalk.gfi.com/32bit-object-64bit-environment/

Si hay una manera de incorporar esto al código, sería genial.

Pude resolver mi problema utilizando la solución presentada aquí: https://techtalk.gfi.com/32bit-object-64bit-environment/

Si hay una manera de incorporar esto al código, sería genial.

link down