Jump to content

See if a File is open if Open close file?


ashley
 Share

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

$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]

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...