VBScript and sendkeys not working with DirectX? - vbscript

I am playing GTA SA:MP. It uses DirectX, and if you are AFK on the server you get automatically kicked, and I would like to let it AFK on the server while I'm left.
I tried creating a VBScript that automatically after 8 minutes presses W and then loops but it actually doesn't work. The player doesn't move.
Code:
Dim a
Dim antiafk
set antiafk=createobject("wscript.shell")
msgbox "Atentie! Ca sa opriti scriptul, trebuie sa il opriti NUMAI din Task Manager. Nu exista alta cale."
Do until a=5
wscript.sleep 480000
antiafk.sendkeys "w"
Loop
Any ideas?

SendKeys method sends one or more keystrokes to the active window. Use AppActivate method to activate your application window first.
The script is a simplified version of your one, with no 8 minutes loop and notepad.exe instead of your application name.
option explicit
On Error Goto 0
Dim antiafk
set antiafk=createobject("wscript.shell")
Dim lngPID, booSuccess, foo
lngPID = getlngPID()
If lngPID = 0 Then
foo = antiafk.Popup( "No instance found", 5, _
"29445451", vbOKOnly + vbCritical)
Else
booSuccess = antiafk.AppActivate( lngPID)
If not booSuccess Then
foo = antiafk.Popup( "AppActivate Method unsuccesfull", 5, _
"29445451", vbOKOnly + vbExclamation)
Else
antiafk.sendkeys "w"
End If
End If
Function getLngPID()
getLngPID = 0
Dim strComputer
Dim objWMIService, colProcessList, objProcess
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Notepad.exe'")
'find appropriate and suitable Process ID
For Each objProcess in colProcessList
getLngPID = objProcess.ProcessId
Next
End Function

Related

Can anyone help me close this program in VBScript?

MsgBox ("Do you want to start the autoclicker?", vbOkOnly, "Autoclicker")
CreateObject("WScript.Shell").Run("""C:\Users\Henry\Desktop\Fun.vbs""")
MsgBox ("Do you want to stop the autoclicker?", vbOkOnly, "Autoclicker")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objItem in colItems
'msgbox objItem.ProcessID & " " & objItem.CommandLine
If objItem.name = "Calculator.exe" then objItem.terminate
Next
This kills calculator.exe. Change it to wscript.exe. You might want to check command line if you just want to kill fun.vbs.
The following routine kills all processes whose command lines contain a specified string. The 3 lines below the routine are for testing it. We pause the routine by showing a message box and when you dismiss the message box, we kill the script instance, so the second message box doesn't show up. When you use it, you want to replace the last 3 lines with
KillProcesses "Fun.vbs"
I'd be careful using this and specify as much of the command line as possible to make sure I absolutely, positively match only the processes I want to terminate. You can modify the Task Manager and add a column to show the command line for every running process. In the routine below, the search in command line is case-insensitive.
Option Explicit
Sub KillProcesses(strPartOfCommandLine)
Dim colProcesses
Dim objProcess
Dim lReturn
' Get list of running processes using WMI
Set colProcesses = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * From Win32_Process")
For Each objProcess in colProcesses
If (Instr(1, objProcess.Commandline, strPartOfCommandLine, vbTextCompare) <> 0) Then
lReturn = objProcess.Terminate(0)
End If
Next
End Sub
Msgbox "Before being killed"
KillProcesses "KillProcesses.vbs"
Msgbox "After being killed"
I made before a script that ask you what vbscript did you want to kill and log the result into file.
So just, give a try :
Option Explicit
Dim Titre,Copyright,fso,ws,NomFichierLog,temp,PathNomFichierLog,OutPut,Count,strComputer
Copyright = "[© Hackoo © 2014 ]"
Titre = " Process "& DblQuote("Wscript.exe") &" running "
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject( "Wscript.Shell" )
NomFichierLog="Process_WScript.txt"
temp = ws.ExpandEnvironmentStrings("%temp%")
PathNomFichierLog = temp & "\" & NomFichierLog
Set OutPut = fso.CreateTextFile(temp & "\" & NomFichierLog,1)
Count = 0
strComputer = "."
Call Find("wscript.exe")
Call Explorer(PathNomFichierLog)
'***************************************************************************************************
Function Explorer(File)
Dim ws
Set ws = CreateObject("wscript.shell")
ws.run "Explorer "& File & "\",1,True
end Function
'***************************************************************************************************
Sub Find(MyProcess)
Dim colItems,objItem,Processus,Question
Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
& "Where Name like '%"& MyProcess &"%' AND NOT commandline like '%" & wsh.scriptname & "%'",,48)
For Each objItem in colItems
Count= Count + 1
Processus = Mid(objItem.CommandLine,InStr(objItem.CommandLine,""" """) + 2) 'Extraction of the commandline script path
Processus = Replace(Processus,chr(34),"")
Question = MsgBox ("Did you want to stop this script : "& DblQuote(Processus) &" ?" ,VBYesNO+VbQuestion,Titre+Copyright)
If Question = VbYes then
objItem.Terminate(0)'Kill this process
OutPut.WriteLine DblQuote(Processus)
else
Count= Count - 1 'decrement the counter -1
End if
Next
OutPut.WriteLine String(100,"*")
OutPut.WriteLine count & Titre & " were stopped !"
End Sub
'**********************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************

