0

I'm trying to modify Windows registry so that the context menu for certain audio file extensions so that is an option to start my application on the said files. The application is written Java but wrapped in an exe wrapper using Winrun4j.

Ive been using this example How do I make context menus for my software? to try and do this. The frustrating thing is the demo adds a context menu for .txt files and that works, and if I hack the code and add it to my application for .txt file it also works. But if I then just change the code to work on audio files such as .mp3 no errors are reported but it doesn't work, no option is shown on the context menu for .mp3s

The code check to see if a particular group is associated with another application and if it is that affects the the value entered into registry, but both audio files and text files are associated with another application (Audio Vlc, txt, UltraEdit) so that doesn't seem to be the problem. I did try ignoring these applications and adding direct into Software/.mp3/classes instead of Software/Vlc/classes but it made no difference.

Here is the output when I run:

C:\Program Files (x86)\Jthink\SongKong>songkong.exe

10/02/2014 17.13.10:com.jthink.songkong.Windows.AddContextMenu:buildLaunchCommand:SEVERE: Architecture is:x86
10/02/2014 17.13.10:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
 -g -m "%1"
10/02/2014 17.13.10:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.mp3 /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp3
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp3 /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.mp3)
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.mp3\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.mp3\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp3\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp3\Shell\SongKong32\Command /ve
 /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: MP3:AddContextMenu:true
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
 -g -m "%1"
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.ogg /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.ogg
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.ogg /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.ogg)
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.ogg\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.ogg\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.ogg\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.ogg\Shell\SongKong32\Command /ve
 /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: OGG:AddContextMenu:true
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
 -g -m "%1"
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.flac /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.flac
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.flac /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.flac)
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.flac\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.flac\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.flac\Shell\SongKong32 /ve /t REG
_SZ /d Fix with SongKong /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.flac\Shell\SongKong32\Command /v
e /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: FLAC:AddContextMenu:true
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
 -g -m "%1"
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.m4a /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4a
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4a /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.m4a)
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.m4a\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.m4a\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4a\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4a\Shell\SongKong32\Command /ve
 /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: M4A:AddContextMenu:true
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
 -g -m "%1"
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.mp4 /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp4
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp4 /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.mp4)
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.mp4\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.mp4\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp4\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp4\Shell\SongKong32\Command /ve
 /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: MP4:AddContextMenu:true
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
 -g -m "%1"
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.m4p /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4p
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4p /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.m4p)
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.m4p\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.m4p\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4p\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4p\Shell\SongKong32\Command /ve
 /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: M4P:AddContextMenu:true
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
 -g -m "%1"
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.wma /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.wma
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.wma /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.wma)
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.wma\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.wma\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.wma\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.wma\Shell\SongKong32\Command /ve
 /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: WMA:AddContextMenu:true
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
 -g -m "%1"
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.m4b /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\(value not set)
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\.m4b\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\.m4b\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\.m4b\Shell\SongKong32 /ve /t REG_SZ
/d Fix with SongKong /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\.m4b\Shell\SongKong32\Command /ve /t
 REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: M4B:AddContextMenu:true
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
 -g -m "%1"
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.txt /ve
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\UltraEdit.txt
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\UltraEdit.txt /ve
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\Text Document
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32 /ve /
t REG_SZ /d Fix with SongKong /f
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32\Comma
nd /ve /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.13:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: txt:AddContextMenu:true

Here is the relevent code

public class AddContextMenu
{
    public static void addContextMenu() {

        String launchCmd = buildLaunchCommand();
        String INTERNAL_NAME = "SongKong";
        final String osArch = System.getProperty("os.arch").toLowerCase();
        if(!osArch.contains("64"))
        {
            INTERNAL_NAME = "SongKong";
        }

        String DISPLAY_NAME = "Fix with SongKong";
        for(AudioFormat audioFormat: AudioFormat.values())
        {
            if(audioFormat.getFileSuffix().length()>0)
            {
                boolean success = ContextMenuManager.createContextMenu(
                        INTERNAL_NAME, DISPLAY_NAME, SongKong.class,
                        "." + audioFormat.getFileSuffix(), launchCmd);
                MainWindow.logger.severe(audioFormat+":AddContextMenu:"+success);
            }
        }

        //For testing
        boolean success = ContextMenuManager.createContextMenu(
                INTERNAL_NAME, DISPLAY_NAME, SongKong.class,
                ".txt", launchCmd);
        MainWindow.logger.severe("txt"+":AddContextMenu:"+success);
    }

