2

My application is a sort of "scripting" system. I've made the "scripts" usable as drop targets using info from this answer, however, the default action is to open the script for editing. To run it, you need to pass a /r argument. Is there any way to either: a. use the Run verb, or b. pass the extra argument?

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppJob]
@="MyApp Job"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppJob\shell]
@="open"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppJob\shell\open]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppJob\shell\open\command]
@="\"C:\\Program Files\\MyApp\\MyApp.exe\" \"%1\" \"%2\" \"%3\" \"%4\" \"%5\" \"%6\" \"%7\" \"%8\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppJob\shell\Run]
@="&Run"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppJob\shell\Run\command]
@="\"C:\\Program Files\\MyApp\\MyApp.exe\" /r \"%1\" \"%2\" \"%3\" \"%4\" \"%5\" \"%6\" \"%7\" \"%8\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppJob\shellex]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MyAppJob\shellex\DropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.myJob]
@="MyAppJob"

(Program names have been changed above.)

I've tried adding a key "Verb"="Run" to the DropHandler subkey, but of course, it isn't that simple.

I know I could set Run as the default verb, but I'd prefer not to have to if at all possible.

Community
  • 1
  • 1
RoadieRich
  • 6,330
  • 3
  • 35
  • 52
  • So just to be 100% clear - when you drop a second file on to the script file, you want it to run the script via some executable in the form `myapp.exe /r target_script.myjob dropped_file.xyz`? If so, one option would be to create a second "dummy" executable that handles these situations and passes the appropriate parameters over to `myapp.exe`. – Polynomial Jul 31 '13 at 13:16
  • @Polynomial that's exactly what I'm after, but I can't see how to set it up - at least, without having to create a "dummy executable" for every script. – RoadieRich Jul 31 '13 at 13:21
  • Good point. You may have to write your own shell extension DLL using IDropHandler to make this work properly. This would allow you to override the default verb. – Polynomial Jul 31 '13 at 13:32
  • So, it's back to battling with non-compiling MSDN c++ code... Fun. – RoadieRich Jul 31 '13 at 13:33
  • COM is so lovely. Enjoy ;] – Polynomial Jul 31 '13 at 13:42

1 Answers1

2

Ok, the answer, as suggested in comments, was to create my own ShellExtension dll. I took a look at the microsoft provided example, but it wouldn't compile, and an example on CodeProject didn't make much sense (I think it was written for a different version of VS).

After some googling, however, I discovered this C# codeproject tutorial, which introduced me to SharpShell, which is a C# library for shell extensions. This simplified matters a whole lot:

[ComVisible(true)]
[COMServerAssociation(AssociationType.ClassOfExtension, ".myjob")]
public class MyAppDropHandler : SharpDropHandler
{
    protected override void DragEnter(DragEventArgs dragEventArgs)
    {
        dragEventArgs.Effect = DragDropEffects.Move;
    }

    protected override void Drop(DragEventArgs dragEventArgs)
    {
        new Process
        {
            StartInfo =
            {
                FileName = this.SelectedItemPath,
                Verb = "Run",
                Arguments = String.Join(" ", list.Select(s => String.Format("\"{0}\"", s)))
            }
        }.Start();
    }
}

I then followed the instructions for registering the extension here, and it appears to work great.

RoadieRich
  • 6,330
  • 3
  • 35
  • 52