-2

I read MSDN and tried this line of code

ShellExecute(handle, "open", "fully_qualified_path_to_file", NULL, NULL, SW_SHOWNORMAL);

It works fine, but I need some more functionality. I wrote an app, which makes output in file with .bin extension. On my OS .bin files doesn't associated with any specific program. MSDN says that I can associate all .bin files with my app with programming, but this case is unacceptable for me.

How I can add this line of code to open specific file with specific program? p.s. I tried to put my application path in 4th argument as shown in MSDN, smith like that,

C:\\Application.exe" "%1"

but it didn't work.

evg256
  • 1
  • 2
  • 10
    If you already know what program you want to run, then just use CreateProcess. – Raymond Chen Nov 10 '17 at 03:41
  • 1
    You wrote: _p.s. I tried to put my application path in 4th argument, but it didn't work_. Please [edit] your question and show that code as well as error codes, error messages etc. _It doesn't work_ is not a problem description. – Jabberwocky Nov 10 '17 at 08:50
  • The fourth argument specify the parameters. So you have argument three and four reversed. Pass the executable file name as argument three, and the .bin file as argument four. However, this is the wrong way to create a new process. Pointless to ask `ShellExecute` to call `CreateProcess` for you. Call `CreateProcess` directly. – David Heffernan Nov 10 '17 at 10:12
  • @RaymondChen Because the logic of how an application is detected as requiring elevation is not fully documented, using CreateProcess could fail in the future while ShellExecute will always work. – Anders Nov 10 '17 at 13:39
  • Then do what Anders says below. Pass the program as parameter 3 and an appropriate command line in parameter 4. – Raymond Chen Nov 10 '17 at 14:30
  • Thank you all guys for the feedback, both methods work pretty fine. – evg256 Nov 10 '17 at 18:12

1 Answers1

1

If the lpFile parameter is not a .exe then Windows will only be able to execute the file if it has a file type registration in the registry (ShellExecute will read the command line from the registry and replace %1 with the filename).

If you want to force a specific application then lpFile needs to specify the name/path of said application and the file you want it to open has to be part of the parameters in a format supported by the application, usually just the full path to the file (quoted with " if the path contains spaces). ShellExecute will not translate %1 for you in this case.

Anders
  • 97,548
  • 12
  • 110
  • 164