Jump to content

Security Program (Problem)


Recommended Posts

Hi everyone, i have a little problem with my Security Program, when i "lock" the PC the program starts a loop that can't make me do anything but closing it from the "Activity Manager", and then restart explorer.exe... Maybe ther's a constant i forgot? it's driving me crazy, please help me...

#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.6.1
Author: Edward Kirk

Script Function:
Programma Sicurezza

#ce ----------------------------------------------------------------------------
#include<GuiConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $id = 'asd', $psw = '123'
$maingui=GUICreate('Blocco PC',200,100)
$b=guictrlcreatebutton('Lock',30,40)
GUISetBkColor('FFFFFFF',$maingui)
$sb=guictrlcreatebutton('Unlock',100,40)
GUISetState()

while 1
Switch GUIGetMsg()
Case $b
while 1
If ProcessExists('explorer.exe') Then
ProcessClose('explorer.exe')
EndIf
WEnd
case $sb
$logingui=GUICreate('Login',170,130)
GUICtrlCreateLabel('Name',2,2)
GUICtrlSetFont(-1,8,5000)
$nome = GUICtrlCreateInput('',2,25,90,20)
GUICtrlSetFont(-1,8,5000)
GUICtrlCreateLabel('Password',2,50)
GUICtrlSetFont(-1,8,5000)
$pw = GUICtrlCreateInput('',2,65,90,20,0x00020)
GUICtrlSetFont(-1,8,5000)
$login = GUICtrlCreateButton('Login',2,90,80,20)
GUICtrlSetFont(-1,8,5000)
GUISetState()

While 1
Switch guigetmsg()
case $GUI_EVENT_CLOSE
GUIDelete($logingui)
ExitLoop
Case $login
If GUICtrlRead($nome) = $id And guictrlread($pw) = $psw Then
ShellExecute('C:\Windows\explorer.exe')
Else
beep(3000,3000)
beep(3000,3000)
beep(3000,3000)
EndIf
GUIDelete($logingui)
EndSwitch
WEnd
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd

Link to comment
Share on other sites

  • Moderators

Doctorkirk,

no one who cares about my problem?

Quite probably the correct answer. ;)

However, as you seem so put out I will welcome to the Autoit Forum - and put you out of your misery. :D

You correctly state that the script enters an infinite loop and then becomes unresponsive. So the trick is to get the outer loop to include the check for Explorer.exe - you can use a flag to only check when you need. And as a matter of fact you would also never leave the unlock loop either. :o

See if you can follow what is happening here - it tells you what is going on as you progress through the script:

#include<GuiConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $id = 'asd', $psw = '123', $fFlag = False

$maingui = GUICreate('Blocco PC', 200, 100)
$b = GUICtrlCreateButton('Lock', 30, 40)
GUISetBkColor('FFFFFFF', $maingui)
$sb = GUICtrlCreateButton('Unlock', 100, 40)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $b
            MsgBox(0, "Hi", "Setting the flag")
            $fFlag = True ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Set the flag
        Case $sb
            $logingui = GUICreate('Login', 170, 130)
            GUICtrlCreateLabel('Name', 2, 2)
            GUICtrlSetFont(-1, 8, 5000)
            $nome = GUICtrlCreateInput('', 2, 25, 90, 20)
            GUICtrlSetFont(-1, 8, 5000)
            GUICtrlCreateLabel('Password', 2, 50)
            GUICtrlSetFont(-1, 8, 5000)
            $pw = GUICtrlCreateInput('', 2, 65, 90, 20, 0x00020)
            GUICtrlSetFont(-1, 8, 5000)
            $login = GUICtrlCreateButton('Login', 2, 90, 80, 20)
            GUICtrlSetFont(-1, 8, 5000)
            GUISetState()

            While 1
                Switch GUIGetMsg()
                    Case $GUI_EVENT_CLOSE
                        ExitLoop
                    Case $login
                        If GUICtrlRead($nome) = $id And GUICtrlRead($pw) = $psw Then
                            ;ShellExecute('C:\Windows\explorer.exe')
                            MsgBox(0, "Hi", "Explorer restarted")
                            $fFlag = False  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Clear flag
                            ToolTip("")
                            ExitLoop ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Exit the loop
                        Else
                            Beep(3000, 3000)
                            Beep(3000, 3000)
                            Beep(3000, 3000)
                        EndIf
                EndSwitch
            WEnd
            GUIDelete($logingui)
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If $fFlag Then ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Only check and close if the flag is set
        ToolTip("Checking for Explorer")
        ;If ProcessExists('explorer.exe') Then
        ;    ProcessClose('explorer.exe')
        ;EndIf
    EndIf

WEnd

All clear? Please ask if not. :)

And finally, I am not really in favour of your method of lockng the PC - there are much better ways out there if you search the forum. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Yopu are not providing an exit from the while...wend loop when the $b control is actioned...

kylomas

edit: Pshaw! Scooped by M23 again...got to get a new set of fingers...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Moderators

kylomas,

Let me know next time and I will slow down a bit! :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Quite probably the correct answer. ;)

hahahah Thanks for replying me instead! :)

btw, yes i quite i got it, the problem is understanding the flag thing... but i can try to understand it reading another 10 or 15 times the code hahah

I know there are much better ways to lock the PC, but it was a "project"... and when i start a thing i wanna end it, by myself or with some help :sweating:

Thanks again for your help! :D

Link to comment
Share on other sites

  • Moderators

Doctorkirk,

the problem is understanding the flag thing

As I said above: if you have questions then please ask - that is why I come here. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Doctorkirk,

As I explained, you need to break out of the inner loop when you want to lock the PC. Can you see that this loop has no exit: :huh:

