启动相关程序或从另一个程序显示“打开方式”对话框

时间:2014-05-09 14:04:44

标签: java windows rundll32 shell32.dll

在Window Seven下,以下命令显示一个对话框,然后在没有任何其他操作的情况下终止,为什么?

预期效果是启动关联计划Notepad++或至少Notepad

RUNDLL32.EXE SHELL32.DLL,OpenAs_RunDLL D:\doc\toto.txt

enter image description here

3 个答案:

答案 0 :(得分:4)

首先,请注意OpenAs_RunDLLundocumented entry point所以期望它工作的唯一理由是它在HKEY_CLASSES_ROOT注册表中显示为Open With shell动词的实现(在至少某些版本的Windows。)

这只意味着当适当的shell函数调用时,可以期望它可以工作这并不意味着它必然可以在任何任意的上下文中工作。

在我的家庭计算机(Windows Vista)上,当通过“开始”菜单发出命令时,通过OpenAs_RunDLL呼叫rundll32可以正常工作(即,使用所选应用程序打开指定的文件)运行对话框,可以使用键盘快捷键Windows+R打开。

从命令行控制台窗口发出时工作,症状与您描述的相同:显示对话框,但未启动应用程序。这是完全合法的行为,因为您在其未设计的上下文中使用了未记录的入口点。

由于无法保证在未来的Windows版本中根本不存在OpenAs_RunDLL,因此结果很简单:不要使用它。请改用支持的SHOpenWithDialog API函数,或将ShellExecuteShellExecuteExopenas verb一起使用;后者可能特别有用,因为它是easy to do from a scripting language such as VBScript

答案 1 :(得分:2)

解决方案非常简单:cmde.exe start

这是嵌入命令的Java代码:

private void open( File file ) {
   try {
      final String cmd =
         String.format( "cmd.exe /C start %s", file.getAbsolutePath());
      Runtime.getRuntime().exec( cmd );
   }
   catch( final Throwable t ) {
      t.printStackTrace();
   }
}

选择.project时,将显示以下对话框:

enter image description here

当选择底部的单选按钮时,将显示以下对话框:

enter image description here

这正是我想要的。

答案 2 :(得分:2)

基于对类似问题的其他答案以及来自CodeProject: Calling the Open With dialog box from your application, using C#PInvoke.net: SHOpenWithDialog (shell32)的代码,这是适用于我的代码

ShellHelper.OpenAs(mainForm.Handle, "path/to/file")

在Windows XP和Windows Vista及更高版本上。此代码仅使用文档化的API(无rundll32

public class ShellHelper
{
    #region http://www.pinvoke.net/default.aspx/shell32/SHOpenWithDialog.html

    [DllImport("shell32.dll", EntryPoint = "SHOpenWithDialog", CharSet = CharSet.Unicode)]
    private static extern int SHOpenWithDialog(IntPtr hWndParent, ref tagOPENASINFO oOAI);

    // http://msdn.microsoft.com/en-us/library/windows/desktop/bb773363(v=vs.85).aspx 
    private struct tagOPENASINFO
    {
        [MarshalAs(UnmanagedType.LPWStr)]
        public string cszFile;

        [MarshalAs(UnmanagedType.LPWStr)]
        public string cszClass;

        [MarshalAs(UnmanagedType.I4)]
        public tagOPEN_AS_INFO_FLAGS oaifInFlags;
    }

    [Flags]
    private enum tagOPEN_AS_INFO_FLAGS
    {
        OAIF_ALLOW_REGISTRATION = 0x00000001,   // Show "Always" checkbox
        OAIF_REGISTER_EXT = 0x00000002,   // Perform registration when user hits OK
        OAIF_EXEC = 0x00000004,   // Exec file after registering
        OAIF_FORCE_REGISTRATION = 0x00000008,   // Force the checkbox to be registration
        OAIF_HIDE_REGISTRATION = 0x00000020,   // Vista+: Hide the "always use this file" checkbox
        OAIF_URL_PROTOCOL = 0x00000040,   // Vista+: cszFile is actually a URI scheme; show handlers for that scheme
        OAIF_FILE_IS_URI = 0x00000080    // Win8+: The location pointed to by the pcszFile parameter is given as a URI
    }

    private static void DoOpenFileWith(IntPtr hwndParent, string sFilename)
    {
        tagOPENASINFO oOAI = new tagOPENASINFO();
        oOAI.cszFile = sFilename;
        oOAI.cszClass = String.Empty;
        oOAI.oaifInFlags = tagOPEN_AS_INFO_FLAGS.OAIF_ALLOW_REGISTRATION | tagOPEN_AS_INFO_FLAGS.OAIF_EXEC;
        SHOpenWithDialog(hwndParent, ref oOAI);
    }

    #endregion

    #region http://www.codeproject.com/Articles/13103/Calling-the-Open-With-dialog-box-from-your-applica

    [Serializable]
    private struct ShellExecuteInfo
    {
        public int Size;
        public uint Mask;
        public IntPtr hwnd;
        public string Verb;
        public string File;
        public string Parameters;
        public string Directory;
        public uint Show;
        public IntPtr InstApp;
        public IntPtr IDList;
        public string Class;
        public IntPtr hkeyClass;
        public uint HotKey;
        public IntPtr Icon;
        public IntPtr Monitor;
    }

    // Code For OpenWithDialog Box

    [DllImport("shell32.dll", SetLastError = true)]
    extern private static bool ShellExecuteEx(ref ShellExecuteInfo lpExecInfo);

    private const uint SW_NORMAL = 1;

    private static void OpenAsOld(IntPtr hwndParent, string file)
    {
        ShellExecuteInfo sei = new ShellExecuteInfo();
        sei.Size = Marshal.SizeOf(sei);
        sei.Verb = "openas";
        sei.File = file;
        sei.Show = SW_NORMAL;
        sei.hwnd = hwndParent;
        if (!ShellExecuteEx(ref sei))
            throw new System.ComponentModel.Win32Exception();
    }

    #endregion

    public static void OpenAs(IntPtr hWndParent, string file)
    {
        if (System.Environment.OSVersion.Version.Major > 5)
        {
            DoOpenFileWith(hWndParent, file);
        }
        else
        {
            OpenAsOld(hWndParent, file);
        }
    }
}