Java accessing Window's “Open with…” list

扶醉桌前 提交于 2019-12-24 02:25:13

问题


I would like to get the list of "Open with..." contextual menu in Windows XP/Vista/7 from Java.

From the Windows Registry, I've managed to find a way to get the command to launch applications. But I did not find out how to get these applications names and icons as shown in the Explorer Open with list contextual menu.

Also, the way to manage these informations seems to change from one version of the OS to another.

Is there any library I could use with Java for this ?

Thanks.


回答1:


You don't need the registry for this. You need to use GetProcAddress on OpenAs_RunDLLA in Shell32.dll. I can't find any documentation for it, but I have Delphi code that defines it as

SHOpenWithProc = procedure(HWND: THandle; HInstance; THandle; 
                           CmdLine: PChar; CmdShow: Integer);

The HWND, HInstance, and CmdShow should be fairly familiar. PChar in Delphi corresponds (ANSI version - see below) to a pointer to a null terminated (C-style) string, and in the Unicode version to a null terminated WSTR. procedure in Delphi corresponds to C's void someproc();. The CmdLine should point to a fully-qualified filename, so Windows knows what to offer in the "Open With" dialog.

I'm not sure how you would use GetProcAddress (and the preceeding LoadLibrary call) in Java, but this may get you started.

Note that the function being loaded is the ANSI version; for WideChar (Unicode), you'd want to load the OpenAs_RunDLLW version instead, and adjust the CmdLine parameter accordingly (I think - I haven't tried the code on the wide version).

NOTE: This may help too. It's a MSDN article on using OpenAs_RunDLL via the API's ShellExecute function.




回答2:


As alluded to in the comment above, Java code to invoke the Open With dialog in Windows would be (exception handling omitted):

CommandLine cmd = new CommandLine("rundll32.exe");
cmd.addArgument("shell32.dll,OpenAs_RunDLL");
cmd.addArgument(fullPathToMyFile.toString());
Process process = CommandLauncherFactory.createVMLauncher().exec(cmd, null);
This uses the Apache Commons Exec library; there are other ways of invoking a process from Java too.

来源:https://stackoverflow.com/questions/5197880/java-accessing-windows-open-with-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!