Case $b
    While 1
        If ProcessExists('explorer.exe') Then
            ProcessClose('explorer.exe')
        EndIf
    WEnd

And if you stay in that loop then you never run the GUIGetMsg in the outer loop - so you can never unlock as the GUI and that button are unresponsive. :(

So what I did was to move the check on Explorer.exe into that main loop. Now every pass through the loop we run GUIGetMsg so the GUI and controls remain active - but we only check Explorer.exe if the flag is set to True and that only happens when we press the "Lock" button. As the GUI and controls are responsive, we can still press the "Unlock" button which, if we enter the correct password, clears the flag (resets it to False) and prevents the check on Explorer.exe from running. :sweating:

Does that make more sense? Flags can play an important part in a script and it is important to understand how they work - so do tell me if you still have problems. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

oouh! Understood, you clear the flag only when we insert the right login information but exiting the loop we don't have any more problems, and for the explorer thing you check if it's false (as it starts) and then you change the flag statement to true just to make us use the "unlock" button, am i right?

:)

Link to comment
Share on other sites

  • Moderators

Doctorkirk,

for the explorer thing you check

Yes, but not as you describe it. ;)

- At first the flag is set to False (because we declare it that way) which means that we do not check Explorer.exe as the surrounding If statement is not satisifed.

- If we press the Lock button we set the flag to True - now as we pass through the loop the If statement is satisfied and we check Explorer.exe.

- A successful "Unlock" clears the flag (resets it to False) and so we no longer check as the If condition is again unsatisfied. Clear now? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Close Enough! ahah Now it's clear thanks! While we are talking M23, I have to ask you a little more question, just to not open a new topic for a "silly" thing, i was working on a program some years ago, when i want to put the thing written on a ".ini" or "txt" file into a list (GuiCtrlCreateList), but when I tried to do it the list was only reading the ".txt" all in a line :unsure: or (with the ".ini" one) only the selected line... how the hell i could make that damn list read ALL the line of my txt or ini file? xD (Tried to take a look at your "GuiListViewEx" but i think it's not fine for me...) Please do not hate me : ahaha

Link to comment
Share on other sites

  • Moderators

Doctorkirk,

Why on earth would I hate you? :huh:

If you have a file like this:

Line 1
Line 2
Line 3

then this script shows you how to load it into a list:

#include <GUIConstantsEx.au3>
#include <File.au3>

; Read the file into an array - each element is a line
Global $aLines
_FileReadToArray("Test.txt", $aLines)

; Create the GUI
$hGUI = GUICreate("Test", 500, 500)

$cList = GUICtrlCreateList("", 10, 10, 300, 300)

; Load the file lines into the list
For $i = 1 To $aLines[0]
    GUICtrlSetData($cList, $aLines[$i])
Next

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Any questions? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Everything Clear! Thanks Again!

I Think that would be more difficult trying to delete the line in the list and the same line in the file, right? :unsure:

I Found the way to delete it from the list, but obviously not from the file :sweating:

Edited by Doctorkirk
Link to comment
Share on other sites

  • Moderators

Doctorkirk,

How are you deleting the line from the list? If you want to delete it from the array as well, it is often easier to do that first and then reload the contents of the list from the amended array. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

How are you deleting the line from the list?

I'm deleting it from the list this way

Case $b2
_GUICtrlListBox_DeleteString($lista, _GUICtrlListBox_GetCaretIndex($lista))

I have problem with the Array, because they are sorted by the name in the list, but non-sorted in the ".txt" file, can't find the way to delete it as well... ?_?

Edited by Doctorkirk
Link to comment
Share on other sites

  • Moderators

Doctorkirk,

The listbox sorts entries by default - you need to change the style values to remove the $LBS_SORT style if you do not want it sorted. ;)

As I mentioned above, I would use delete the entry form the array first and then reload the list - that way you always have the 2 in sync which makes saving the list very easy. So I would suggest GUICtrlRead to get the selected value, _ArraySearch to find it in the array and _ArrayDelete to remove it. Finally, reload the list as we did before - but if you put this into a function you can call it from different places in the script when needed - something like this: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>

; Read the file into an array - each element is a line
Global $aLines
_FileReadToArray("Test.txt", $aLines)

; Create the GUI
$hGUI = GUICreate("Test", 500, 500)

$cList = GUICtrlCreateList("", 10, 10, 300, 300, BitOR($WS_BORDER, $WS_VSCROLL)) ; Here we have removed the sort style

; Fill the list
_Load_List()

$cButton_Delete = GUICtrlCreateButton("Delete", 10, 400, 80, 30)
$cButton_Save = GUICtrlCreateButton("Save", 110, 400, 80, 30)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton_Save
            ; Save the array - which will match the current list
            _FileWriteFromArray("Test.txt", $aLines, 1)
        Case $cButton_Delete
            ; Read the selected value
            $sText = GUICtrlRead($cList)
            ; Find it in the array
            $iIndex = _ArraySearch($aLines, $sText)
            If $iIndex <> -1 Then
                ; Delete it from the array
                _ArrayDelete($aLines, $iIndex)
                ; Reload the list
                _Load_List()
            EndIf
    EndSwitch
WEnd

Func _Load_List()
    ; Clear the list - add a Sleep after this line to see it happening
    GUICtrlSetData($cList, "|")
    ; Reset the count element in case something has been deleted
    $aLines[0] = UBound($aLines) - 1
    ; Load the array elements into the list
    For $i = 1 To $aLines[0]
        GUICtrlSetData($cList, $aLines[$i])
    Next
EndFunc

When you have deleted a couple of lines, try pressing "Save" and rerunning the script - you will see that the modified list is saved as well as you write the current array to the file. If you just exit the script, the file on disk is unchanged and you get the original list when you rerun the script. :)

Questions? ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...