杉宫竹苑工作室

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1832|回复: 0

Installshield 停止操作系统进程的代码

[复制链接]
发表于 2017-3-1 15:17:06 | 显示全部楼层 |阅读模式

正式会员享受无限制浏览网站功能和高速网盘下载,赶快加入本站吧!

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Setup.rul的代码
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //                                                                           
  3. //  IIIIIII SSSSSS                                                            
  4. //    II    SS                          InstallShield (R)                     
  5. //    II    SSSSSS      (c) 1996-2000, InstallShield Software Corporation     
  6. //    II        SS      (c) 1990-1996, InstallShield Corporation              
  7. //  IIIIIII SSSSSS                     All Rights Reserved.                  
  8. //                                                                           
  9. //                                                                           
  10. //  This code is generated as a starting setup template.  You should   
  11. //  modify it to provide all necessary steps for your setup.
  12. //                                                                           
  13. //                                                                           
  14. //    File Name:  Setup.rul                                                   
  15. //                                                                           
  16. //  Description:  InstallShield script                                       
  17. //                                                                           
  18. //     Comments:  This template script performs a basic setup. With minor           
  19. //                modifications, this template can be adapted to create      
  20. //                new, customized setups.
  21. //
  22. ////////////////////////////////////////////////////////////////////////////////


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

  26. ////////////////////// string defines ////////////////////////////

  27. //////////////////// installation declarations ///////////////////

  28. // ----- DLL function prototypes -----


  29.     // your DLL function prototypes

  30. #include "ShutDownRunningApp.rul"   

  31. // ---- script function prototypes -----


  32.     // your script function prototypes

  33.     // your global variables



  34. //////////////////////////////////////////////////////////////////////////////
  35. //                                                                           
  36. //  FUNCTION:   OnFirstUIBefore                                            
  37. //                                                                           
  38. //  EVENT:      FirstUIBefore event is sent when installation is run for the first
  39. //              time on given machine. In the handler installation usually displays
  40. //              UI allowing end user to specify installation parameters. After this
  41. //              function returns, ComponentTransferData is called to perform file
  42. //              transfer.
  43. //                                                                           
  44. ///////////////////////////////////////////////////////////////////////////////
  45. function OnFirstUIBefore()
  46.     NUMBER  nResult,nSetupType;
  47.     STRING  szTitle, szMsg;
  48. begin   

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

  51.         ProcessEnd("notepad");
  52.         
  53.         Delay(2);  // Delay to allow process list to refresh
  54.         
  55.         if ProcessRunning("notepad") then
  56.             MessageBox("Application is running.", INFORMATION);
  57.         else
  58.             MessageBox("Application is not running.", INFORMATION);
  59.         endif;
  60.     else
  61.         MessageBox("Application is not running.", INFORMATION);
  62.     endif;


  63.     abort;

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

  72. Dlg_Start:
  73.     // beginning of dialogs label

  74. Dlg_ObjDialogs:
  75.     nResult = ShowObjWizardPages(nResult);
  76.     if (nResult = BACK) goto Dlg_Start;
  77.    
  78.     // setup default status
  79.     SetStatusWindow(0, "");
  80.     Enable(STATUSEX);
  81.     StatusUpdate(ON, 100);

  82.     return 0;
  83. end;

  84. //////////////////////////////////////////////////////////////////////////////
  85. //
  86. //  FUNCTION:   OnFirstUIAfter
  87. //
  88. //  EVENT:      FirstUIAfter event is sent after file transfer, when installation
  89. //              is run for the first time on given machine. In this event handler
  90. //              installation usually displays UI that will inform end user that
  91. //              installation has been completed successfully.
  92. //
  93. ///////////////////////////////////////////////////////////////////////////////
  94. function OnFirstUIAfter()
  95. begin
  96.     Disable(STATUSEX);

  97.     ShowObjWizardPages(NEXT);                                               
  98. end;

  99. ///////////////////////////////////////////////////////////////////////////////
  100. //
  101. //  FUNCTION:   OnMaintUIAfter
  102. //
  103. //  EVENT:      MaintUIAfter event is sent after file transfer, when end user runs
  104. //              installation that has already been installed on the machine. Usually
  105. //              this happens through Add/Remove Programs applet.
  106. //              In the handler installation usually displays UI that will inform
  107. //              end user that maintenance/uninstallation has been completed successfully.
  108. //
  109. ///////////////////////////////////////////////////////////////////////////////
  110. function OnMaintUIAfter()
  111. begin
  112.     Disable(STATUSEX);                                               

  113.     ShowObjWizardPages(NEXT);
  114. end;

  115. ///////////////////////////////////////////////////////////////////////////////
  116. //
  117. //  FUNCTION:   OnMoving
  118. //
  119. //  EVENT:      Moving event is sent when file transfer is started as a result of
  120. //              ComponentTransferData call, before any file transfer operations
  121. //              are performed.
  122. //
  123. ///////////////////////////////////////////////////////////////////////////////
  124. function OnMoving()
  125.     STRING szAppPath;
  126. begin
  127.     // Set LOGO Compliance Application Path
  128.     // TO DO : if your application .exe is in a subfolder of TARGETDIR then add subfolder
  129.     szAppPath = TARGETDIR;
  130.     RegDBSetItem(REGDB_APPPATH, szAppPath);
  131.     RegDBSetItem(REGDB_APPPATH_DEFAULT, szAppPath ^ @PRODUCT_KEY);
  132. end;

  133. // --- include script file section ---