VBS Code for Program Terminator

I would like a VBS code that:
Displays an input box to enter a process name
and can append the process to the code written below:
Option Explicit
Dim strComputer, strProcessToKill, objWMIService, colProcess, objProcess
strComputer = "."
strProcessToKill = "notepad.exe"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer _
& "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")
For Each objProcess in colProcess
msgbox "... terminating " & objProcess.Name
objProcess.Terminate()
Next
Replace the line
strProcessToKill = "notepad.exe"
with
strProcessToKill = InputBox("Enter process name:")
You should add some safety checks, though, in case the user presses Cancel or presses OK without having anything entered:
If IsEmpty(strProcessToKill) Then
WScript.Echo "User pressed 'Cancel'."
WScript.Quit 1
ElseIf strProcessToKill = "" Then
WScript.Echo "Nothing entered."
WScript.Quit 1
End If

VB script Kill process and delete folder

I am a noop when it comes to vbscript but I managed to make something. But now I am kinda stuck.
Let me try to explain what I am trying to do
I need to kill a process on a computer [That part I know and made] VBpart 1
When the process is killed I need to delete a folder with all it content [ That I managed also] VB part 2
VB part 1
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim xComputer, xProcessKill
xComputer = "."
xProcessKill = "'diamant client.exe'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& xComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & xProcessKill)
For Each objProcess in colProcess
objProcess.Terminate()
next
VB part 2
strFolder = "\\srvad01\DFS\Sandbox$\username\sandbox\DiaSoft Framework Diamant Client 1.0 NL"
set objFSO = createobject("Scripting.FileSystemObject")
objFSO.DeleteFolder strFolder
But somehow I can't merge these 2 in 1 file While they work separate fine
What am I doing wrong?
Kind regarts Arie

How can I limit the number of instances of an application being run in a single Windows session?

Some time ago on I asked about limiting the number of instances of Excel being run concurrently in Windows.
Thanks to the help I got on StackOverflow.com I was able to put together the following function that shuts down any instance of Excel that is launched if there is already another instance of Excel running.
Private Function KillDuplicateProcesses() As Boolean
Dim objWMIService As Object
Dim colItems As Variant
Dim objItem As Object
Dim intCount As Integer
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.InstancesOf("Win32_Process")
For Each objItem In colItems
intCount = intCount + Abs(LCase(objItem.Name) = "excel.exe")
If intCount > 1 Then
MsgBox "Excel is already running." & vbCrLf & vbCrLf & _
"To open a file use " & IIf(Application.Version >= 12, "Office Button", "File") & " > Open (Ctrl + O).", vbCritical
KillDuplicateProcesses = True
Application.Quit
Exit For
End If
Next
End Function
The problem is that if a user is logged onto a remote desktop session as an administrator, that user account can see all of the other users and the processes that they have running. So if another user is logged onto the same machine and is running Excel, the function counts those instances as well and shuts down the instance of Excel that has just been launched.
I need to limit the scope of that function to the currently running session. According to MSDN documentation there is a class property called SessionID. Can I use that property and compare it against the current session's ID to limit what the function counts, or is there a better way to do it?
Any suggestions would be greatly appreciated.
Thanks!
Below is the solution code per Tim's suggestion. Note I am comparing the GetOwner properties against Environ UserName and UserDomain. Environ is considered unreliable because it can be changed by the user.
Private Function KillDuplicateProcesses() As Boolean
Dim objWMIService As Object
Dim colItems As Variant
Dim objItem As Object
Dim intCount As Integer
Dim strProcessUser As Variant
Dim strProcessDomain As Variant
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'excel.exe'")
If colItems.Count > 1 Then
For Each objItem In colItems
strProcessUser = ""
strProcessDomain = ""
objItem.GetOwner strProcessUser, strProcessDomain
If IsNull(strProcessUser) Then strProcessUser = ""
If IsNull(strProcessDomain) Then strProcessDomain = ""
intCount = intCount + Abs(strProcessUser = Environ("UserName") _
And strProcessDomain = Environ("UserDomain"))
If intCount > 1 Then
MsgBox "You cannot run more than one instance of Excel while iTools is activated." & vbCrLf & vbCrLf & _
"To open a file use " & IIf(Application.Version >= 12, "Office Button", "File") & " > Open (Ctrl + O).", vbCritical
KillDuplicateProcesses = True
Application.Quit
Exit For
End If
Next
End If
End Function
'get process owner username and domain
Dim strUser, strDomain
objItem.getOwner strUser, strDomain
MsgBox strUser & ", " & strDomain

