How can I call GetActiveObject on a process in another context?

0

Note: While my sample code is in Powershell, I could easily convert a C# sample into Powershell. I believe that C# developers are more likely to have encountered this issue, even though I am using Powershell to try and do this.

I am trying to debug an issue with server-side Word automation. I know that you're not meant to use it for server applications and we are in the process of changing the application so it doesn't do this but until this happens I need a reliable way to debug issues where it hangs as it does so randomly.

It is not OK for me to just switch .Visible = True on the Word object because it doesn't fail all the time. I just need a way to retrieve it every now and again when it does.

If I start Word and execute this script, it works:

$w = [System.Runtime.InteropServices.Marshal]::GetActiveObject("Word.Application")
$w.Visible = $false;
Start-Sleep -Seconds 5
$w.Visible = $true;

However, if the hanging copy of Word starts as invoked by the COM+ process, the following exception occurs:

Exception calling "GetActiveObject" with "1" argument(s): "Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))"

The Powershell and COM+ process run under the same user, and in Task Manager they both show up as running under the same user.

I have tried running Powershell as all combinations of elevated/non-elevated and x86/x64 modes to no avail.

Therefore I assume the process is in another context or area that I am not able to access from my Powershell session.

So my question is one of:

  1. How can I launch the Powershell process as part of the COM+ process?
  2. How can I switch my context so I can access this object instance?
  3. Is there any other way I can try and retrieve this hidden window and set it to visible?

Unfortunately, most questions that seemed to revolve around this theme end with someone saying "well, don't do that then". Sadly we don't have an option to redevelop this significant part of the system at the moment and I would like to get this documented for those of us who have to deal with these legacy systems from time to time.

c#
.net
powershell
com
asked on Stack Overflow Jul 23, 2015 by Steve Rukuts

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0