    static protected String buildLaunchCommand()
    {
        File userDir  = new File(System.getProperty("user.dir"));
        final String osArch = System.getProperty("os.arch").toLowerCase();
        MainWindow.logger.severe("Architecture is:"+osArch);
        if(osArch.contains("64"))
        {
            return "\""+ userDir.getAbsolutePath() + "\\SongKong64.exe\" -g -m \"%1\"";
        }
        else
        {
            return "\""+ userDir.getAbsolutePath() + "\\SongKong.exe\" -g -m \"%1\"";
        }
    }
}



package com.jthink.songkong.Windows;

import com.jthink.songkong.ui.MainWindow;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


final public class ContextMenuManager
{

    static public boolean createContextMenu(String internalName,
                                            String displayName, Class mainClass, String targetExtention, String launchCmd)
    {

        MainWindow.logger.severe("launchCmd:"+launchCmd);

        String targetKeyNode = inferTargetKeyNode(targetExtention, internalName);
        MainWindow.logger.severe("targetKeyNode"+targetKeyNode);
        if (keyNodeExists(targetKeyNode))
        {
            // TODO: Handle existence of Registry key-node with the same name
            MainWindow.logger.severe("targetKeyNodeExists"+targetKeyNode);
            return false;
        }

        String[][] registryCmds = buildRegistryAddCommands(
                targetKeyNode, displayName, launchCmd);

        boolean operationSuccessful = true;
        for (int i = 0; i < registryCmds.length; i++)
        {
            operationSuccessful &= executeCommand(registryCmds[i]);
        }

        if (!operationSuccessful)
        {
            removeContextMenu(internalName, targetExtention);   // "Roll back"
        }
        return operationSuccessful;
    }

    static public boolean removeContextMenu(String internalName,
                                            String targetExtention)
    {
        /* 
         * Implementation note: 
         * This way we might be deleting another app's context-menu entry if 
         * it happens to have the same name as ours. It may be a good idea to
         * add an custom value to our key-node with a name amd or data highly
         * unlikely to have been inserted by another app as well. This way we
         * should minimize the risk of accidentally deleting another app's 
         * entry to next to zero.
         */

        String targetKeyNode = inferTargetKeyNode(targetExtention, internalName);
        if (!keyNodeExists(targetKeyNode))
        {
            return true;
        }

        String[][] registryCmds = buildRegistryDeleteCommands(targetKeyNode);

        boolean operationSuccessful = true;
        for (int i = 0; i < registryCmds.length; i++)
        {
            operationSuccessful &= executeCommand(registryCmds[i]);
        }

        if (!operationSuccessful)
        {
            // TODO: Do some "rolling forward" (?)
            // (Rather difficult, so we better ignore it and hope we didn't 
            //  break anything :D)
        }
        return operationSuccessful;
    }




    static protected String[][] buildRegistryAddCommands(String targetKeyNode,
                                                         String displayName, String launchCmd)
    {
        String[][] cmds = new String[2][];

        // REG ADD <target_key_node>\Shell\<internal_name> /ve /t REG_SZ /d <display_name> /f
        cmds[0] = new String[]{
                "REG", "ADD", targetKeyNode, "/ve", "/t", "REG_SZ", "/d",
                displayName, "/f"};

        // REG ADD <target_key_node>\Shell\<internal_name>\Command /ve /t REG_SZ /d <properly_escaped_launch_command> /f
        String escapedLaunchCmd = launchCmd.replaceAll("\"", "\\\\\"");
        cmds[1] = new String[]{
                "REG", "ADD", targetKeyNode + "\\Command", "/ve", "/t", "REG_SZ",
                "/d", escapedLaunchCmd, "/f"};

        return cmds;
    }

    static protected String[][] buildRegistryDeleteCommands(
            String targetKeyNode)
    {
        String[][] cmds = new String[1][];

        /* REG ADD <target_key_node>\Shell\<internal_name> /f */
        cmds[0] = new String[]{"REG", "DELETE", targetKeyNode, "/f"};

        return cmds;
    }

    static protected boolean executeCommand(String[] cmd)
    {
        return executeCommand(cmd, null);
    }

    static protected boolean executeCommand(String[] cmd, List<String> output)
    {
        StringBuffer sb = new StringBuffer();
        for(String c:cmd)
        {
            sb.append(c+" ");
        }
        MainWindow.logger.severe("Cmd:"+sb);

        try
        {
            ProcessBuilder pb = new ProcessBuilder(cmd);
            Process proc = pb.start();

            /*BufferedReader brErr = new BufferedReader(new InputStreamReader(
                    proc.getErrorStream()));
            String lineErr;
            while ((lineErr = brErr.readLine()) != null) {
                System.err.println(lineErr);
            }*/

            if (output != null)
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        proc.getInputStream()));
                String line;
                while ((line = br.readLine()) != null)
                {
                    output.add(line);
                }
            }

            int exitStatus = proc.waitFor();
            return (exitStatus == 0);
        }
        catch (Exception x)
        {
            x.printStackTrace();
        }
        return false;
    }

    static protected String inferTargetKeyNode(String targetExtension,
                                               String internalName)
    {
        try
        {
            //Search
            //i.e.
            Pattern pattern = Pattern.compile(
                    "\\s*\\(.+\\)\\s*REG_SZ\\s*(.*)");
            String[] cmd = {"REG", "QUERY", "", "/ve"};

            String classKeyNode = "HKCR\\" + targetExtension;
            boolean findAssociatedClass = true;
            while (findAssociatedClass)
            {
                cmd[2] = classKeyNode;
                List<String> output = new ArrayList<>();
                executeCommand(cmd, output);

                findAssociatedClass = false;

                //Look to see whther we should associate with suffix directly or via another application (i.e.mp4 or Vlc files)
                for (String line : output)
                {
                    Matcher matcher = pattern.matcher(line);
                    if (matcher.find()
                            && keyNodeExists("HKCR\\" + matcher.group(1)))
                    {
                        classKeyNode = "HKCR\\" + matcher.group(1);
                        findAssociatedClass = true;
                        break;
                    }
                }
            }

            //Place in User Space rather than root space (which need admin permissions
            classKeyNode = classKeyNode
                    .replaceFirst("HKCR", "HKCU\\\\Software\\\\Classes");
            return classKeyNode + "\\Shell\\" + internalName;
        }
        catch (Exception x)
        {
            x.printStackTrace();
        }
        return "";
    }

    static protected boolean keyNodeExists(String keyNode)
    {
        if ((keyNode == null) || (keyNode.trim().length() == 0))
        {
            throw new RuntimeException(
                    "Invalid or non-existent Registry-key node !");
        }
        try
        {
            String[] cmd = {"REG", "QUERY", keyNode};
            return executeCommand(cmd);
        }
        catch (Exception x)
        {
            x.printStackTrace();
        }
        return false;
    }
}
Community
  • 1
  • 1
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • I don't have "proof" of that, but I suspect that infering what the target-key should be under HKCR and then adding the key to HKCU is a problem (I don't know if it is the only one). If you want to add a context-menu for the user, why not infer the key under HKCU in the first place ? Your added key is probably never looked up (aka taken into account by the OS), because noone told the OS to go look it where you put it. – gkalpak Feb 11 '14 at 15:04
  • BTW, it is possible that there is not entry under HKCU regarding your target file-extension (meaning the specified file-type should not be handled in a special way for the current user, so the OS looks under HKCR for default handling). If that is the case, you probably need to add it yourself (which should be rather straightforward, but haven't tried it myself - or I don't remember it). – gkalpak Feb 11 '14 at 15:05
  • @ExpertSystem heh, well I was just using your code example but I can try. I also tried just writing to HKCR because I thought I would have admin permissions when running wrapped as an exe but didn't seem to work, Ill recheck – Paul Taylor Feb 11 '14 at 15:47
  • @ExpertSystem i ran my application with Administrator priviledges and keys were added to HKCR but that still didnt work – Paul Taylor Feb 11 '14 at 16:00
  • LOL - You are right: It is my code you are using. It's almost a year back and I don't remember what/how I was doing it, but one thing I know for sure: I trust my code more than I trust myself. So if the code says it should/could be done that way, then it must be true :) (Anyway, I'll try it myself and get back to you.) – gkalpak Feb 11 '14 at 16:39
  • Hm...I tried it and it works just fine for me (I tried on two different PCs). It could be something specific to your setup, but I am afraid I can't help you much further :( Have you tried using my demo code and see if it works (just add your extensions in the TARGET_EXTENSIONS array). – gkalpak Feb 11 '14 at 16:52
  • @ExpertSystem Modified your demo code to do .txt, .mp3 and .ogg and .txt worked but the others did not. So it seems like your solution is a partial solution that only works sometimes, but then again it is possible that my attempts have messed up my registry somehow and our preventing your solution working properly. – Paul Taylor Feb 11 '14 at 18:27
  • @ExpertSystem No, reverted registry still failing remoed application they were going under (Vlc) now going under Winamp and still failing. I would be interested in your results if you try for mp3s/mp4s rather than txt - does it work for you for music files ? – Paul Taylor Feb 11 '14 at 22:03
  • Yes. By _"Hm...I tried it and it works just fine for me"_ I meant I tried it for `.mp3` – gkalpak Feb 11 '14 at 22:19
  • Maybe it is just mine ( or at least just some). The good news is I also want to add context menu for folders which is more important to me I thought would be harder, but it is actually much simpler - and works – Paul Taylor Feb 12 '14 at 10:59

0 Answers0