

'
'requires Sapero's Windows include files
'--------------------------------------- 
'
'this code works better on Win32 than Win64 - can anyone fix it :-) 
'
$ifndef WIN32
$define WIN32
$endif

$ifdef WIN32
$define WIN32_LEAN_AND_MEAN
$endif
 
$include "windowssdk.inc"
$include "Psapi.inc"
$include "TlHelp32.inc"

DWORD aProcesses[1024], cbNeeded, cProcesses, procId, bProcess[512] 
handle hProcess, hTokenSelf, hProcSelf
 
OPENCONSOLE

hProcSelf = OpenProcess( PROCESS_ALL_ACCESS,FALSE,GetCurrentProcessId() )
OpenProcessToken(hProcSelf,TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY ,&hTokenSelf)

SetPrivilege(hTokenSelf,"SeDebugPrivilege",true)

retval = findPIDByName("")

print "Press any key to get the hell out of here"

WAITCON

CLOSECONSOLE
END




sub findPIDByName(string fileNameToFindPID),int
'// required by fined PIDByName
const TH32CS_SNAPHEAPLIST	=0x00000001
const TH32CS_SNAPPROCESS	=0x00000002
const TH32CS_SNAPTHREAD		=0x00000004
const TH32CS_SNAPMODULE		=0x00000008
const TH32CS_SNAPMODULE32	=0x00000010
const TH32CS_SNAPALL		=(TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE)
const TH32CS_INHERIT		=0x80000000
const PROCESS_ALL_ACCESS	=0x1F0FFF


type PROCESSENTRY32
	uint dwSize
	uint cntUsage
	uint th32ProcessID
	uint th32DefaultHeapID
	uint th32ModuleID
	uint cntThreads
	uint th32ParentProcessID
	uint pcPriClassBase
	uint dwFlags
	istring szExeFile[259]
endtype

def pe:PROCESSENTRY32
string item,PID
int retval, x = 0


hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0)
pe.dwSize=len(pe)
retval=Process32First(hSnapshot,pe)

do

item = pe.szExeFile
PID = STR$(pe.th32ProcessID)

x++

	dwPriorityClass = 0
 
def dwVerHnd:word
dwVerHnd = 0

string lpFilename=space$(255)
DEF xxx$:istring
hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe.th32ProcessID)
GetModuleFileNameEx(hProcess,0,lpFilename,len(lpFilename))
dwVerInfoSize = GetFileVersionInfoSize(lpFilename, dwVerHnd)

PRINT lpFilename," ",dwVerinfoSize

    if hProcess = NULL
      print error("Open Process Fail findpidbyname ")
    else
    
      dwPriorityClass = GetPriorityClass(hProcess)

      if dwPriorityClass <>0
			'print dwPriorityClass 
		ELSE
			print error("dwPriorityClass")
		endif
	CloseHandle(hProcess)

 
endif

	pe.dwSize=len(PROCESSENTRY32)
	retval=Process32Next(hSnapshot,pe)

until retval = false

CloseHandle(hSnapshot)
return 0
ENDSUB

sub findProcessIdByName(int processID),INT

int ret 
string szProcessName

szProcessName = SPACE$(255)

hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE,processID )

	if hProcess <>0
		
		ret = EnumProcessModules( hProcess, bProcess[0], len(bProcess), &cbNeeded)
 
			if ret <>0
				ret = GetModuleBaseName(hProcess, bProcess[0], szProcessName,len(szProcessName))
'				ret = GetModuleFileNameEx(hProcess, bProcess[0], szProcessName,len(szProcessName))

	
					if ret <>0
						print szProcessName
					ELSE
						print error("GetModule")
					ENDIF

			ELSE
				error("EnumProcessModules")
			ENDIF
		CloseHandle( hProcess )
	ELSE
		print error( "Openprocess Failed"+str$(processID))
	ENDIF
 return 0
ENDSUB

sub killProcess(int PID)

HANDLE hProc

hProc = OpenProcess(PROCESS_ALL_ACCESS|PROCESS_TERMINATE, FALSE, PID)

          if hProc <>0

               if TerminateProcess(hProc, 0) <>0
                    MessageBox (NULL, "CLOSED", "CLOSED", @MB_ICONSTOP) 
               else 
                    MessageBox (NULL, error("SUB KillProcess "), "NOT CLOSED PID"+str$(procId), @MB_ICONSTOP) 
                  CloseHandle(hProc)
			   endif
          else 
               MessageBox (NULL, error("SUB KillProcess "),"I CANT CLOSE "+str$(procId),@MB_ICONSTOP) 
		  ENDIF
