使用python获取window注册表值的方法
提供regfullpath的方法,可以自行封装个regpath的函数
import logging
import pywintypes
import win32api
import win32con
def getValue(strKeyPath, intAccessMask, strValueName, default=None): if strKeyPath[0] == '\\':
strKeyPath = strKeyPath[1:]
strRoot, strKey = strKeyPath.split('\\', 1)
if strRoot not in g_dicKeyRoot:
raise KeyError(strRoot)
intRoot = g_dicKeyRoot[strRoot]
if intAccessMask == None:
intAccessMask = win32con.KEY_QUERY_VALUE | win32con.KEY_ENUMERATE_SUB_KEYS | win32con.KEY_NOTIFY
h = win32api.RegOpenKeyEx(intRoot, strKey, 0, intAccessMask)
try:
value, vType = win32api.RegQueryValueEx(h, strValueName)
except pywintypes.error as e:
if str(e.args[0]) == '2': # value name doesn't exist
if strValueName == '':
return None, win32con.REG_NONE
if default != None:
return default, None
raise
return value, vType def actGetRegKey(strRegKeyPath, strKeyName):
'''
@param strRegKeyPath: Specify registry key path. Ex. r"HKEY_LOCAL_MACHINE\SOFTWARE\test123"
@param strKeyName: Specify registry key name. Ex. "VirusCount"
@return: strKeyValue[0] -> key value
@example: actGetRegKey(r"HKEY_LOCAL_MACHINE\SOFTWARE\test123","VirusCount")
'''
try:
intAccessMask = win32con.KEY_QUERY_VALUE | win32con.KEY_ENUMERATE_SUB_KEYS | win32con.KEY_NOTIFY
strKeyValue = getValue(strRegKeyPath, intAccessMask, strKeyName)
logging.debug(
'GetRegKey success, path %s, keyName %s , keyValue %s' % (strRegKeyPath, strKeyName, strKeyValue))
return strKeyValue[0]
except pywintypes.error as e:
if 'Access is denied' in str(e):
logging.info(
'Reading regkey %s %s access is denied, change way to KEY_READ' % (strRegKeyPath, strKeyName))
strKeyValue = getValue(strRegKeyPath, win32con.KEY_READ, strKeyName)
logging.debug(
'GetRegKey success, path %s, keyName %s , keyValue %s' % (strRegKeyPath, strKeyName, strKeyValue))
return strKeyValue[0] def GetRegistryValuebyFullPath(fullregpath, regkeyname):
try:
logging.info("full registry path: %s" % fullregpath)
registryValue = actGetRegKey(fullregpath, regkeyname)
logging.info("registryValue: %s" % registryValue)
return registryValue
except pywintypes.error as e:
logging.error('GetRegistryValue Error! %s' % str(e))
使用python获取window注册表值的方法的更多相关文章
- python获取文件扩展名的方法(转)
主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧.具体实现方法如下: 1 2 3 4 import os.path def file_extension(path ...
- python获取文件扩展名的方法
主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧 import os.path def file_extension(path): ] print file_ex ...
- python获取本地ip地址的方法
#_*_coding:utf8_*_ #以下两种方法可以在ubuntu下或者windows下获得本地的IP地址 import socket # 方法一 localIP = socket.gethost ...
- Win10打开照片提示“无效的注册表值”解决方法
1.点开开始菜单,右键单击,选择“以管理员运行”[键盘win键+R]输入PowerShell. 2.输入Get-AppxPackage *photo* | Remove-AppxPackage后回车. ...
- Python入门之获取当前所在目录的方法详解
#本文给大家讲解的是使用python获取当前所在目录的方法以及相关示例,非常的清晰简单,有需要的小伙伴可以参考下 sys.path 模块搜索路径的字符串列表.由环境变量PYTHONPATH初始化得到. ...
- python 获取mac地址zz
通过python获取当前mac地址的方法如下:(1)通用方法,借助uuid模块def get_mac_address(): import uuid node = uuid.getnode() ...
- SQL读取注册表值
最近写一个自动检查SQL Serve安全配置的检查脚本,需要查询注册表,下面是使用SQL查询注册表值的方法. ) ) ) ) --For Named instance --SET @Instance ...
- python获取字母在字母表对应位置的几种方法及性能对比较
python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...
- python动态获取对象的属性和方法 (转载)
首先通过一个例子来看一下本文中可能用到的对象和相关概念. #coding:utf-8 import sys def foo():pass class Cat(object): def __init__ ...
随机推荐
- Java 注解及其底层原理
目录 什么是注解? 注解的分类 Java自带的标准注解 元注解 @Retention @Documented @Target @Inherited @Repeatable 自定义注解 自定义注解的读取 ...
- 痞子衡嵌入式:聊聊i.MXRT1170双核下不同GPIO组的访问以及中断设计
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT1170双核下不同GPIO组的访问以及中断设计. 在双核 i.MXRT1170 下设计应用程序,有一个比较重要的考虑点就是外 ...
- Luogu P1903 [国家集训队]数颜色 / 维护队列 (带修莫队)
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...
- java中为什么只存在值传递(以传入自定义引用类型为例)
java中只有值传递 为什么这么说?两个例子: public class Student { int sage = 20; String sname = "云胡不归"; publi ...
- java-引用数组、继承、super关键字
1.引用类型数组: 1) Cell[] cells = new Cell[4]; cells[0] = new Cell(2,5); cells[1] = new Cell(2,6); cel ...
- poi生成表格自动合并单元格
直接复制这个工具类即可使用: /** * 合并单元格 * @author tongyao * @param sheet sheet页 * @param titleColumn 标题占用行 * @par ...
- 【PostgreSQL】PostgreSQL 15移除了Stats Collector
试用即将发行的PostgreSQL 15的人会发现少了一个后台进程: postgres 1710 1 0 04:03 ? 00:00:00 /usr/pgsql-15/bin/postmaster ...
- Android序列化的几种实现方式
一.Serializable序列化 Serializable是java提供的一种序列化方式,其使用方式非常简单,只需要实现Serializable接口就可以实现序列化. public interfac ...
- flutter系列之:Material主题的基础-MaterialApp
简介 为了简化大家的使用,虽然flutter推荐所有的widget都有自己来进行搭建,但是在大框架上面,flutter提供了Material和Cupertino两种主题风格的Widgets集合,大家可 ...
- 华南理工大学 Python第3章课后小测-1
1.(单选)给出如下代码 s = 'Hello scut' print(s[::-1]) 上述代码的输出结果是(本题分数:4)A) HelloB) Hello scutC) olleH tucsD) ...