VBScript to shutdown Windows when a process ends?

I have a program that scans through data at the end of the night on some occasions. On those occasions, I would like to run a VBScript that will watch for that program to close, and when it does, will shut down Windows.
I created a .BAT file that runs the program and then shuts Windows down, but I don't always need to shutdown when I finish using the program.
So I would like to use the scanning program, and if, at the end of the night, I am ready to leave, but the program is still scanning, I would to open the VBScript that will watch for my scanning program to close.
Is this possible?
Windows 7 Ultimate
x64 UAC = ON
Well, I figured out how to do this via this post at Techimo.com.
Dim isRunning, wasRunningAtStart, strComputer, strShutdown, objWMIService
Dim objcolProcesses, objShell, strProcesses, strProcessName
'boolean condition for the loop
isRunning = True
wasRunningAtStart = True
'-----Specify the computer name on which to watch a process:
strComputer = "." '>>> "." for this computer
'-----Specify the process to watch. Must be enclosed in Single Quotes:
strProcessName = "'processname.exe'" '>>> Example: "'notepad.exe'"
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
strProcesses = "SELECT * FROM Win32_Process WHERE Name = "
strShutdown = "shutdown -s -t 0 -f -m \\" & strComputer
Set objShell = CreateObject("WScript.Shell")
'Check the process once, no need to run if the process
'isn't already running
'Query WMI for the running processes matching our process name
Set objColProcesses = objWMIService.ExecQuery ( _
strProcesses & strProcessName)
'If the process is running, the count will be greater than 0,
'so we switch our boolean here to exit the loop.
If objcolProcesses.Count = 0 Then
wasRunningAtStart = False
isRunning = False
End If
Set objColProcesses = Nothing
Do While isRunning
'Wait 2 seconds, prevents this script from using the CPU
WScript.Sleep 2000
'Query WMI for the running processes matching our process name
Set objColProcesses = objWMIService.ExecQuery ( _
strProcesses & strProcessName)
'If the process is running, the count will be greater than 0,
'so we switch our boolean here to exit the loop.
If objColProcesses.Count = 0 Then
isRunning = False
End If
Loop
If wasRunningAtStart Then
'MsgBox "Would shutdown here"
objShell.Run strShutdown
Else
MsgBox "The specified program is not already running."
End If
Set objColProcesses = Nothing
Set objShell = Nothing
Set objWMIService = Nothing
' Shutdown.vbs
' Example VBScript to Shutdown computers
' Author Josh Murray
' Version 4.1 - February 2007
' --------------------------------------Option Explicit
Dim objShell, strComputer, strInput
Dim strShutdown
Do
strComputer = (InputBox(" ComputerName to shutdown", "Computer Name"))
If strComputer <> "" Then
strInput = True
End if
Loop until strInput = True
strShutdown = "shutdown -s -t 0 -f -m \\" & strComputer
set objShell = CreateObject("WScript.Shell")
objShell.Run strShutdown
Wscript.Quit

Resources