ENDSUB


sub SetPrivilege(HANDLE hToken, string lpszPrivilege,INT bEnablePrivilege) 


	def tp:TOKEN_PRIVILEGES
	def luid:LUID

	if LookupPrivilegeValue(NULL,lpszPrivilege,&luid ) = 0
		error("LookupPrivilegeValue error: ")
		return FALSE
	ENDIF

	tp.PrivilegeCount = 1
	tp.Privileges[0].Luid = luid
	
    if bEnablePrivilege = true
		tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED
	else
		tp.Privileges[0].Attributes = 0
	ENDIF

	'// Enable the privilege or disable all privileges.

	if AdjustTokenPrivileges(hToken,FALSE, &tp, LEN(TOKEN_PRIVILEGES), 0, 0) =0
			error("AdjustTokenPrivileges error:") 
		  return FALSE 
	ENDIF 

	if GetLastError() = ERROR_NOT_ALL_ASSIGNED

		  print "The token does not have the specified privilege."
		  return FALSE
	ENDIF

	return TRUE

ENDSUB

SUB error(string errorCall),string  

   INT CodeErrorId, nBufferSize, flag 
   STRING sBuffer,retError  

   nBufferSize = 1024 
   sBuffer = String$(nBufferSize, Chr$(0)) 
   flag=FORMAT_MESSAGE_FROM_SYSTEM 

   CodeErrorId=GetLastError() 

   FormatMessage(flag, NULL,CodeErrorId,LANG_NEUTRAL, sBuffer, nBufferSize ,NULL ) 
	retError = errorCall+" / "+sBuffer+" / ErrorID = "+str$(CodeErrorID)

return retError

endsub

sub findPIDByNameCount(string fileNameToFindPID),int
'// required by fined PIDByName
const TH32CS_SNAPHEAPLIST	=0x00000001
const TH32CS_SNAPPROCESS	=0x00000002
const TH32CS_SNAPTHREAD		=0x00000004
const TH32CS_SNAPMODULE		=0x00000008
const TH32CS_SNAPMODULE32	=0x00000010
const TH32CS_SNAPALL		=(TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE)
const TH32CS_INHERIT		=0x80000000
const PROCESS_ALL_ACCESS	=0x1F0FFF


type PROCESSENTRY32
	uint dwSize
	uint cntUsage
	uint th32ProcessID
	uint th32DefaultHeapID
	uint th32ModuleID
	uint cntThreads
	uint th32ParentProcessID
	uint pcPriClassBase
	uint dwFlags
	istring szExeFile[259]
endtype

def pe:PROCESSENTRY32
string item,PID
int retval, x = 0


hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0)
pe.dwSize=len(pe)
retval=Process32First(hSnapshot,pe)

kmscount = 0

DO

PRINT
PRINT
PRINT

item = pe.szExeFile
PID = STR$(pe.th32ProcessID)

item = LTRIM$(item)


x++

	dwPriorityClass = 0
 
	def dwVerHnd:word
	dwVerHnd = 0

	string lpFilename=space$(255)
	hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe.th32ProcessID)
	GetModuleFileNameEx(hProcess,0,lpFilename,len(lpFilename))
	dwVerInfoSize = GetFileVersionInfoSize(lpFilename, dwVerHnd)

    PRINT lpFilename," ",dwVerinfoSize

    if hProcess = NULL
      print error("Open Process Fail findpidbyname ")
    else
    
      dwPriorityClass = GetPriorityClass(hProcess)

      if dwPriorityClass <>0
			'print dwPriorityClass 
		ELSE
			print error("dwPriorityClass")
		endif
	CloseHandle(hProcess)

' Print process Info
    'print "process ID        = ",pe.th32ProcessID
    'print "thread count      = ",pe.cntThreads
    'print "parent process ID = ",pe.th32ParentProcessID
    'print "Priority Base     = ",pe.pcPriClassBase
	'print "Priority Class    = ",dwPriorityClass
 
endif

	pe.dwSize=len(PROCESSENTRY32)
	retval=Process32Next(hSnapshot,pe)

until retval = false

CloseHandle(hSnapshot)
return 0
ENDSUB