惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
Cloudbric
Cloudbric
The Register - Security
The Register - Security
小众软件
小众软件
PCI Perspectives
PCI Perspectives
G
Google Developers Blog
AI
AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
TaoSecurity Blog
TaoSecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
F
Full Disclosure
N
Netflix TechBlog - Medium
博客园_首页
Last Week in AI
Last Week in AI
A
Arctic Wolf
B
Blog RSS Feed
J
Java Code Geeks
C
Cybersecurity and Infrastructure Security Agency CISA
I
InfoQ
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
有赞技术团队
有赞技术团队
S
Schneier on Security
L
Lohrmann on Cybersecurity
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Security Latest
Security Latest
Vercel News
Vercel News
博客园 - 司徒正美
Webroot Blog
Webroot Blog
Hacker News: Ask HN
Hacker News: Ask HN
A
About on SuperTechFans

博客园 - arvin2012

VC++常用方法函数集合, EVC++4.0 长用数据类型转换,FFT算法 指向函数的指针数组到底该怎么用 内存耗尽怎么办? 位运算符的一些简单应用 异步Socket通信总结 贵在坚持 vc++操作word MFC中的ClassWizard的使用 追MM与设计模式的有趣见解 动软.Net代码生成器 发布最新2.12版 vc中常用的方法 c++ file and directory 删除,移动,目录浏览对话框,找某目录下的所有文件(包括子目录) 简单好用的读写ini文件的类 Windows API 技巧集 vc 使用总结 VC 常见的108个问题 VC中动态生成控件 什么是COM,如何使用COM
vc中读写注册表
arvin2012 · 2008-07-28 · via 博客园 - arvin2012

 Win 95及NT的注册数据库(Registry) 是系统中非常重要的组成部分。在Win32 API中有一组Reg函数来处理这些问题。其一般的读写过程如下:
 
    1、使用RegOpenKeyEx或RegCreateKeyEx函数打开或创建一个键; 
    2、如果上一步成功,使用RegQueryValueEx读取子键的值,使用RegSetValueEx设置子键值,使用RegEnumKey获得所有子键,使用RegDeleteKey删除一个键; 
    3、完成操作后使用RegCloseKey关闭键。 
    下面这段程序打开HKEY_CURRENT_USER\Software\Zeal SoftStudio\AskPro FTP\LastTime键,然后读取WOL子键的值。 

    HKEY hkey; 
    char sz[256]; 
    DWORD dwtype, sl = 256; 
     
    RegOpenKeyEx(HKEY_CURRENT_USER, 
    "Software\\Zeal SoftStudio\\AskPro FTP\\LastTime", 
    NULL, KEY_ALL_ACCESS, &hkey); 
    RegQueryValueEx(hkey, "WOL", NULL, &dwtype, (LPBYTE)sz, &sl); 
    RegCloseKey(hkey); 
    MFC程序可以使用CRegKey类读写注册表。VB中调用API的办法可以参考QA000226 "如何访问Windows系统注册表"。

    打开注册键
    LONG RegOpenKeyEx( HKEY hKey,  // handle to open key 

    LPCTSTR lpSubKey,              // address of name of subkey to open 
    DWORD ulOptions,               // reserved =0
    REGSAM samDesired,             // security access mask 
    PHKEY phkResult                // address of handle to open key 
    );

    例:
    HKEY hd;
    hd=HKEY_LOCAL_MACHINE;
    char* Regkeyname="SoftWare\\Xy123\\Poker\\";
    LONG a=RegOpenKeyEx(hd,Regkeyname,0,KEY_READ, &hd);   //成功返回ERROR_SUCCESS,否则返回错误代码

    关闭注册键
    LONG RegCloseKey( HKEY hKey // handle to key to close );
例:
     RegCloseKey(HKEY_LOCAL_MACHINE);
OR:  RegCloseKey(hd); 
建立注册键
LONG RegCreateKeyEx( HKEY hKey, // handle to an open key 
      LPCTSTR lpSubKey, // address of subkey name 
      DWORD Reserved, // reserved =0
      LPTSTR lpClass, // address of class string 
      DWORD dwOptions, // special options flag 
      REGSAM samDesired, // desired security access 

      LPSECURITY_ATTRIBUTES lpSecurityAttributes, // address of key security         structure 
      PHKEY phkResult, // address of buffer for opened handle 
      LPDWORD lpdwDisposition // address of disposition value buffer );
例:
   char *sclass="";  //类名指定为空
   DWORD nbf=0;    //接受返回值,指明是建立新键还是打开已有的键.(经试验总是返回REG_OPENED_EXISTING_KEY.
   LONG II=RegCreateKeyEx(hd,Regkeyname,0,sclass,REG_OPTION_NON_VOLATILE,
                KEY_READ|KEY_WRITE,NULL,&hd,&nbf);

//REG_OPTION_NON_VOLATILE 指明键永久保留.安全结构指明NULL,自动获得一默认值
//成功返回ERROR_SUCCESS,否则返回错误代码 
枚举键值
LONG RegEnumValue( HKEY hKey, // handle to key to query 
      DWORD dwIndex, // index of value to query 
      LPTSTR lpValueName, // address of buffer for value string 
      LPDWORD lpcbValueName, // address for size of value buffer 
      LPDWORD lpReserved, // reserved =NULL
      LPDWORD lpType, // address of buffer for type code 

      LPBYTE lpData, // address of buffer for value data 
      LPDWORD lpcbData // address for size of data buffer);
例:
   DWORD dinx=0;
   char valuename[70];  //分配数值名称缓冲区
   strcpy(valuename,"DeskPattern");  //随便指定哪个键值名
   DWORD nsize=69;  //数值名称缓冲区大小
   DWORD k=REG_SZ;  //指明数据类型
   unsigned char vari[70]; //分配数值缓冲区
   DWORD ncbvari=69; //数值缓冲区大小
   dinx=0; //从0开始

   while((II=RegEnumValue(hd,dinx,valuename,&nsize,NULL,&k,vari,&ncbvari)) 
          != ERROR_NO_MORE_ITEMS)
   {
       dinx++;//索引 +1,准备取下一个值
       nsize=69; //恢复原来大小
       ncbvari=69;
   }
成功后返回值0,各变量返回后设置如下:
valuename=数值名称,以0结尾;如 : DeskColor
nsize=数值名称长度, 9
k=REG_SZ  DeskColor 的类型为 REG_SZ
vari=键值,32768 DeskColor="32768",
ncbvari=键值长度 REG_SZ包括结尾0,=6, 
读取键值
LONG RegQueryValueEx( HKEY hKey, // handle to key to query 

       LPTSTR lpValueName, // address of name of value to query 
       LPDWORD lpReserved, // reserved 
       LPDWORD lpType, // address of buffer for value type 
       LPBYTE lpData, // address of data buffer 
       LPDWORD lpcbData // address of data buffer size );
例:
   RegQueryValueEx(hd,valuename,NULL,&k,vari,&ncbvari);
变量定义及成功后各变量设置值同RegEnumValueEx 
写键值
LONG RegSetValueEx( HKEY hKey, // handle to key to set value for 
       LPCTSTR lpValueName, // name of the value to set 

       DWORD Reserved, // reserved 
       DWORD dwType, // flag for value type 
       CONST BYTE *lpData, // address of value data 
       DWORD cbData // size of value data );
例:
   strcpy(valuename,"Hello");
   unsigned char vari[10];
   DWORD k=REG_SZ;
   strcpy((char*)vari,"1234567")
   RegSetValueEx(hd,valuename,0,k,vari,7);
成功后在Poker下增加一个键值 Hello : REG_SZ : 1234567
写整型变量:
int hi=8;
RegSetValueEx(pj,valuename,0,REG_BINARY,(unsigned char*)&hi,sizeof(int));

成功后在Poker下增加一个键值 Hello2 : REG_BINARY :08 00 00 00

void AddEventSource()
{
    HKEY hk; 
    DWORD dwData; 
    UCHAR szBuf[80]; 

    // Add your source name as a subkey under the Application 
    // key in the EventLog registry key. 

    if (RegCreateKey(HKEY_LOCAL_MACHINE, 
            "SYSTEM\\CurrentControlSet\\Services\ 
            \\EventLog\\Application\\SamplApp", &hk)) 
        ErrorExit("Could not create the registry key."); 

    // Set the name of the message file. 

    strcpy(szBuf, "%SystemRoot%\\System\\SamplApp.dll"); 

    // Add the name to the EventMessageFile subkey. 

    if (RegSetValueEx(hk,             // subkey handle 
            "EventMessageFile",       // value name 
            0,                        // must be zero 
            REG_EXPAND_SZ,            // value type 
            (LPBYTE) szBuf,           // pointer to value data 
            strlen(szBuf) + 1))       // length of value data 

        ErrorExit("Could not set the event message file."); 

    // Set the supported event types in the TypesSupported subkey. 

    dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | 
        EVENTLOG_INFORMATION_TYPE; 

    if (RegSetValueEx(hk,      // subkey handle 
            "TypesSupported",  // value name 
            0,                 // must be zero 
            REG_DWORD,         // value type 
            (LPBYTE) &dwData,  // pointer to value data 

            sizeof(DWORD)))    // length of value data 
        ErrorExit("Could not set the supported types."); 

    RegCloseKey(hk); 

以下代码把注册表自启动shell的键值改写为C:\DK1\ATM\HARP\ExAtmShell.exe:

         HKEY hkey;
LONG res; 
DWORD datatype=REG_SZ; 
unsigned char szvalue[_MAX_PATH];
strcpy((char*)szvalue,"C:\\DK1\\ATM\\HARP\\ExAtmShell.exe");

res =::RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\", 0, 
KEY_WRITE|KEY_READ, &hkey); 

if(res!=ERROR_SUCCESS)
{
AfxMessageBox("aaa");
return;
}
res = ::RegSetValueEx(hkey, "Shell", 0, datatype, szvalue, strlen(LPCSTR(szvalue))); 

RegCloseKey(hkey);
if(res==ERROR_SUCCESS)
::AfxMessageBox("你已经成功地将注册表自启动shell的键值设置为C:\\DK1\\ATM\\HARP\\ExAtmShell.exe");
else
::AfxMessageBox("设定失败:目标位置不存在这样的键!");