Installshield停止操作系统进程的代码 --IS6及以上版本适用

简介: 原文:Installshield停止操作系统进程的代码 --IS6及以上版本适用setup.rul的代码   Code//////////////////////////////////////////////////////////////////////////////////     ...
原文: Installshield停止操作系统进程的代码 --IS6及以上版本适用

setup.rul的代码

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code

////////////////////////////////////////////////////////////////////////////////
//                                                                            
//  IIIIIII SSSSSS                                                            
//    II    SS                          InstallShield (R)                     
//    II    SSSSSS      (c) 1996-2000, InstallShield Software Corporation     
//    II        SS      (c) 1990-1996, InstallShield Corporation              
//  IIIIIII SSSSSS                     All Rights Reserved.                   
//                                                                            
//                                                                            
//  This code is generated as a starting setup template.  You should   
//  modify it to provide all necessary steps for your setup.
//                                                                            
//                                                                            
//    File Name:  Setup.rul                                                   
//                                                                            
//  Description:  InstallShield script                                        
//                                                                            
//     Comments:  This template script performs a basic setup. With minor           
//                modifications, this template can be adapted to create       
//                new, customized setups.
//
////////////////////////////////////////////////////////////////////////////////


// Include header files
    
#include 
"ifx.h" //DO NOT REMOVE

////////////////////// string defines ////////////////////////////

//////////////////// installation declarations ///////////////////

// ----- DLL function prototypes -----


    
// your DLL function prototypes

#include 
"ShutDownRunningApp.rul"    

// ---- script function prototypes -----


    
// your script function prototypes

    
// your global variables



//////////////////////////////////////////////////////////////////////////////
//                                                                           
//  FUNCTION:   OnFirstUIBefore                                            
//                                                                           
//  EVENT:      FirstUIBefore event is sent when installation is run for the first
//              time on given machine. In the handler installation usually displays
//              UI allowing end user to specify installation parameters. After this
//              function returns, ComponentTransferData is called to perform file
//              transfer.
//                                                                           
///////////////////////////////////////////////////////////////////////////////
function OnFirstUIBefore()
    NUMBER  nResult,nSetupType;
    STRING  szTitle, szMsg;
begin    

    
if ProcessRunning("notepad") then
        MessageBox(
"Application is running.", INFORMATION);

        ProcessEnd(
"notepad");
        
        Delay(
2);  // Delay to allow process list to refresh
        
        
if ProcessRunning("notepad") then
            MessageBox(
"Application is running.", INFORMATION);
        
else
            MessageBox(
"Application is not running.", INFORMATION);
        endif;
    
else
        MessageBox(
"Application is not running.", INFORMATION);
    endif;


    abort;

    
// TO DO: if you want to enable background, window title, and caption bar title                                                                     
    
// SetTitle( @TITLE_MAIN, 24, WHITE );                                        
    
// SetTitle( @TITLE_CAPTIONBAR, 0, BACKGROUNDCAPTION );                        
    
// Enable( FULLWINDOWMODE );                           
    
// Enable( BACKGROUND );                              
    
// SetColor(BACKGROUND,RGB (0, 128, 128));                       
   
    TARGETDIR 
= PROGRAMFILES ^@COMPANY_NAME ^@PRODUCT_NAME;    

Dlg_Start:
    
// beginning of dialogs label

Dlg_ObjDialogs:
    nResult 
= ShowObjWizardPages(nResult);
    
if (nResult = BACK) goto Dlg_Start;
    
    
// setup default status
    SetStatusWindow(0"");
    Enable(STATUSEX);
    StatusUpdate(ON, 
100);

    
return 0;
end;

//////////////////////////////////////////////////////////////////////////////
//
//  FUNCTION:   OnFirstUIAfter
//
//  EVENT:      FirstUIAfter event is sent after file transfer, when installation 
//              is run for the first time on given machine. In this event handler 
//              installation usually displays UI that will inform end user that
//              installation has been completed successfully.
//
///////////////////////////////////////////////////////////////////////////////
function OnFirstUIAfter()
begin
    Disable(STATUSEX);

    ShowObjWizardPages(NEXT);                                               
end;

