Question

This is a possible duplicate. Couldn't find the exact one, I remember going through one such long time back.

The tool needs a capability to detect if any MS Word file is opened anywhere from the computer. So we use a process watcher, like this:

string query = "SELECT TargetInstance FROM __Instance" + Event + "Event WITHIN 0.5 " +
               "WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name = '" + processName + "'";

string scope = @"\\.\root\CIMV2";

//Create a watcher and listen for events
watcher = new ManagementEventWatcher(scope, query);
watcher.EventArrived += eventHandler;
watcher.Start();

Now whatever that is, here is the tricky part: I need to get the word automation instance of the currently opened WINWORD process. So I use Marshal.GetActiveObject to get the running instance. But an opened Word application gets registered in the running object table only if the word app loses focus (as documented by msdn). How do I manually register the word application in ROT if I have the process with me? Some pseudo code below

//process started
private void ProcessStarted(object sender, EventArrivedEventArgs e)
{
     // Marshal.GetActiveObject fails since there is no object yet.
     // Process.GetProcessesByName("WINWORD") got it.
     // now how to register this word process to ROT?
     // so that I have a global instance of word application?
}

Note: I can with some heavy API calls programmatically force focus away from opened word window and then bring it back, but thats a complete mess.

Any normal way of registering?

Was it helpful?

Solution

As far as I know there is no way to register another application's objects in the ROT without its direct cooperation. The table doesn't track processes, it tracks class factories and their associated monikers. In order to register something in the ROT you need its IUnknown pointer, which is not something you're going to get from Word directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top