[英]How to add Python script to registry using Ctypes and kernel32.dll
I'm trying to add my program to registry and this is my code...我正在尝试将我的程序添加到注册表中,这是我的代码...
def regc():
reg = windll.kernel32
print(reg)
hkey = 'HKEY_CURRENT_USER'
lsubkey = 'Software\Microsoft\Windows\CurrentVersion\Run'
reserved = 0
flag = 'REG_OPTION_BACKUP_RESTORE'
samdesired = 'KEY_ALL_ACCESS'
ipsec = None
handle = reg.RegCreateKeyExA(hkey, lsubkey, reserved, flag, samdesired, ipsec, None)
Its not giving me any errors but it still isn't creating a new key in registry.它没有给我任何错误,但它仍然没有在注册表中创建一个新键。 What am I doing wrong?
我究竟做错了什么?
To use ctypes
correctly, define .argtypes
and .restype
to do error checking of your parameters.要正确使用
ctypes
,请定义.argtypes
和.restype
来对参数进行错误检查。 Many of the types used are wrong.许多使用的类型是错误的。
hkey
, flag
, and samdesired
are not strings, for example. hkey
, hkey
、 flag
和samdesired
不是字符串。 The return value is not a handle, but a status.返回值不是句柄,而是状态。 The return value is an output parameter (
pkhResult
in the docs).返回值是一个输出参数(
pkhResult
中的pkhResult
)。 You must read the documentation and examine the header files of all the variable definitions carefully.您必须阅读文档并仔细检查所有变量定义的头文件。
Also, in Python 3 strings are Unicode, so use the W
form of Windows APIs to accept Unicode strings.此外,在 Python 3 中字符串是 Unicode,因此使用
W
形式的 Windows API 来接受 Unicode 字符串。 Use raw strings ( r'...'
) for the subkey since it contains backslashes that could be interpreted as escape codes.对子项使用原始字符串 (
r'...'
),因为它包含可以解释为转义码的反斜杠。
Here's a working example:这是一个工作示例:
from ctypes import *
from ctypes import wintypes as w
# Values found from reading RegCreateKeyExW documentation,
# using Go To Definition on the types in Visual Studio,
# and printing constants in a C program, e.g. printf("%lx\n",KEY_ALL_ACCESS);
HKEY = c_void_p
PHKEY = POINTER(HKEY)
REGSAM = w.DWORD
LPSECURITY_ATTRIBUTES = c_void_p
LSTATUS = w.LONG
# Disposition values
REG_CREATED_NEW_KEY = 0x00000001
REG_OPENED_EXISTING_KEY = 0x00000002
ERROR_SUCCESS = 0
HKEY_CURRENT_USER = c_void_p(0x80000001)
REG_OPTION_NON_VOLATILE = 0
KEY_ALL_ACCESS = 0x000F003F
dll = WinDLL('kernel32')
dll.RegCreateKeyExW.argtypes = HKEY,w.LPCWSTR,w.DWORD,w.LPWSTR,w.DWORD,REGSAM,LPSECURITY_ATTRIBUTES,PHKEY,w.LPDWORD
dll.RegCreateKeyExW.restype = LSTATUS
hkey = HKEY_CURRENT_USER
lsubkey = r'Software\Microsoft\Windows\CurrentVersion\Run'
options = REG_OPTION_NON_VOLATILE
samdesired = KEY_ALL_ACCESS
# Storage for output parameters...pass by reference.
handle = HKEY()
disp = w.DWORD()
status = dll.RegCreateKeyExW(HKEY_CURRENT_USER, lsubkey, 0, None, options, samdesired, None, byref(handle), byref(disp))
if status == ERROR_SUCCESS:
print(f'{disp=} {handle=}')
Output:输出:
disp=c_ulong(2) handle=c_void_p(3460)
The disposition value of 2 indicates the key already exists ( REG_OPENED_EXISTING_KEY
).处置值 2 表示密钥已存在(
REG_OPENED_EXISTING_KEY
)。
You could also install pywin32 and use win32api.RegCreateKey
or win32api.RegCreateKeyEx
where all the work is already done for you.您还可以安装pywin32并使用
win32api.RegCreateKey
或win32api.RegCreateKeyEx
,其中所有工作都已为您完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.