Please forgive me re-posting this question since I wasn't clear in my
original post.
-------->
Starting an external process needs to pass it a ProcessStartInfo() object.
ProcessStartInfo has a property "UseShellExecute" that can open/print a
file. For example, here's a piece of code that launches an external process
to open a Microsoft Word Document:
Process p = new Process();
ProcessStartInfo s = new ProcessStartInfo();
s.Verb = "Print";
s.FileName = @"C:\twain.doc";
p.StartInfo = s;
p.StartInfo.UseShellExecute = true;
p.Start();
The code executes without a problem, openning the document with Microsoft
Word. However, I cannot grab any handle of the newly launched process.
Accessing p.Handle will throw an exception of "no associated process", and
in fact, most of the fields of p are either null or with the same exception.
If I try the same code above but with FileName either an EXE file or TXT
files, I can grab the new process handle without any problem. My guess is
the file extension association. Grabbing the handle of a launched process
from an executable file is obviously easy. Grabbing the handle of a "TXT"
file might be easy by just looking at the registry key "shell\open\command".
However, for Microsoft Word document, it uses something called "DDEEXEC"
server, maybe that's the reason Process.Start() does not return any handle?
I've been stuck on this for a while and what I need to do is to launch an
external application on files and be able to monitor the process of the
external application, if it hangs kill it and restart over again. My
alternatives include attempting to implement the mechanism of calling
DDEEXEC in my own code and thus to grab the handle (if not possible at all),
or just monitor all processes on the computer and detect the newly added
process (which is resource-consuming and inaccurate).
Please try the code I included above, any help or hint will be greatly
appreciated!
Regards,
- Fei -