B4J Question Single instance - multiple files

Discussion in 'B4J Questions' started by lymey, Jul 14, 2015.

  1. lymey

    lymey Active Member Licensed User

    I have an app that needs to open multiple files.
    When I set it as the default app for a file type, and select a single file (Windows) the Args array correctly passes the filename.

    BUT when I select multiple files it starts multiple instances (one for each file selected) rather than passing the two (or more) filenames to the Args array of a single instance.

    I use launch4J to create the .exe file. One of the options is to allow only a single instance. When I do that, only one instance opens, but with only the first filename selected.

    How do I pass multiple selected filenames to a single instance of the application rather than having multiple instances open?
  2. Roycefer

    Roycefer Well-Known Member Licensed User

    Based on my (albeit preliminary) research, such functionality is not built into Windows. It looks like Windows can only open multiple instances of your program, not one instance will multiple filenames as command line args. Here's a Stackoverflow answer saying as much, and there are numerous others: http://stackoverflow.com/questions/...ple-filenames-to-a-context-menu-shell-command .

    I think what you'll have to do is create a separate program called launcher.exe. Set Windows to open your filetype with launcher.exe instead of your current program. Each instance of launcher.exe will append its command line argument (the filename) to a file called temp.txt and will also create a unique file called instance-<datetime.now>.txt. Then each instance will wait 1 second (using a timer) and then check to see how many files are named instance-<datetime.now>.txt. If an instance of launcher.exe detects more than one instance file, that instance of launcher.exe will delete its own instance file and shut down. If there's only instance file, it will delete that file and open your main program with a command line arg called "multi" and then shut itself down. If your program detects the "multi" command line arg, it won't open up files based on command line args, it'll open up temp.txt to get a list of filenames that it should open. Then it will delete temp.txt. This will allow your program to open up multiple files from the Windows Explorer context menu "Open" option. The downside is this whole launching process will take about 1.5 seconds (though you might be able to shave that time down with some experimenting).
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice