我正在尝试制作一个在windows环境中持久化的python脚本。我正在使用PyInstaller创建一个exe。我设法让这个脚本只在WindowsXP环境下运行,而不在任何其他版本的Windows上运行。我可以将exe移到%temp%文件夹,但是它不会写入注册表中的“Software\Microsoft\Windows\CurrentVersion\Run”。我希望你们能给我你们对代码的看法。有没有更有效的方法来写注册表?在
import sys, base64, os, socket, subprocess
from _winreg import *
def autorun(tempdir, fileName, run):
# Copy executable to %TEMP%:
os.system('copy %s %s'%(fileName, tempdir))
# Queries Windows registry for the autorun key value
# Stores the key values in runkey array
key = OpenKey(HKEY_LOCAL_MACHINE, run)
runkey =[]
try:
i = 0
while True:
subkey = EnumValue(key, i)
runkey.append(subkey[0])
i += 1
except WindowsError:
pass
# If the autorun key "helloworld" isn't set this will set the key:
if 'helloworld' not in runkey:
try:
key= OpenKey(HKEY_LOCAL_MACHINE, run,0,KEY_ALL_ACCESS)
SetValueEx(key ,'helloworld',0,REG_SZ,r"%TEMP%\hello.exe")
key.Close()
except WindowsError:
pass
def hello():
print "hello world"
def main():
tempdir = '%TEMP%'
fileName = sys.argv[0]
run = "Software\Microsoft\Windows\CurrentVersion\Run"
autorun(tempdir, fileName, run)
hello()
if __name__ == "__main__":
main()
写入注册表的问题是,您可能会发现您需要以管理员身份运行应用程序。这可能就是为什么您在不同的环境中有不同的性能。在
如果没有严格绑定到注册表,可以尝试使用pickle在会话之间保存信息。这样做的好处是跨平台。在
我解决了这个问题,将HKEY U LOCAL U机器值替换为HKEY U CURRENT U USER。重新启动后,.exe文件未运行的问题仍然存在。在
相关问题 更多 >
编程相关推荐