判断某键值是否存在[注册表操作]

#include <stdio.h>
#include <windows.h>
#define MAX 128
int main(int argc, char* argv[]) 
{

    // 根键、子键名称、和到子键的句柄
    HKEY hRoot = HKEY_LOCAL_MACHINE;
    char *szSubKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
    HKEY hKey;
    // 打开指定子键
    DWORD dwDisposition = REG_OPENED_EXISTING_KEY;    // 如果不存在不创建
    LONG lRet = ::RegCreateKeyEx(hRoot, szSubKey, 0, NULL, 
            REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
    if(lRet != ERROR_SUCCESS)
    {
        MessageBox(NULL,"RegCreateKeyEx函数执行失败!","提示",MB_OK);
        return -1;
    }
    unsigned long * lpType=NULL;
    unsigned char * lpData=NULL;
    LPDWORD lpcbDate=NULL;
    LONG lResult  = ::RegQueryValueEx(hKey,"WindowsSelfRun",0,lpType, lpData,lpcbDate);
    if(lResult==ERROR_SUCCESS)
    {
        MessageBox(NULL,"WindowsSelfRun键值已经存在!","提示",MB_OK);
    }
    else
    {
        MessageBox(NULL,"WindowsSelfRun键值不存在!将要写入...","提示",MB_OK);
        //下面可以添加写入键值内容及类型的代码
    }
    // 关闭子键句柄
    ::RegCloseKey(hKey);
    return 0;
}
/****************************************************************
*需要注意的是:32bit的程序写入注册表的位置与64bit程序写入注册表的
*位置是不相同的。32bit的程序比64bit程序多了一个Wow6432Node。
*****************************************************************/

导出不同位置的Run项可以看到:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run]
"USB3MON"="\"C:\\Program Files (x86)\\Intel\\Intel(R) USB 3.0 eXtensible Host Controller Driver\\Application\\iusb3mon.exe\""
"Dolby Advanced Audio v2"="\"C:\\Program Files (x86)\\Dolby Advanced Audio v2\\pcee4.exe\" -autostart"
"331BigDog"="C:\\Program Files (x86)\\USB Camera\\VM331STI.EXE"
"avgnt"="\"F:\\Avira\\AntiVir Desktop\\avgnt.exe\" /min"
"WindowsSelfRun"="G:\\vc2012\\注册表重启\\Debug\\注册表重启.exe
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"IAStorIcon"="\"C:\\Program Files\\Intel\\Intel(R) Rapid Storage Technology\\IAStorIconLaunch.exe\" \"C:\\Program Files\\Intel\\Intel(R) Rapid Storage Technology\\IAStorIcon.exe\" 60"
"IgfxTray"="C:\\Windows\\system32\\igfxtray.exe"
"HotKeysCmds"="C:\\Windows\\system32\\hkcmd.exe"
"Persistence"="C:\\Windows\\system32\\igfxpers.exe"
"Broadcom Wireless Manager UI"="C:\\Program Files\\Broadcom\\Broadcom 802.11 Network Adapter\\WLTRAY.exe"
"ETDCtrl"=hex(2):25,00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,46,00,69,00,\
  6c,00,65,00,73,00,25,00,5c,00,45,00,6c,00,61,00,6e,00,74,00,65,00,63,00,68,\
  00,5c,00,45,00,54,00,44,00,43,00,74,00,72,00,6c,00,2e,00,65,00,78,00,65,00,\
  00,00
"WindowsSelfRun"="G:\\vc2012\\注册表重启\\x64\\Release\\注册表重启.exe"

 

posted @ 2013-04-14 10:59  乾卦  阅读(2439)  评论(0编辑  收藏  举报