复制代码

ShutDownRunningApp.rul的代码
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Description: Windows NT process control functions.
  4. //
  5. //              The process code is adapted from code posted by William F.
  6. //              Snodgrass to www.installsite.org. The original code header
  7. //              is appended below. The array code is adapted from code posted
  8. //              by Rajesh Ramachandran to the installshield.is6.installscript
  9. //              newsgroup.
  10. //
  11. // Submitted by Richard Iwasa (riwasa@email.com).
  12. //
  13. // Usage example:
  14. //
  15. // if ProcessRunning("notepad") then
  16. //        MessageBox("Application is running.", INFORMATION);
  17. //
  18. //        ProcessEnd("notepad");
  19. //        
  20. //      Delay(2);  // Delay to allow process list to refresh
  21. //        
  22. //        if ProcessRunning("notepad") then
  23. //            MessageBox("Application is running.", INFORMATION);
  24. //        else
  25. //            MessageBox("Application is not running.", INFORMATION);
  26. //        endif;
  27. //    else
  28. //        MessageBox("Application is not running.", INFORMATION);
  29. //    endif;
  30. //
  31. // Original code header appended below:
  32. //
  33. // GetRunningApp();
  34. // ShutDownApp();
  35. //
  36. // These script created functions will look for any running application
  37. // based on the file name, then display an error message within the Setup.
  38. // You can optionally halt the install or just continue on.
  39. //
  40. // You can use the ShutDownApp() function for shutting down that process
  41. // or others as well. This is useful for processes that run in the
  42. // background but have no Windows associated with them. May not work with
  43. // Services.
  44. //
  45. // This script calls functions in PSAPI.DLL that are not supported on
  46. // Windows 95 or 98.
  47. //
  48. // ***Instructions***
  49. // Place these script peices into the Setup.rul file.
  50. //
  51. // Modify the script to include the applications you would like to get or
  52. // shutdown.
  53. //
  54. // Submitted by William F. Snodgrass
  55. // Contact info: bsnodgrass@geographix.com
  56. //
  57. // Created by Theron Welch, 3/3/99
  58. // Minor modifications by Stefan Krueger, 11/03/99
  59. //
  60. // Copyright (c) 1999-2000 GeoGraphix, Inc.
  61. //
  62. //////////////////////////////////////////////////////////////////////////////



  63. /////////////////////////////////////////////////
  64. // Function prototypes.
  65. /////////////////////////////////////////////////

  66. prototype POINTER ArrayToPointer(BYREF VARIANT);
  67. prototype NUMBER  ProcessEnd(STRING);
  68. prototype BOOL    ProcessRunning(STRING);

  69. // Kernel functions.

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

  72. // Process information functions.

  73. prototype NUMBER PSAPI.EnumProcesses(POINTER, NUMBER, BYREF NUMBER);
  74. prototype NUMBER PSAPI.EnumProcessModules(NUMBER, BYREF NUMBER, NUMBER,
  75.         BYREF NUMBER);
  76. prototype NUMBER PSAPI.GetModuleFileNameExA(NUMBER, NUMBER, BYREF STRING,
  77.         NUMBER);



  78. /////////////////////////////////////////////////
  79. // Structures.
  80. /////////////////////////////////////////////////

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

  82. typedef _SAFEARRAY
  83. begin
  84.     SHORT   cDims;
  85.     SHORT   fFeatures;
  86.     LONG    cbElements;
  87.     LONG    cLocks;
  88.     POINTER pvData;
  89.     // rgsaBound omitted
  90. end;

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

  92. typedef _VARIANT
  93. begin
  94.     SHORT  vt;
  95.     SHORT  wReserver1;
  96.     SHORT  wReserved2;
  97.     SHORT  wReserved3;
  98.     NUMBER nData;
  99. end;


  100.         
  101. /////////////////////////////////////////////////
  102. // Constants.
  103. /////////////////////////////////////////////////

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

  106. // Process information constants.

  107. #define PROCESS_QUERY_INFORMATION  0x400
  108. #define PROCESS_ALL_ACCESS         0x1f0fff
  109. #define PROCESS_VM_READ            0x10



  110. //////////////////////////////////////////////////////////////////////////////
  111. //
  112. // Function:    ArrayToPointer
  113. //
  114. // Description: Converts an InstallShield array into a C array.
  115. //
  116. //              When an array is created in InstallScript, a VARIANT variable
  117. //              is created which holds an OLEAutomation SAFEARRAY. To pass
  118. //              such an array to a DLL function expecting a C-style array,
  119. //              this function explicitly typecasts the pointer to the array
  120. //              to a _VARIANT pointer so that the _SAFEARRAY pointer can be
  121. //              extracted. The pointer to the actual data is then extracted
  122. //              from the _SAFEARRAY pointer.
  123. //
  124. // Parameters:  structArray - Array variable.
  125. //
  126. // Returns:     POINTER - Pointer to array.
  127. //
  128. //////////////////////////////////////////////////////////////////////////////

  129. function POINTER ArrayToPointer(structArray)
  130.     _SAFEARRAY POINTER pstructArray;    // _SAFEARRAY array pointer
  131.     _VARIANT   POINTER pstructVariant;  // _VARIANT array pointer
  132. begin
  133.     // Typecast the pointer to the array to a _VARIANT pointer.
  134.    
  135.     pstructVariant = &structArray;
  136.    
  137.     // Extract the _SAFEARRAY pointer from the _VARIANT.
  138.    
  139.     pstructArray = pstructVariant->nData;
  140.    
  141.     // Return the pointer to the actual data from the _SAFEARRAY.
  142.    
  143.     return pstructArray->pvData;
  144. end;



  145. //////////////////////////////////////////////////////////////////////////////
  146. //
  147. // Function:    _Process_End
  148. //
  149. // Description: Terminates running processes for the specified application.
  150. //
  151. // Parameters:  szAppName - Name of the application to terminate.
  152. //
  153. // Returns:     >= 0 - Number of processes terminated.
  154. //                -1 - Failure.
  155. //
  156. //////////////////////////////////////////////////////////////////////////////

  157. function NUMBER ProcessEnd(szAppName)
  158.     NUMBER  nvReturn;           // Number of processes terminated
  159.     NUMBER  nvProcessIDs(512);  // Array of process IDs
  160.     NUMBER  nvBytesReturned;    // Number of bytes returned in process ID array
  161.     NUMBER  nvProcesses;        // Number of processes running
  162.     NUMBER  nvIndex;            // Loop index
  163.     NUMBER  nvProcessHandle;    // Handle to a process
  164.     NUMBER  nvModuleHandle;     // Handle to a process module
  165.     NUMBER  nvBytesRequired;    // Number of bytes required to store values
  166.     POINTER pvProcessIDs;       // Pointer to process ID array
  167.     STRING  svModuleName;       // Module name
  168.     STRING  svFileName;         // Module filename
  169. begin
  170.     // The psapi.dll reads the Windows NT performance database. The DLL
  171.     // is part of the Win32 SDK.
  172.    
  173.     if UseDLL(WINSYSDIR ^ PSAPI_FILE) < 0 then
  174.         // Could not load psapi.dll.
  175.         
  176.         MessageBox("ERROR: Could not load [" + WINSYSDIR ^ PSAPI_FILE +
  177.                 "].", SEVERE);
  178.         
  179.         return -1;
  180.     endif;
  181.    
  182.     // Get the PIDs of all currently running processes.
  183.    
  184.     pvProcessIDs = ArrayToPointer(nvProcessIDs);

  185.     EnumProcesses(pvProcessIDs, 512, nvBytesReturned);

  186.     // Determine the number of process IDs retrieved. Each process ID
  187.     // is PROCESSID_LENGTH bytes.
  188.    
  189.     nvProcesses = nvBytesReturned / PROCESSID_LENGTH;
  190.    
  191.     // Get the executable associated with each process, and check if
  192.     // its filename matches the one passed to the function.
  193.    
  194.     for nvIndex = 1 to nvProcesses
  195.         // Get a handle to the process. The OpenProcess function
  196.         // must have full (all) access to be able to terminate
  197.         // processes.
  198.         
  199.         nvProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION |
  200.                 PROCESS_ALL_ACCESS, 0, nvProcessIDs(nvIndex));
  201.                
  202.         if nvProcessHandle != 0 then
  203.             // Get a handle to the first module in the process, which
  204.             // should be the executable.
  205.             
  206.             if EnumProcessModules(nvProcessHandle, nvModuleHandle,        
  207.                     PROCESSID_LENGTH, nvBytesRequired) != 0 then
  208.                 // Get the path of the module.
  209.                
  210.                 if GetModuleFileNameExA(nvProcessHandle, nvModuleHandle,
  211.                         svModuleName, SizeOf(svModuleName)) != 0 then
  212.                     // Extract the filename (without an extension) from
  213.                     // the path.
  214.                     
  215.                     ParsePath(svFileName, svModuleName, FILENAME_ONLY);

  216.                     if StrCompare(svFileName, szAppName) = 0 then
  217.                         // The process module matches the application
  218.                         // name passed to the function.
  219.                         
  220.                         if TerminateProcess(nvProcessHandle, 0) > 0 then
  221.                             nvReturn++;
  222.                         endif;
  223.                     endif;
  224.                 endif;
  225.             endif;
  226.         endif;
  227.     endfor;
  228.             
  229.     if UnUseDLL(PSAPI_FILE) < 0 then
  230.         MessageBox("ERROR: Could not unload [" + WINSYSDIR ^ PSAPI_FILE +
  231.                 "].", SEVERE);
  232.                
  233.         return -1;
  234.     endif;
  235.         
  236.     return nvReturn;
  237. end;



  238. //////////////////////////////////////////////////////////////////////////////
  239. //
  240. // Function:    _Process_Running
  241. //
  242. // Description: Determines if the specified process is running in memory.
  243. //
  244. // Parameters:  szAppName - Name of the application to check.
  245. //
  246. // Returns:     TRUE  - The process is running.
  247. //              FALSE - The process is not running.
  248. //
  249. //////////////////////////////////////////////////////////////////////////////

  250. function BOOL ProcessRunning(szAppName)
  251.     BOOL    bvRunning;          // Process is running
  252.     NUMBER  nvProcessIDs(512);  // Array of process IDs
  253.     NUMBER  nvBytesReturned;    // Number of bytes returned in process ID array
  254.     NUMBER  nvProcesses;        // Number of processes running
  255.     NUMBER  nvIndex;            // Loop index
  256.     NUMBER  nvProcessHandle;    // Handle to a process
  257.     NUMBER  nvModuleHandle;     // Handle to a process module
  258.     NUMBER  nvBytesRequired;    // Number of bytes required to store values
  259.     POINTER pvProcessIDs;       // Pointer to process ID array
  260.     STRING  svModuleName;       // Module name
  261.     STRING  svFileName;         // Module filename
  262. begin
  263.     // The psapi.dll reads the Windows NT performance database. The DLL
  264.     // is part of the Win32 SDK.
  265.    
  266.     if UseDLL(WINSYSDIR ^ PSAPI_FILE) < 0 then
  267.         // Could not load psapi.dll.
  268.         
  269.         MessageBox("ERROR: Could not load [" + WINSYSDIR ^ PSAPI_FILE +
  270.                 "].", SEVERE);
  271.         
  272.         return FALSE;
  273.     endif;
  274.    
  275.     // Get the PIDs of all currently running processes.
  276.    
  277.     pvProcessIDs = ArrayToPointer(nvProcessIDs);

  278.     EnumProcesses(pvProcessIDs, 512, nvBytesReturned);

  279.     // Determine the number of process IDs retrieved. Each process ID
  280.     // is PROCESSID_LENGTH bytes.
  281.    
  282.     nvProcesses = nvBytesReturned / PROCESSID_LENGTH;
  283.    
  284.     // Get the executable associated with each process, and check if
  285.     // its filename matches the one passed to the function.
  286.    
  287.     for nvIndex = 1 to nvProcesses
  288.         // Get a handle to the process.
  289.         
  290.         nvProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION |
  291.                 PROCESS_VM_READ, 0, nvProcessIDs(nvIndex));
  292.                
  293.         if nvProcessHandle != 0 then
  294.             // Get a handle to the first module in the process, which
  295.             // should be the executable.
  296.             
  297.             if EnumProcessModules(nvProcessHandle, nvModuleHandle,        
  298.                     PROCESSID_LENGTH, nvBytesRequired) != 0 then
  299.                 // Get the path of the module.
  300.                
  301.                 if GetModuleFileNameExA(nvProcessHandle, nvModuleHandle,
  302.                         svModuleName, SizeOf(svModuleName)) != 0 then
  303.                     // Extract the filename (without an extension) from
  304.                     // the path.
  305.                     
  306.                     ParsePath(svFileName, svModuleName, FILENAME_ONLY);
  307.                     
  308.                     if StrCompare(svFileName, szAppName) = 0 then
  309.                         // The process module matches the application
  310.                         // name passed to the function.
  311.                         
  312.                         bvRunning = TRUE;
  313.                         
  314.                         goto ProcessRunningEnd;
  315.                     endif;
  316.                 endif;
  317.             endif;
  318.         endif;
  319.     endfor;
  320.             
  321.     ProcessRunningEnd:
  322.         
  323.     if UnUseDLL(PSAPI_FILE) < 0 then
  324.         MessageBox("ERROR: Could not unload [" + WINSYSDIR ^ PSAPI_FILE +
  325.                 "].", SEVERE);
  326.                
  327.         return FALSE;
  328.     endif;
  329.         
  330.     return bvRunning;
  331. end;
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SgzyStudio

GMT+8, 2024-5-14 17:37 , Processed in 0.113311 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表