Home > front end >  Is there a way to lock and unlock an ___.exe file using python?
Is there a way to lock and unlock an ___.exe file using python?

Time:06-06

For example:- first the program should lock abc.exe file and the file should only be able to unlock or run when user enters the correct password for it.
This should happen using Python program, since this is just a submodule of a main program.

This program do locks the program but unlocking doesn't work. ''' import locket from tkinter import * from tkinter import ttk

win = Tk()
win.title('SELECT THE PROGRAM')

path = "C:\\users\\admin\\abc\\abc.exe"
lock = locket.lock_file(path)
try:
    lock.acquire()

finally:
    lock.release()
#locket._unlock_file()

def close_win():
    win.destroy()


def disable_event():
    pass


btn = ttk.Button(win, text="Click here to Close", command=close_win)
btn.pack()
win.protocol("WM_DELETE_WINDOW", disable_event)

# Create a fullscreen window
win.attributes('-fullscreen', True)
win.mainloop()
'''

CodePudding user response:

I played with your idea and "unlocking doesn't work" because the file is empty. When you try to lock the file the original file get 0 bytes and is not "unlocking" because windows can not run an empty executable file.

Try this for your self: Create an empty text file with any name and replace the extension of the file from txt to exe and try to run it. You will get same error message from windows.

In my case is look like this:This screen shot if from Winodows 10

Now an work around for this is to move your original file some where in PC and rename it to something else with a different extension just to hide it from users and instead of the original file place a fake one with same name. After you get the user condition of using the file you can move it back. Roaming is most common directory for software's to save some extra files and is locket-ed under "C:\Users\USERNAME\AppData\Roaming"

I will not provide you the source code for this, instead I will let you to think on your own logic. As an hint you will need to search for os library for moving the files under a different name and file handeling.

  • Related