troubleshooting Question

Convert VB code to Pascal Script...

Avatar of ragoran
ragoranFlag for Canada asked on
DelphiPascalInstallation
9 Comments1 Solution1543 ViewsLast Modified:
I have this code snippet that is working fine in VBA.  This code will search the running processes and terminate a specific one if it is running.

I need to convert this code to Pascal Script to be included in the uninstall portion of an Inno Setup package.

I never used Pascal Script before, but with the help of some documentation and many example, I managed to get a compile code that runs...but do nothing.

I think the problem is two-fold:

1- How to specify the buffer size in the Record pass to the Windows API function?
2- How to get the left most character of the resulting string for comparison


' THIS IS THE WORKING VBA CODE
Private Type PROCESSENTRY32
    lSize            As Long
    lUsage           As Long
    lProcessId       As Long
    lDefaultHeapId   As Long
    lModuleId        As Long
    lThreads         As Long
    lParentProcessId As Long
    lPriClassBase    As Long
    lFlags           As Long
    ExeFile         As String * 260
End Type

Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
    Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, _
    ByVal lProcessId As Long) As Long
    
Private Declare Function ProcessFirst Lib "kernel32" _
    Alias "Process32First" (ByVal hSnapshot As Long, _
    uProcess As PROCESSENTRY32) As Long
    
Private Declare Function ProcessNext Lib "kernel32" _
    Alias "Process32Next" (ByVal hSnapshot As Long, _
    uProcess As PROCESSENTRY32) As Long


Private Declare Function OpenProcess Lib "kernel32" _
  (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
   ByVal dwProcessId As Long) As Long

Declare Function TerminateProcess Lib "kernel32" _
    (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)



Public Sub KillYSCProcess()

   Dim lSnapShot  As Long
   Dim uProcess   As PROCESSENTRY32
   Dim r          As Long
   Dim nPrc       As Long
   
   lSnapShot = CreateToolhelpSnapshot(2&, 0&)
   If lSnapShot <> 0 Then
      
      uProcess.lSize = Len(uProcess)
      r = ProcessFirst(lSnapShot, uProcess)
      
      Do While r
         If Left(uProcess.ExeFile, 18) = "YPGStayConnect.exe" Then
            nPrc = OpenProcess(&H1F0FFF, 0&, uProcess.lProcessId)
            TerminateProcess nPrc, 0&
            Exit Do
         End If
         r = ProcessNext(lSnapShot, uProcess)
      Loop
      CloseHandle (lSnapShot)
   End If

End Sub
// THIS IS THE PASCAL SCRIPT CODE (NOT WORKING)

type
	ProcessEntry32 = Record
		lSize: DWORD;
		lUsage: DWORD;
		lProcessID: WORD;
		lDefaultHeapID: DWORD;
		lModuleID: DWORD;
		lThreads: DWORD;
		lParentProcessID: DWORD;
		lPriClassBase: DWORD;
		lFlags: DWORD;
		ExeFile: string;
	end;


function CreateToolhelpSnapshot(lFlags: DWORD; lProcessId: DWORD): DWORD;
external 'CreateToolhelp32Snapshot@Kernel32.dll';

function ProcessFirst(hSnapShot: DWORD; var uProcess: ProcessEntry32): DWORD;
external 'Process32First@kernel32.dll';

function ProcessNext(hSnapShot: DWORD; var uProcess: ProcessEntry32): DWORD;
external 'Process32Next@kernel32.dll';

function OpenProcess(dwDesiredAccess:DWORD; bInheritHandle:DWORD; dwProcessId:DWORD):DWORD;
external 'OpenProcess@kernel32.dll';

function TerminateProcess(hprocess:DWORD; uExitCode:DWORD):DWORD;
external 'TerminateProcess@kernel32.dll';


procedure KillYSCProcess;
var
   wProcess: ProcessEntry32;
   wSnapshot: DWORD;
   wPrc: DWORD;
begin
	wProcess.lSize := sizeof(wProcess);
	wSnapshot:= CreateToolhelpSnapshot(2,0)
	// msgbox(char(wProcess.lSize) + ' ' + char(wSnapshot,mbInformation), MB_OK);
	if wSnapshot <> 0 then begin
		if processFirst(wSnapshot, wProcess)<>0 then begin
			repeat
				msgbox(wprocess.ExeFile, mbInformation, MB_OK);
				if wProcess.ExeFile = 'YPGStayConnect.exe' then begin
					msgbox('Process Found',mbInformation, MB_OK);
					wPrc:= OpenProcess(2035711 ,0, wProcess.lProcessId);
					TerminateProcess(wPrc,0);
					break;
				end;
			until processNext(wSnapshot,  wProcess)<>0;
		end;
	end;
end;

function InitializeUninstall(): Boolean;
begin
	msgbox('hunt YSC process', mbInformation, MB_OK);
	killYSCProcess;
	result:=true;
end;
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros