Posted by
Hugo Gratama van Andel on
Jan 20, 2006; 12:50pm
URL: http://imagej.273.s1.nabble.com/Launching-ImageJ-from-external-program-tp3703994p3703996.html
Hello Jef,
I have looked in the code of JavaLauncher and it is easy to add some
lines to look in a specific directory like c:\Program Files\ImageJ.
But ... that is nog what you (I?) want. For instance I have ImageJ
installed in "C:\Program Files (x86)\ImageJ".
ImageJ could be installed everywhere, and you would like to let it work
in al ocassions. With an extra 'few more lines' it is also possible to
to find the ImageJ.cfg in the directory where the ImageJ.exe file (that
is executed) is located and to use that as the new working directory.
I have attached the source code. Would you like to receive a compiled
version?
The executable now works fine, but I notice a strange delay when opening
an image with extension associations (or in the "open with"-dialog). It
takes up to 12 seconds to launch ImageJ with the image. The cause of the
delay is not the executable but it is the javaw.exe (as can be seen in
the task manager, the executable ends directly, while the javaw.exe is
inactive for a time and then suddenly launches imageJ with the image)
When running the executable without an image it does launch directly.
Is ImageJ not used to be called with a specified image??
I hope that someone has an idea to solve the delay.
with regards,
Hugo Gratama van Andel
Jeff Brandenburg wrote:
>
> This question ("how do I get ImageJ to launch as a helper application
> in Windows?") is starting to come up more frequently on the list.
> Wayne has pointed out what needs to be done:
>
>> The ImageJ Windows launcher (ImageJ.exe) expects its configuration
>> file (ImageJ.cfg) to be in the current directory. Someone familiar
>> with C++ could modify the launcher to look for the configuration file
>> in the c:\Program Files\ImageJ directory if it can't find it in the
>> current directory. The C++ source code for the launcher is available at
>>
>>
http://www.rolemaker.dk/nonRoleMaker/javalauncher/
>> marner_java_launcher.htm
>>
>> This fix would also allow extension associations (i.e., assigning
>> .tif to Image) to work.
>
>
> The source code for the Marner Java launcher is pretty short, and seems
> simple and straightforward enough.
>
> I wish someone with a Windows box, Developer Studio, and some Windows
> expertise would take a swing at this. I've got a system here that
> launches ImageJ to do remote volume browsing, and it works like a charm
> on OS X, but it's embarrassing to tell Windows users that I can't make
> it work with their machines. :-/
> --
> -jeffB (Jeff Brandenburg, Duke Center for In-Vivo Microscopy)
>
--
Hugo Gratama van Andel - MSc
Academic Medical Centre - University of Amsterdam
Dept. Medical Physics L0-152
Meibergdreef 9
1105 AZ Amsterdam
The Netherlands
Phone : +31 20 566 5206
FAX : +31 20 691 7233
e-mail :
[hidden email]
/*
This is an application that can be used to launch Java
applications by just clicking on a exe file.
You can rename launcher.exe to anything you want.
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <shellapi.h>
void deletenewlines(char* buffer)
{
while(*buffer != '\0')
{
if (*buffer == '\n')
{
*buffer = '\0';
return;
}
++buffer;
}
}
void getExecutable(char *result, char* lpCmdLine)
{ //returns the name of the executable appended with .cfg
const char* constCommandLine = ::GetCommandLine();
char* commandLine = new char[strlen(constCommandLine)+1];
strcpy(commandLine,constCommandLine);
// Remove lpCmdLine from back
int lastpos = strlen(commandLine)-strlen(lpCmdLine);
commandLine[lastpos] = '\0';
lastpos--;
while(commandLine[lastpos]=='\"' || commandLine[lastpos]==' ')
{
commandLine[lastpos]='\0';
lastpos--;
}
int firstpos = lastpos;
while(commandLine[firstpos] > 0 && commandLine[firstpos]!='\\'
&& commandLine[firstpos]!=':' && commandLine[firstpos]!='/')
firstpos--;
strcpy(&result[0], &commandLine[firstpos+1]);
int resultlength = strlen(result);
bool containsdot = false;
for (int i=0; i<resultlength; i++)
if (result[i]=='.')
containsdot = true;
if (containsdot)
while(result[resultlength]!='.')
resultlength--;
strcpy(&result[resultlength],".cfg");
resultlength = strlen(result);
result[resultlength+1] = '\0';
}
void getExecutablePath(char *result, char* lpCmdLine)
{ // returns the path to the executable
const char* constCommandLine = ::GetCommandLine();
char* commandLine = new char[strlen(constCommandLine)+1];
strcpy(commandLine,constCommandLine);
// Remove lpCmdLine from back
int lastpos = strlen(commandLine)-strlen(lpCmdLine);
commandLine[lastpos] = '\0';
lastpos--;
while(commandLine[lastpos]!='\\')
{
commandLine[lastpos]='\0';
lastpos--;
}
int firstpos = 0;
while(commandLine[firstpos]=='\"' || commandLine[firstpos]==' ')
firstpos++;
strcpy(&result[0], &commandLine[firstpos]);
int resultlength = strlen(result);
resultlength = strlen(result);
result[resultlength+1] = '\0';
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
FILE* file = NULL;
// Look for config file with same name as executable but with cfg suffix.
char executable[1000];
getExecutable(executable,lpCmdLine);
char executablePath[1000];
getExecutablePath(executablePath,lpCmdLine);
char executablePathWithName[1000];
file = fopen(executable,"rt"); // Check for the executable.cfg -file in the current working directory
char cfgFileName[1000];
if (file)
strcpy(cfgFileName,executable);
if (!file)
{ // Check for the cfg executable.file in the directory of the application
sprintf(executablePathWithName,"%s%s",executablePath,executable);
file = fopen(executablePathWithName,"rt");
strcpy(cfgFileName, executablePath);
_chdir(executablePath);
}
// Open default config file
if (!file)
{
file = fopen("ImageJ.cfg","rt");
strcpy(cfgFileName, "ImageJ.cfg");
}
if (file==NULL)
{
char temp[1000];
sprintf(temp,"Could not find file %s or ImageJ.cfg. Please\n"
"reinstall the application.", executable);
MessageBox(NULL, temp,"Error loading configuration file", MB_OK);
return 1;
}
char buffer[10000];
// Read new directory from config file. We will change to this dir
// before starting execution.
fgets(buffer,10000,file);
deletenewlines(buffer);
int result = _chdir(buffer);
if (result!=0)
{
char temp[1000];
sprintf(temp, "Could not find the directory given in %s\n" \
"Please reinstall the application.", cfgFileName);
MessageBox(NULL, temp, "Error changing directory",MB_OK);
return 2;
}
// Read the name of the executable. Is usually java.exe or javaw.exe
char exe[1000];
fgets(exe,1000,file);
deletenewlines(exe);
// Read any extra parameters you want to give to the program.
// such as -jar parameters.
char parameter[1000];
fgets(parameter,1000,file);
deletenewlines(parameter);
// Add parameters given to the exe
strcat(parameter," ");
strcat(parameter, lpCmdLine);
char thecwd[1000];
_getcwd(thecwd,1000);
if (ShellExecute(NULL,NULL,exe,parameter,thecwd,SW_SHOWNORMAL)<=(HINSTANCE)32)
{
MessageBox(NULL,
"Could not start the application",
"Please reinstall the application",MB_OK);
return 2;
}
return 0;
}