Click to See Complete Forum and Search --> : Process handles for applications embedded in windows


Amerikon
January 17th, 2008, 03:21 PM
I'm writing a simple C# console app (in Windows Vista) that gets a file and has to open it in the default editor for that file type. I'm using some code like this:

proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = (path + fileName);

if (proc.Start()) {
//Do Stuff
} else {
//No Process was returned so I can't do anything!
return failure;
}

I need the process object so (at a minimum) I can know when the application has been closed. It works fine for any installed application (including MS Office), however if I'm given a jpg or a wmv file that opens up in one of MS's embedded applictions (Photo Gallery and Media Player respectively) the proc.Start() call returns false and the Process object is unassigned even though calling proc.Start() does actually call up the file in it's default application. I don't normally do a lot of Windows specific programming so I'm wondering if there's something simple that I'm missing.

Thanks.

HanneSThEGreaT
January 18th, 2008, 07:09 AM
As I understand your question.....

You would need to set the UseShellExecute property of the ProcessInfo object to true, in order to launch the wmv or jpg in their respected default applications.

Amerikon
January 18th, 2008, 09:41 AM
From what I can tell, the UseShellExecute defaults to true. The issue is not getting windows to execute the file, but the fact that for applications that are integrated into the OS the Start() call on the process doesn't bind the process information to the object. Let me try and flesh out my example some more.

Imagine this function:

private void execProc(String path, String fileName) {
proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = (path + fileName);

nativeEditorProc.Start();

Console.WriteLine("Process: " + proc.ToString() +
"\nName: " + proc.ProcessName +
"\nPID: " + proc.Id);
}

If I make this call: execProc(".\\", "foo.pdf")
This is the output:
Process: System.Diagnostic.Process (AcroRd32)
Name: AcroRd32
PID: 38788

If I make this call: execPro(".\\", "foo.jpg")
This is the output:
System.InvalidOperationException: No process is associated with this object.
at ... <stack trace>

Note that before the exception occurs, foo.jpg is opened in Windows Photo Gallery, so the proc.Start() works, it just doesn't bind the process to the proc object.

Amerikon
January 21st, 2008, 03:27 PM
*bump*

Anybody?

Arjay
January 21st, 2008, 04:49 PM
Use the Spy++ tool to look at the toplevel window of 'Windows Photo Gallery' and then look at the process id and see what it points to.

Using Spy++:
1) Use the find window grabber tool
2) Grab the top level window and release the mouse
3) Click on the process tab in the Windows Properties dialog
4) Click on the process id to open the Process properties dialog
5) Check out the Module name on the General tab