How to differentially process files opened from windows explorer and from cmd?

We have 2 ways to open a file with an application:

  • Double-click on a file in the explorer. Windows explorer will search for its extension in the registry, find the default application command line (CLI) format string, substitute the "%1" argument with the file’s path, and finally run the CLI.
  • Directly call the application in the CLI, passing the file’s path as an argument.

If things are straightforward as above, we should mimic the double-click operation from the CLI perfectly, i.e., have the application unable to tell if it is called from CLI or double-click in the explorer: they receive very same arguments from the OS. This is what I longly believe.

However, MATLAB broke my dream. I found that MATLAB can differentially treat its .m script files opened from explorer and CLI. When there’s already a MATLAB instance running, double-click on a .m file in the explorer will open that file in the existing instance. However, when I opened the file by the CLI found from the registry, MATLAB started a new instance!

How could MATLAB achieve this? Is it monitoring the explorer? Or it hides its true CLI somewhere else?

I found the CLI from the registry at HKEY_CLASSES_ROOTMATLAB.mlx.9.11.0ShellOpencommand. The default value is

"C:Program FilesMATLABR2021bbinwin64matlab.exe" -r  "eval('[WinReg_path,WinReg_name,WinReg_ext]=fileparts(''%1'');cd(WinReg_path);clear WinReg_path WinReg_name WinReg_ext');"

I tried to substitute the %1 placeholder with the file path, but a new instance still started. So confusing!

Source: Windows Questions