为什么我可以为某些扩展添加上下文菜单,但没有其他人使用Reg和Java

时间:2014-02-10 17:37:12

标签: java contextmenu regedit

我正在尝试修改Windows注册表,以便某些音频文件扩展名的上下文菜单,以便在所述文件上启动我的应用程序。该应用程序是用Java编写的,但是使用Winrun4j包装在exe包装器中。

我一直在使用此示例How do I make context menus for my software?来尝试执行此操作。令人沮丧的是,演示为.txt文件添加了一个上下文菜单,并且可以工作,如果我破解代码并将其添加到我的应用程序中.txt文件也可以。但是,如果我只是更改代码以处理音​​频文件,如.mp3没有报告错误,但它不起作用,在.mp3s的上下文菜单上没有显示任何选项

代码检查特定组是否与另一个应用程序相关联,是否影响输入到注册表中的值,但音频文件和文本文件都与另一个应用程序相关联(Audio Vlc,txt,UltraEdit)所以这似乎不是问题。我确实尝试忽略这些应用程序并直接添加到Software / .mp3 / classes而不是Software / Vlc / classes中,但它没有任何区别。

这是我跑步时的输出:

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

以下是相关代码

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;
    }
}

0 个答案:

没有答案