///////////////////////////////////////////////////////////////////////////////
//
//  FUNCTION:   OnMaintUIAfter
//
//  EVENT:      MaintUIAfter event is sent after file transfer, when end user runs 
//              installation that has already been installed on the machine. Usually 
//              this happens through Add/Remove Programs applet. 
//              In the handler installation usually displays UI that will inform 
//              end user that maintenance/uninstallation has been completed successfully.
//
///////////////////////////////////////////////////////////////////////////////
function OnMaintUIAfter()
begin
    Disable(STATUSEX);                                               

    ShowObjWizardPages(NEXT);
end;

///////////////////////////////////////////////////////////////////////////////
//
//  FUNCTION:   OnMoving
//
//  EVENT:      Moving event is sent when file transfer is started as a result of
//              ComponentTransferData call, before any file transfer operations 
//              are performed.
//
///////////////////////////////////////////////////////////////////////////////
function OnMoving()
    STRING szAppPath;
begin
    
// Set LOGO Compliance Application Path 
    
// TO DO : if your application .exe is in a subfolder of TARGETDIR then add subfolder 
    szAppPath = TARGETDIR;
    RegDBSetItem(REGDB_APPPATH, szAppPath);
    RegDBSetItem(REGDB_APPPATH_DEFAULT, szAppPath 
^ @PRODUCT_KEY);
end;

// --- include script file section ---


 

ShutDownRunningApp.rul的代码

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code
//////////////////////////////////////////////////////////////////////////////
//
// Description: Windows NT process control functions.
//
//              The process code is adapted from code posted by William F.
//              Snodgrass to www.installsite.org. The original code header
//              is appended below. The array code is adapted from code posted
//              by Rajesh Ramachandran to the installshield.is6.installscript
//              newsgroup.
// 
// Submitted by Richard Iwasa (riwasa@email.com).
//
// Usage example:
//
// if ProcessRunning("notepad") then
//        MessageBox("Application is running.", INFORMATION);
//
//        ProcessEnd("notepad");
//        
//      Delay(2);  // Delay to allow process list to refresh
//        
//        if ProcessRunning("notepad") then
//            MessageBox("Application is running.", INFORMATION);
//        else
//            MessageBox("Application is not running.", INFORMATION);
//        endif;
//    else
//        MessageBox("Application is not running.", INFORMATION);
//    endif;
//
// Original code header appended below:
//
// GetRunningApp();
// ShutDownApp();
// 
// These script created functions will look for any running application
// based on the file name, then display an error message within the Setup.
// You can optionally halt the install or just continue on.
// 
// You can use the ShutDownApp() function for shutting down that process
// or others as well. This is useful for processes that run in the 
// background but have no Windows associated with them. May not work with
// Services.
// 
// This script calls functions in PSAPI.DLL that are not supported on 
// Windows 95 or 98.
// 
// ***Instructions***
// Place these script peices into the Setup.rul file.
// 
// Modify the script to include the applications you would like to get or
// shutdown.
// 
// Submitted by William F. Snodgrass
// Contact info: bsnodgrass@geographix.com
// 
// Created by Theron Welch, 3/3/99
// Minor modifications by Stefan Krueger, 11/03/99
// 
// Copyright (c) 1999-2000 GeoGraphix, Inc. 
//
//////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////
// Function prototypes.
/////////////////////////////////////////////////

prototype POINTER ArrayToPointer(BYREF VARIANT);
prototype NUMBER  ProcessEnd(STRING);
prototype BOOL    ProcessRunning(STRING);

// Kernel functions.

prototype NUMBER Kernel32.OpenProcess(NUMBER, BOOL, NUMBER);
prototype NUMBER Kernel32.TerminateProcess(NUMBER, NUMBER);

// Process information functions.

prototype NUMBER PSAPI.EnumProcesses(POINTER, NUMBER, BYREF NUMBER);
prototype NUMBER PSAPI.EnumProcessModules(NUMBER, BYREF NUMBER, NUMBER,
        BYREF NUMBER);
prototype NUMBER PSAPI.GetModuleFileNameExA(NUMBER, NUMBER, BYREF STRING,
        NUMBER);



/////////////////////////////////////////////////
// Structures.
/////////////////////////////////////////////////

// Structure to mirror the C/C++ SAFEARRAY data structure.

