how to get word com object from processID Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Spitzi
Posts: 309
Joined: 24 Feb 2022, 03:45

how to get word com object from processID

Post by Spitzi » 18 Feb 2023, 16:37

Hi there.

This has bugged me for months now, and I can't seem to figure out. Today, I tried ChatGPT for hints on how to implement this, and it gave me some, but nothing works...

I have this code:

Code: Select all

Gui, aGui:New
Gui, aGui:Add, ListView, w1200 r10, Process Name|Command Line|PID|ParentID|File Name|DocCount
for proc in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where name = 'winword.exe'")
{
	WinGetTitle, Title, % "ahk_pid " proc.ProcessId
	Title := strsplit(Title, " - ").1									; gets rid of the " - Word" part
	; oword := ComObjGet("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process where ProcessId=" . proc.ProcessId).ItemIndex(0).GetOwner()
	; oword := ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where ProcessId=" . proc.ProcessId).ItemIndex(0).GetOwner()

	oword := ComObjActive("Word.Application", proc.ProcessId)

	; oword := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2", "Process", proc.ProcessId)

	docCount := oword.Documents.Count

	LV_Add("", proc.Name, proc.CommandLine,proc.ProcessId, proc.ParentProcessId, Title, docCount)
}
LV_ModifyCol()
Gui, aGui:Show,, WinWord Process List
It shows me all running instances of word. I would like to know how many documents are open in every word instance. So i tried to get the com object corresponding to the processID. But above code does not work. The oword comObj is always the same, all document counts are the same.

any help greatly appreciated

gregster
Posts: 8938
Joined: 30 Sep 2013, 06:48

Re: how to get word com object from processID

Post by gregster » 18 Feb 2023, 16:51

Spitzi wrote:
18 Feb 2023, 16:37
Today, I tried ChatGPT for hints on how to implement this, and it gave me some, but nothing works...
We hear that a lot. That's one reason why we have this rule since a few weeks:
https://www.autohotkey.com/forumrules/#inappropriate_content wrote:Currently, posts that are answering or asking for help that contain AI-generated code (such as ChatGPT) will not be accepted. [...]
Assuming that you already tried to fix the code yourself, I won't close this topic. But I would recommend to rather use Google and these forums than using a chat AI which just invents a lot - with big confidence, though.

Spitzi
Posts: 309
Joined: 24 Feb 2022, 03:45

Re: how to get word com object from processID

Post by Spitzi » 18 Feb 2023, 17:05

Thanks @gregster .

I see your point. The code I posted works, except for the bit where I want to get the com object for a word instance when I know it's process id. and that's where ChatGPT could not help me either. It's just like you said: it just invents stuff combining several programming languages.

It's actually this line:

Code: Select all

oword := ComObjActive("Word.Application", proc.ProcessId)
but the comObject is always the same...

I've been trying to get this to work for months now. It feels like there's just one small bit missing...

teadrinker
Posts: 4314
Joined: 29 Mar 2015, 09:41
Contact:

Re: how to get word com object from processID  Topic is solved

Post by teadrinker » 18 Feb 2023, 19:53

Code: Select all

ControlGet, hwnd, Hwnd,, _WwG1, % "ahk_pid " . proc.ProcessId
oWord := AccObjectFromWindow(hwnd, -16).Application

AccObjectFromWindow(hWnd, idObject = 0)
{
   static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
        , IID_IAccessible := "{618736E0-3C3D-11CF-810C-00AA00389B71}"
        , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
        , h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
        
   VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF
   DllCall("ole32\CLSIDFromString", "Str", idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, "Ptr", &IID)
   if DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject, "Ptr", &IID, "PtrP", pAcc) = 0
      Return ComObjEnwrap(VT_DISPATCH, pAcc, F_OWNVALUE)
}

Spitzi
Posts: 309
Joined: 24 Feb 2022, 03:45

Re: how to get word com object from processID

Post by Spitzi » 20 Mar 2023, 03:18

Thanks @teadrinker! You have helped me out several times in this forum already and I wanted to say that I really appreciate it!!

Your code is awesome!! My problem initially was how to get the right comObj of a word app, if more than one instances of the word app are running. My goal for a long time was to kill the word processes with 0 documents in them, and look what comObj is left over.

I finally solved it using hints from @FanaticGuru and of course @lexikos s ' GetActiveObjects-Function. I posted the code I use now here:

viewtopic.php?f=76&t=108549

Thanks again and greetings from Switzerland (with one bank less to worry about :angel: ;) )


Post Reply

Return to “Ask for Help (v1)”