Portable installer: Error out if calibre.exe is running

This commit is contained in:
Kovid Goyal 2012-09-16 14:49:57 +05:30
parent e5e5da661e
commit 95b52a2059
2 changed files with 40 additions and 2 deletions

View File

@ -385,7 +385,7 @@ class Win32Freeze(Command, WixMixIn):
zf = self.a(self.j('dist', 'calibre-portable-%s.zip.lz'%VERSION)) zf = self.a(self.j('dist', 'calibre-portable-%s.zip.lz'%VERSION))
usz = os.path.getsize(zf) usz = os.path.getsize(zf)
def cc(src, obj): def cc(src, obj):
cflags = '/c /EHsc /MT /W4 /Ox /nologo /D_UNICODE /DUNICODE'.split() cflags = '/c /EHsc /MT /W4 /Ox /nologo /D_UNICODE /DUNICODE /DPSAPI_VERSION=1'.split()
cflags.append(r'/I%s\include'%LZMA) cflags.append(r'/I%s\include'%LZMA)
cflags.append('/DUNCOMPRESSED_SIZE=%d'%usz) cflags.append('/DUNCOMPRESSED_SIZE=%d'%usz)
if self.newer(obj, [src]): if self.newer(obj, [src]):
@ -414,7 +414,7 @@ class Win32Freeze(Command, WixMixIn):
desc='Calibre Portable Installer', extra_data=zf, desc='Calibre Portable Installer', extra_data=zf,
product_description='Calibre Portable Installer'), product_description='Calibre Portable Installer'),
xobj, obj, 'User32.lib', 'Shell32.lib', 'easylzma_s.lib', xobj, obj, 'User32.lib', 'Shell32.lib', 'easylzma_s.lib',
'Ole32.lib', 'Shlwapi.lib', 'Kernel32.lib'] 'Ole32.lib', 'Shlwapi.lib', 'Kernel32.lib', 'Psapi.lib']
self.run_builder(cmd) self.run_builder(cmd)
os.remove(zf) os.remove(zf)

View File

@ -10,6 +10,7 @@
#include <Shlobj.h> #include <Shlobj.h>
#include <Shlwapi.h> #include <Shlwapi.h>
#include <Shellapi.h> #include <Shellapi.h>
#include <Psapi.h>
#include <wchar.h> #include <wchar.h>
#include <stdio.h> #include <stdio.h>
#include <io.h> #include <io.h>
@ -433,6 +434,39 @@ static BOOL move_program() {
} }
// }}} // }}}
static BOOL ensure_not_running(LPCWSTR dest) {
DWORD processes[4096], needed, num;
unsigned int i;
WCHAR name[4*MAX_PATH] = L"<unknown>";
HANDLE h;
DWORD len;
LPWSTR fname = NULL;
if ( !EnumProcesses( processes, sizeof(processes), &needed ) ) {
return true;
}
num = needed / sizeof(DWORD);
for (i = 0; i < num; i++) {
if (processes[i] == 0) continue;
h = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, processes[i] );
if (h != NULL) {
len = GetProcessImageFileNameW(h, name, 4*MAX_PATH);
CloseHandle(h);
if (len != 0) {
name[len] = 0;
fname = PathFindFileName(name);
if (wcscmp(fname, L"calibre.exe") == 0) {
show_error(L"Calibre appears to be running on your computer. Please quit it before trying to install Calibre Portable.");
return false;
}
}
}
}
return true;
}
static void launch_calibre() { static void launch_calibre() {
STARTUPINFO si; STARTUPINFO si;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
@ -517,6 +551,10 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
goto end; goto end;
} }
if (existing) {
if (!ensure_not_running(fdest)) goto end;
}
// Make a temp dir to unpack into // Make a temp dir to unpack into
if (!SetCurrentDirectoryW(fdest)) { show_detailed_error(L"Failed to change to unzip directory: ", fdest, 0); goto end; } if (!SetCurrentDirectoryW(fdest)) { show_detailed_error(L"Failed to change to unzip directory: ", fdest, 0); goto end; }