typedef _SAFEARRAY
begin
    SHORT   cDims;
    SHORT   fFeatures;
    LONG    cbElements;
    LONG    cLocks;
    POINTER pvData;
    
// rgsaBound omitted
end;

// Structure to mirror the C/C++ VARIANT data structure.

typedef _VARIANT
begin
    SHORT  vt;
    SHORT  wReserver1;
    SHORT  wReserved2;
    SHORT  wReserved3;
    NUMBER nData;
end;


        
/////////////////////////////////////////////////
// Constants.
/////////////////////////////////////////////////

#define PSAPI_FILE        "psapi.dll"  // Windows NT process DLL
#define PROCESSID_LENGTH  4            // 4 bytes (DWORD) for a process ID

// Process information constants.

#define PROCESS_QUERY_INFORMATION  0x400
#define PROCESS_ALL_ACCESS         0x1f0fff
#define PROCESS_VM_READ            0x10



//////////////////////////////////////////////////////////////////////////////
//
// Function:    ArrayToPointer
//
// Description: Converts an InstallShield array into a C array.
//
//              When an array is created in InstallScript, a VARIANT variable
//              is created which holds an OLEAutomation SAFEARRAY. To pass
//              such an array to a DLL function expecting a C-style array,
//              this function explicitly typecasts the pointer to the array
//              to a _VARIANT pointer so that the _SAFEARRAY pointer can be
//              extracted. The pointer to the actual data is then extracted
//              from the _SAFEARRAY pointer.
//
// Parameters:  structArray - Array variable.
//
// Returns:     POINTER - Pointer to array.
//
//////////////////////////////////////////////////////////////////////////////

function POINTER ArrayToPointer(structArray)
    _SAFEARRAY POINTER pstructArray;    
// _SAFEARRAY array pointer
    _VARIANT   POINTER pstructVariant;  // _VARIANT array pointer
begin
    
// Typecast the pointer to the array to a _VARIANT pointer.
    
    pstructVariant 
= &structArray;
    
    
// Extract the _SAFEARRAY pointer from the _VARIANT.
    
    pstructArray 
= pstructVariant->nData;
    
    
// Return the pointer to the actual data from the _SAFEARRAY.
    
    
return pstructArray->pvData;
end;



//////////////////////////////////////////////////////////////////////////////
//
// Function:    _Process_End
//
// Description: Terminates running processes for the specified application.
//
// Parameters:  szAppName - Name of the application to terminate.
//
// Returns:     >= 0 - Number of processes terminated.
//                -1 - Failure.
//
//////////////////////////////////////////////////////////////////////////////

function NUMBER ProcessEnd(szAppName)
    NUMBER  nvReturn;           
// Number of processes terminated
    NUMBER  nvProcessIDs(512);  // Array of process IDs
    NUMBER  nvBytesReturned;    // Number of bytes returned in process ID array
    NUMBER  nvProcesses;        // Number of processes running
    NUMBER  nvIndex;            // Loop index
    NUMBER  nvProcessHandle;    // Handle to a process
    NUMBER  nvModuleHandle;     // Handle to a process module
    NUMBER  nvBytesRequired;    // Number of bytes required to store values
    POINTER pvProcessIDs;       // Pointer to process ID array
    STRING  svModuleName;       // Module name
    STRING  svFileName;         // Module filename 
begin
    
// The psapi.dll reads the Windows NT performance database. The DLL
    
// is part of the Win32 SDK.
    
    
if UseDLL(WINSYSDIR ^ PSAPI_FILE) < 0 then
        
// Could not load psapi.dll.
        
        MessageBox(
"ERROR: Could not load [" + WINSYSDIR ^ PSAPI_FILE +
                
"].", SEVERE);
        
        
return -1;
    endif;
    
    
// Get the PIDs of all currently running processes.
    
    pvProcessIDs 
= ArrayToPointer(nvProcessIDs);

    EnumProcesses(pvProcessIDs, 
512, nvBytesReturned);

    
// Determine the number of process IDs retrieved. Each process ID
    
// is PROCESSID_LENGTH bytes.
    
    nvProcesses 
= nvBytesReturned / PROCESSID_LENGTH;
    
    
// Get the executable associated with each process, and check if
    
