InstallShield2015制作安装包----------卸载前结束执行中的进程
方法一:InstallShiel直接调用cmd命令来杀掉进程。
1 //更新或卸载时先关闭应用程序 2 sCmdLine=" /c taskkill /f /im \"Frs.exe\""; 3 nResult=LaunchAppAndWait("cmd",sCmdLine ,LAAW_OPTION_HIDDEN|LAAW_OPTION_NOWAIT);
备注:
1、命令行前 ,记得加上 /c
2、LaunchAppAndWait()函数第一个参数“cmd”,还有另外一种方式。并不是很好用。
1 STRING scCmd; 2 STRING sCmdLine; 3 4 scCmd=WINDIR ^ "system32" ^ "cmd.exe"; 5 nResult=LaunchAppAndWait(scCmd,sCmdLine ,LAAW_OPTION_HIDDEN|LAAW_OPTION_NOWAIT);
方法二:来自网友写的一些代码。
1 prototype POINTER ArrayToPointer(BYREF VARIANT); 2 prototype NUMBER ProcessEnd(STRING); 3 prototype BOOL ProcessRunning(STRING); 4 5 // Kernel functions. 6 7 prototype NUMBER Kernel32.OpenProcess(NUMBER, BOOL, NUMBER); 8 prototype NUMBER Kernel32.TerminateProcess(NUMBER, NUMBER); 9 10 // Process information functions. 11 12 prototype NUMBER PSAPI.EnumProcesses(POINTER, NUMBER, BYREF NUMBER); 13 prototype NUMBER PSAPI.EnumProcessModules(NUMBER, BYREF NUMBER, NUMBER, 14 BYREF NUMBER); 15 prototype NUMBER PSAPI.GetModuleFileNameExA(NUMBER, NUMBER, BYREF STRING, 16 NUMBER); 17 18 19 20 ///////////////////////////////////////////////// 21 // Structures. 22 ///////////////////////////////////////////////// 23 24 // Structure to mirror the C/C++ SAFEARRAY data structure. 25 26 typedef _SAFEARRAY 27 begin 28 SHORT cDims; 29 SHORT fFeatures; 30 LONG cbElements; 31 LONG cLocks; 32 POINTER pvData; 33 // rgsaBound omitted 34 end; 35 36 // Structure to mirror the C/C++ VARIANT data structure. 37 38 typedef _VARIANT 39 begin 40 SHORT vt; 41 SHORT wReserver1; 42 SHORT wReserved2; 43 SHORT wReserved3; 44 NUMBER nData; 45 end; 46 47 48 49 ///////////////////////////////////////////////// 50 // Constants. 51 ///////////////////////////////////////////////// 52 53 #define PSAPI_FILE "psapi.dll" // Windows NT process DLL 54 #define PROCESSID_LENGTH 4 // 4 bytes (DWORD) for a process ID 55 56 // Process information constants. 57 58 #define PROCESS_QUERY_INFORMATION 0x400 59 #define PROCESS_ALL_ACCESS 0x1f0fff 60 #define PROCESS_VM_READ 0x10 61 62 63 64 ////////////////////////////////////////////////////////////////////////////// 65 // 66 // Function: ArrayToPointer 67 // 68 // Description: Converts an InstallShield array into a C array. 69 // 70 // When an array is created in InstallScript, a VARIANT variable 71 // is created which holds an OLEAutomation SAFEARRAY. To pass 72 // such an array to a DLL function expecting a C-style array, 73 // this function explicitly typecasts the pointer to the array 74 // to a _VARIANT pointer so that the _SAFEARRAY pointer can be 75 // extracted. The pointer to the actual data is then extracted 76 // from the _SAFEARRAY pointer. 77 // 78 // Parameters: structArray - Array variable. 79 // 80 // Returns: POINTER - Pointer to array. 81 // 82 ////////////////////////////////////////////////////////////////////////////// 83 84 function POINTER ArrayToPointer(structArray) 85 _SAFEARRAY POINTER pstructArray; // _SAFEARRAY array pointer 86 _VARIANT POINTER pstructVariant; // _VARIANT array pointer 87 begin 88 // Typecast the pointer to the array to a _VARIANT pointer. 89 90 pstructVariant = &structArray; 91 92 // Extract the _SAFEARRAY pointer from the _VARIANT. 93 94 pstructArray = pstructVariant->nData; 95 96 // Return the pointer to the actual data from the _SAFEARRAY. 97 98 return pstructArray->pvData; 99 end; 100 101 102 103 ////////////////////////////////////////////////////////////////////////////// 104 // 105 // Function: _Process_End 106 // 107 // Description: Terminates running processes for the specified application. 108 // 109 // Parameters: szAppName - Name of the application to terminate. 110 // 111 // Returns: >= 0 - Number of processes terminated. 112 // -1 - Failure. 113 // 114 ////////////////////////////////////////////////////////////////////////////// 115 116 function NUMBER ProcessEnd(szAppName) 117 NUMBER nvReturn; // Number of processes terminated 118 NUMBER nvProcessIDs(512); // Array of process IDs 119 NUMBER nvBytesReturned; // Number of bytes returned in process ID array 120 NUMBER nvProcesses; // Number of processes running 121 NUMBER nvIndex; // Loop index 122 NUMBER nvProcessHandle; // Handle to a process 123 NUMBER nvModuleHandle; // Handle to a process module 124 NUMBER nvBytesRequired; // Number of bytes required to store values 125 POINTER pvProcessIDs; // Pointer to process ID array 126 STRING svModuleName; // Module name 127 STRING svFileName; // Module filename 128 begin 129 // The psapi.dll reads the Windows NT performance database. The DLL 130 // is part of the Win32 SDK. 131 132 if UseDLL(WINSYSDIR ^ PSAPI_FILE) < 0 then 133 // Could not load psapi.dll. 134 135 MessageBox("ERROR: Could not load [" + WINSYSDIR ^ PSAPI_FILE + 136 "].", SEVERE); 137 138 return -1; 139 endif; 140 141 // Get the PIDs of all currently running processes. 142 143 pvProcessIDs = ArrayToPointer(nvProcessIDs); 144 145 EnumProcesses(pvProcessIDs, 512, nvBytesReturned); 146 147 // Determine the number of process IDs retrieved. Each process ID 148 // is PROCESSID_LENGTH bytes. 149 150 nvProcesses = nvBytesReturned / PROCESSID_LENGTH; 151 152 // Get the executable associated with each process, and check if 153 // its filename matches the one passed to the function. 154 155 for nvIndex = 1 to nvProcesses 156 // Get a handle to the process. The OpenProcess function 157 // must have full (all) access to be able to terminate 158 // processes. 159 160 nvProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION | 161 PROCESS_ALL_ACCESS, 0, nvProcessIDs(nvIndex)); 162 163 if nvProcessHandle != 0 then 164 // Get a handle to the first module in the process, which 165 // should be the executable. 166 167 if EnumProcessModules(nvProcessHandle, nvModuleHandle, 168 PROCESSID_LENGTH, nvBytesRequired) != 0 then 169 // Get the path of the module. 170 171 if GetModuleFileNameExA(nvProcessHandle, nvModuleHandle, 172 svModuleName, SizeOf(svModuleName)) != 0 then 173 // Extract the filename (without an extension) from 174 // the path. 175 176 ParsePath(svFileName, svModuleName, FILENAME_ONLY); 177 178 if StrCompare(svFileName, szAppName) = 0 then 179 // The process module matches the application 180 // name passed to the function. 181 182 if TerminateProcess(nvProcessHandle, 0) > 0 then 183 nvReturn++; 184 endif; 185 endif; 186 endif; 187 endif; 188 endif; 189 endfor; 190 191 if UnUseDLL(PSAPI_FILE) < 0 then 192 MessageBox("ERROR: Could not unload [" + WINSYSDIR ^ PSAPI_FILE + 193 "].", SEVERE); 194 195 return -1; 196 endif; 197 198 return nvReturn; 199 end; 200 201 202 203 ////////////////////////////////////////////////////////////////////////////// 204 // 205 // Function: _Process_Running 206 // 207 // Description: Determines if the specified process is running in memory. 208 // 209 // Parameters: szAppName - Name of the application to check. 210 // 211 // Returns: TRUE - The process is running. 212 // FALSE - The process is not running. 213 // 214 ////////////////////////////////////////////////////////////////////////////// 215 216 function BOOL ProcessRunning(szAppName) 217 BOOL bvRunning; // Process is running 218 NUMBER nvProcessIDs(512); // Array of process IDs 219 NUMBER nvBytesReturned; // Number of bytes returned in process ID array 220 NUMBER nvProcesses; // Number of processes running 221 NUMBER nvIndex; // Loop index 222 NUMBER nvProcessHandle; // Handle to a process 223 NUMBER nvModuleHandle; // Handle to a process module 224 NUMBER nvBytesRequired; // Number of bytes required to store values 225 POINTER pvProcessIDs; // Pointer to process ID array 226 STRING svModuleName; // Module name 227 STRING svFileName; // Module filename 228 begin 229 // The psapi.dll reads the Windows NT performance database. The DLL 230 // is part of the Win32 SDK. 231 232 if UseDLL(WINSYSDIR ^ PSAPI_FILE) < 0 then 233 // Could not load psapi.dll. 234 235 MessageBox("ERROR: Could not load [" + WINSYSDIR ^ PSAPI_FILE + 236 "].", SEVERE); 237 238 return FALSE; 239 endif; 240 241 // Get the PIDs of all currently running processes. 242 243 pvProcessIDs = ArrayToPointer(nvProcessIDs); 244 245 EnumProcesses(pvProcessIDs, 512, nvBytesReturned); 246 247 // Determine the number of process IDs retrieved. Each process ID 248 // is PROCESSID_LENGTH bytes. 249 250 nvProcesses = nvBytesReturned / PROCESSID_LENGTH; 251 252 // Get the executable associated with each process, and check if 253 // its filename matches the one passed to the function. 254 255 for nvIndex = 1 to nvProcesses 256 // Get a handle to the process. 257 258 nvProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION | 259 PROCESS_VM_READ, 0, nvProcessIDs(nvIndex)); 260 261 if nvProcessHandle != 0 then 262 // Get a handle to the first module in the process, which 263 // should be the executable. 264 265 if EnumProcessModules(nvProcessHandle, nvModuleHandle, 266 PROCESSID_LENGTH, nvBytesRequired) != 0 then 267 // Get the path of the module. 268 269 if GetModuleFileNameExA(nvProcessHandle, nvModuleHandle, 270 svModuleName, SizeOf(svModuleName)) != 0 then 271 // Extract the filename (without an extension) from 272 // the path. 273 274 ParsePath(svFileName, svModuleName, FILENAME_ONLY); 275 276 if StrCompare(svFileName, szAppName) = 0 then 277 // The process module matches the application 278 // name passed to the function. 279 280 bvRunning = TRUE; 281 282 goto ProcessRunningEnd; 283 endif; 284 endif; 285 endif; 286 endif; 287 endfor; 288 289 ProcessRunningEnd: 290 291 if UnUseDLL(PSAPI_FILE) < 0 then 292 MessageBox("ERROR: Could not unload [" + WINSYSDIR ^ PSAPI_FILE + 293 "].", SEVERE); 294 295 return FALSE; 296 endif; 297 298 return bvRunning; 299 end;
调用方式如下:
if ProcessRunning("Frs") then MessageBox("Application \"Frs\" is running,will been killed ", INFORMATION); ProcessEnd("Frs"); endif;