A few days ago I wrote about making a wrapper for Firefox in order to solve Eclipse and PDT bugs when they open an external browser. The script was very simple, written in Bash shell, but not portable on Windows unless you have Cygwin installed. Here's a small C program to achieve the same result on this OS :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | /* Written by Alexis Bezverkhyy <alexis@grapsus.net> in august 2010 * This is free and unencumbered software released into the public domain. * For more information, please refer to <http://unlicense.org/> */ #include <stdio.h> #include <string.h> #include <windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int r,i; PROCESS_INFORMATION p; STARTUPINFO si = { sizeof(si) }; char url[1000], ffpath[1000], cmdline[1000], *str; FILE *fpid, *fpath; #define PIDFILE "firefox.pid" #define PATHFILE "firefox.path" str = strstr(lpCmdLine, "http"); if(!str) { printf("no URL given\r\n"); return 1; } for(i=0; str[i] && str[i] != '\'' && str[i] != '('; i++) { url[i] = str[i]; } url[i] = 0; if(!strstr(url, "XDEBUG_SESSION_STOP_NO_EXEC")) { ffpath[0] = 0; if(fpath = fopen("firefox.path", "r")) { fscanf(fpath, "%s", ffpath); fclose(fpath); } if(ffpath[0] == 0) { sprintf(ffpath, "%s\\Mozilla Firefox", getenv("ProgramFiles")); } sprintf(cmdline, "\"%s\\firefox.exe\" -no-remote -P eclipse %s", ffpath, url); r = CreateProcess( NULL, cmdline, NULL, NULL, 0, DETACHED_PROCESS | CREATE_BREAKAWAY_FROM_JOB | CREATE_NEW_PROCESS_GROUP, NULL, ffpath, &si, &p); fpid = fopen(PIDFILE, "w+"); if(fprintf(fpid, "%d", p.dwProcessId)) { printf("Pidfile written!\r\n"); } else { printf("Cannot write pidfile\r\n"); return 1; } fclose(fpid); if(r) { printf("Firefox launched! PID=%d cmdline=%s\r\n", p.dwProcessId, cmdline); } else { printf("Cannot start Firefox r=%d cmdline=%s\r\n", r, cmdline); return 1; } } else { DWORD pid; fpid = fopen(PIDFILE, "r"); if(!fpid) { printf("Cannot open pid file\r\n"); return 1; } if(fscanf(fpid, "%d", &pid)) { HANDLE h; if(h = OpenProcess(PROCESS_ALL_ACCESS, 0, pid)) { if(TerminateProcess(h,0)) { printf("Firefox process %d terminated!\r\n", pid); } else { printf("Cannot terminate Firefox process %d\r\n", pid); return 1; } } } else { printf("Invalid pidfile!\r\n"); return 1; } fclose(fpid); if(DeleteFile(PIDFILE)) { printf("Pidfile removed!\r\n"); } } return 0; } |
Firefox is supposed to be installed in its default location, unless you create a file named firefox.path in your Eclipse folder with the path to your Firefox installation.
Ffwrap can be built with mingw or Visual C++. Here's the exe file for lazy people.