// its filename matches the one passed to the function.
    
    
for nvIndex = 1 to nvProcesses
        
// Get a handle to the process. The OpenProcess function
        
// must have full (all) access to be able to terminate
        
// processes.
        
        nvProcessHandle 
= OpenProcess(PROCESS_QUERY_INFORMATION |
                PROCESS_ALL_ACCESS, 
0, nvProcessIDs(nvIndex));
                
        
if nvProcessHandle != 0 then
            
// Get a handle to the first module in the process, which
            
// should be the executable.
            
            
if EnumProcessModules(nvProcessHandle, nvModuleHandle,        
                    PROCESSID_LENGTH, nvBytesRequired) 
!= 0 then
                
// Get the path of the module.
                
                
if GetModuleFileNameExA(nvProcessHandle, nvModuleHandle,
                        svModuleName, SizeOf(svModuleName)) 
!= 0 then
                    
// Extract the filename (without an extension) from
                    
// the path.
                    
                    ParsePath(svFileName, svModuleName, FILENAME_ONLY);

                    
if StrCompare(svFileName, szAppName) = 0 then
                        
// The process module matches the application 
                        
// name passed to the function.
                        
                        
if TerminateProcess(nvProcessHandle, 0> 0 then
                            nvReturn
++;
                        endif;
                    endif;
                endif;
            endif;
        endif;
    endfor;
            
    
if UnUseDLL(PSAPI_FILE) < 0 then
        MessageBox(
"ERROR: Could not unload [" + WINSYSDIR ^ PSAPI_FILE +
                
"].", SEVERE);
                
        
return -1;
    endif;
        
    
return nvReturn;
end;



//////////////////////////////////////////////////////////////////////////////
//
// Function:    _Process_Running
//
// Description: Determines if the specified process is running in memory.
//
// Parameters:  szAppName - Name of the application to check.
//
// Returns:     TRUE  - The process is running.
//              FALSE - The process is not running.
//
//////////////////////////////////////////////////////////////////////////////

function BOOL ProcessRunning(szAppName)
    BOOL    bvRunning;          
// Process is running
    NUMBER  nvProcessIDs(512);  // Array of process IDs
    NUMBER  nvBytesReturned;    // Number of bytes returned in process ID array
    NUMBER  nvProcesses;        // Number of processes running
    NUMBER  nvIndex;            // Loop index
    NUMBER  nvProcessHandle;    // Handle to a process
    NUMBER  nvModuleHandle;     // Handle to a process module
    NUMBER  nvBytesRequired;    // Number of bytes required to store values
    POINTER pvProcessIDs;       // Pointer to process ID array
    STRING  svModuleName;       // Module name
    STRING  svFileName;         // Module filename 
begin
    
// The psapi.dll reads the Windows NT performance database. The DLL
    
// is part of the Win32 SDK.
    
    
if UseDLL(WINSYSDIR ^ PSAPI_FILE) < 0 then
        
// Could not load psapi.dll.
        
        MessageBox(
"ERROR: Could not load [" + WINSYSDIR ^ PSAPI_FILE +
                
"].", SEVERE);
        
        
return FALSE;
    endif;
    
    
// Get the PIDs of all currently running processes.
    
    pvProcessIDs 
= ArrayToPointer(nvProcessIDs);

    EnumProcesses(pvProcessIDs, 
512, nvBytesReturned);

    
// Determine the number of process IDs retrieved. Each process ID
    
// is PROCESSID_LENGTH bytes.
    
    nvProcesses 
= nvBytesReturned / PROCESSID_LENGTH;
    
    
// Get the executable associated with each process, and check if
    
// its filename matches the one passed to the function.
    
    
for nvIndex = 1 to nvProcesses
        
// Get a handle to the process.
        
        nvProcessHandle 
= OpenProcess(PROCESS_QUERY_INFORMATION |
                PROCESS_VM_READ, 
0, nvProcessIDs(nvIndex));
                
        
if nvProcessHandle != 0 then
            
// Get a handle to the first module in the process, which
            
// should be the executable.
            
            
if EnumProcessModules(nvProcessHandle, nvModuleHandle,        
                    PROCESSID_LENGTH, nvBytesRequired) 
!= 0 then
                
// Get the path of the module.
                
                
if GetModuleFileNameExA(nvProcessHandle, nvModuleHandle,
                        svModuleName, SizeOf(svModuleName)) 
!= 0 then
                    
// Extract the filename (without an extension) from
                    
// the path.
                    
                    ParsePath(svFileName, svModuleName, FILENAME_ONLY);
                    
                    
if StrCompare(svFileName, szAppName) = 0 then
                        
// The process module matches the application 
                        
// name passed to the function.
                        
                        bvRunning 
= TRUE;
                        
                        
goto ProcessRunningEnd;
                    endif;
                endif;
            endif;
        endif;
    endfor;
            
    ProcessRunningEnd:
        
    
if UnUseDLL(PSAPI_FILE) < 0 then
        MessageBox(
"ERROR: Could not unload [" + WINSYSDIR ^ PSAPI_FILE +
                
"].", SEVERE);
                
        
return FALSE;
    endif;
        
    
return bvRunning;
end;

 

 

目录
相关文章
|
3天前
|
算法 调度
深入理解操作系统:进程调度与优先级反转问题
【9月更文挑战第36天】操作系统是计算机科学中的核心概念,它管理着计算机的硬件资源和软件进程。在多任务处理环境中,进程调度是保证系统高效运行的关键机制之一。本文将探讨进程调度的基本概念、调度算法以及它们如何影响系统性能。同时,我们还将讨论优先级反转问题,这是一个在实时系统中常见的问题,它可能导致系统响应时间不可预测。通过分析优先级反转的原因和解决方案,我们可以更好地理解操作系统的设计和优化策略。
|
2天前
|
算法 调度 UED
探索操作系统的心脏:进程调度策略解析
在数字世界的每一次跳动背后,是操作系统中进程调度策略默默支撑着整个计算生态的有序运行。本文将深入剖析进程调度的奥秘,从理论到实践,揭示其对计算性能和系统稳定性的决定性影响。通过深入浅出的讲解和实例分析,我们不仅能理解不同调度策略的工作原理,还能学会如何根据实际应用场景选择或设计合适的调度算法。让我们跟随这篇文章的脚步,一起走进操作系统的核心,解锁进程调度的秘密。
|
1天前
|
iOS开发 MacOS
MacOS环境-手写操作系统-40-进程消息通讯 和 回车键处理
MacOS环境-手写操作系统-40-进程消息通讯 和 回车键处理
9 2
|
2天前
|
算法 调度
探索操作系统的心脏:进程管理与调度
【8月更文挑战第70天】本文深入剖析操作系统中至关重要的进程管理与调度机制,通过生动的比喻和直观的示例代码,带领读者理解进程的生命旅程以及调度算法如何影响系统性能。文章旨在启发读者思考操作系统设计背后的哲学,并鼓励动手实践,从而加深对这一核心主题的理解。
|
2天前
|
算法 Linux 调度
深入理解操作系统:进程管理与调度策略
在数字世界的心脏跳动着的,是那些不眠不休的操作系统。它们如同宇宙中的星系,以精妙的进程管理和调度策略维系着计算秩序的和谐。本文将带您穿梭于操作系统的微观世界,探索进程生命周期的每一个阶段,以及如何通过调度算法确保系统的高效与公平。正如甘地所言:“你必须成为你希望在世界上看到的改变。”在操作系统的世界中,这句话激励我们深入理解并改进这些复杂的系统。
|
1天前
|
存储 算法 调度
MacOS环境-手写操作系统-34-进程优先级
MacOS环境-手写操作系统-34-进程优先级
6 0
|
1天前
|
存储 调度 iOS开发
MacOS环境-手写操作系统-32-进程挂起和恢复
MacOS环境-手写操作系统-32-进程挂起和恢复
6 0
|
1天前
|
算法 调度 iOS开发
MacOS环境-手写操作系统-31-进程自动切换
MacOS环境-手写操作系统-31-进程自动切换
7 0
|
1天前
|
iOS开发 MacOS
MacOS环境-手写操作系统-30-进程之间互相切换
MacOS环境-手写操作系统-30-进程之间互相切换
6 0
|
1天前
|
存储 算法 调度
MacOS环境-手写操作系统-29-进程切换
MacOS环境-手写操作系统-29-进程切换
5 0