Jump to content
Sign in to follow this  
ashley

See if a File is open if Open close file?

Recommended Posts

ashley

Ok, well im still making this parental Control system,

I have the user browse for a file, so that it has the Full file path!

How to i check to see if that file is running!

And if it is running how to i close it!

Would i use?

If ProcessExists($File) Then
    MsgBox(0, "Parental Control", $File & " is running.")
EndIf

ProcessClose($File)

$PID = ProcessExists($File); Will return the PID or 0 if the process isn't found.
If $PID Then ProcessClose($PID)

Thanks in advance

Code taken from helpfiles

Edited by ashley

Share this post


Link to post
Share on other sites
Rental

you can use Processexists() to see if something is running, and processclose() to to close it

Rental

Share this post


Link to post
Share on other sites
Andreik

But what about if the file opens another file and then closes? Will the other file open?

Try this, original code by Siao:

Func IsOpenFile($PATH)
Local $GENERIC_READ  = 0x80000000
Local $GENERIC_WRITE = 0x40000000
Local $OPEN_EXISTING = 3
Local $FILE_ATTRIBUTE_NORMAL = 0x80

If Not FileExists($PATH) Then SetError(1,0,-1)
LOCAL $hFile = DllCall("kernel32.dll", "hwnd", "CreateFile", _
                  "str", $PATH, _
                  "int", BitOR($GENERIC_READ, $GENERIC_WRITE), _
                  "int", 0, _
                  "ptr", 0, _
                  "int", $OPEN_EXISTING, _
                  "int", $FILE_ATTRIBUTE_NORMAL, _
                  "int", 0)
If $hFile[0] = -1 Then
    Return True
Else
    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile[0])
    Return False
EndIf
EndFunc

When the words fail... music speaks

Share this post


Link to post
Share on other sites
ashley

Try this, original code by Siao:

Func IsOpenFile($PATH)
Local $GENERIC_READ  = 0x80000000
Local $GENERIC_WRITE = 0x40000000
Local $OPEN_EXISTING = 3
Local $FILE_ATTRIBUTE_NORMAL = 0x80

If Not FileExists($PATH) Then SetError(1,0,-1)
LOCAL $hFile = DllCall("kernel32.dll", "hwnd", "CreateFile", _
                  "str", $PATH, _
                  "int", BitOR($GENERIC_READ, $GENERIC_WRITE), _
                  "int", 0, _
                  "ptr", 0, _
                  "int", $OPEN_EXISTING, _
                  "int", $FILE_ATTRIBUTE_NORMAL, _
                  "int", 0)
If $hFile[0] = -1 Then
    Return True
Else
    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile[0])
    Return False
EndIf
EndFunc

Okay, i have noidea what any of that means or does!

Share this post


Link to post
Share on other sites
Andreik

Okay, i have noidea what any of that means or does!

I don't know what you don't understand, it's very simple:

Run("calc.exe")
MsgBox(0,"","Run/Open: " & IsOpenFile(@SystemDir & "\Calc.exe"))
ProcessClose("calc.exe")
MsgBox(0,"","Run/Open: " & IsOpenFile(@SystemDir & "\Calc.exe"))

Func IsOpenFile($PATH)
Local $GENERIC_READ  = 0x80000000
Local $GENERIC_WRITE = 0x40000000
Local $OPEN_EXISTING = 3
Local $FILE_ATTRIBUTE_NORMAL = 0x80

If Not FileExists($PATH) Then SetError(1,0,-1)
LOCAL $hFile = DllCall("kernel32.dll", "hwnd", "CreateFile", _
                  "str", $PATH, _
                  "int", BitOR($GENERIC_READ, $GENERIC_WRITE), _
                  "int", 0, _
                  "ptr", 0, _
                  "int", $OPEN_EXISTING, _
                  "int", $FILE_ATTRIBUTE_NORMAL, _
                  "int", 0)
If $hFile[0] = -1 Then
    Return True
Else
    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile[0])
    Return False
EndIf
EndFunc

When the words fail... music speaks

Share this post


Link to post
Share on other sites
jerem488

Otherwise, you can use the WinList() function.

$var = WinList()

For $i = 1 to $var[0][0]
  ; Only display visble windows that have a title
  If $var[$i][0] <> "" And IsVisible($var[$i][1]) Then
    WinClose($var[$i][0])
  EndIf
Next

Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 1 ) Then ; if windows exists
    Return 1
  Else
    Return 0
  EndIf

EndFunc

After you can play with differents values of WinGetState

Edited by jerem488

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Share this post


Link to post
Share on other sites
Andreik

How would this work if the user had to type in the file path into an input box?

Please post your code.

When the words fail... music speaks

Share this post


Link to post
Share on other sites
jerem488

$var = WinList()

For $i = 1 to $var[0][0]
  ; Only display visble windows that have a title
  If $var[$i][0] <> "" And IsVisible($var[$i][1]) And StringInStr($var, $File) Then
    MsgBox(0, "Parental Control", $File & " is running.")
    WinClose($var[$i][0])
  EndIf
Next

Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 1 ) Then ; if windows exists
    Return 1
  Else
    Return 0
  EndIf

EndFunc

normally this code works....


Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.