tags:

views:

281

answers:

2

In my application there is an interface where user can select any file and open in its default application depending on the file association.

I am using FindExecutable and CreateProcessAsUser with Explorer token.

Now the problem is in the case of picture files say .jpg, FindExecutable returns "C:\Program Files\Windows Photo Gallery\PhotoViewer.dll", then CreateProcessAsUser returns "ERROR_BAD_EXE_FORMAT(193) %1 is not a valid Win32 application.". I was passing NULL as the second parameter for CreateProcessAsUser, sending executable path in lpCommandLine(eg: CreateProcessAsUser(hToken, NULL, szCmdline, ...)).

Can anyone help me in solving this?

Regards, Manoj

A: 

Hi, how about using ShellExecuteEx with a properly initialized structure? This should invoke the default action on a given file.

Thorsten Dittmar
My application is running as a webservice. So ShellExecuteEx won't make sure the new application is opened in the foreground always.
Manoj
You might be successful trying "RUNDLL". However, I want to note that running a desktop application from a web service is really a bad idea. A web service is something a client communicates with.
Thorsten Dittmar
There's no desktop, so it won't work.
bmargulies
@bmargulies: I don't know if you're referring to my reply, but as I said I think it's generally a very bad idea to use shell code (like "open a file with its default application") from a web service which is supposed to run silently and accept and answer to client requests.
Thorsten Dittmar
@Thorsten Dittmar: agreed, but my webservice is a local service just like google desktopsearch.
Manoj
Google Desktop search and others have an always running in background .exe to do this kind of bidding. If your webservice needs to open something on the user's desktop, it should send a signal to some sort of process running on it, listening for such a signal, to do it.
Koro
+1  A: 

A Win32 executable has extension .EXE; a DLL is not an executable. CreateProcess cannot create a process with just a .DLL. The missing .EXE is "rundll32.exe".

However, that's not what you are after: you want the Shell behavior. ShellExecuteEx() is usually the most convenient function. AssocQueryString() may be appropriate in this case, with the right flags: ASSOCSTR_EXECUTABLE to get the executable in case it's not yet running, and ASSOCSTR_DDEAPPLICATION etc. in case the application already runs.

MSalters