检查密钥是否已存在(RegOpenKey)

Check if the key already exists (RegOpenKey)

本文关键字:RegOpenKey 存在 密钥 是否 检查      更新时间:2023-10-16

我这样做了:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    HKEY CH;
    if(RegCreateKey(HKEY_LOCAL_MACHINE, L"Software\Microsoft\Windows\CurrentVersion\Run",&CH) != 0)
    {
        printf("Erro - RegCreateKeyn");
        system("PAUSE");
        return -1;
   }
    if(RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\Microsoft\Windows\CurrentVersion\Run",&CH) != 0) // Abre a CH "Minha CH"
    {
        printf("Erro - RegOpenKeyn");
        system("PAUSE");
        return -1;
    }
    if(RegSetValueEx(CH,L"PROC",0,REG_SZ,(LPBYTE) L"C:\pasta1\pasta2\txt.txt",200) != 0)
        printf("Erro - RegSetValuen");
    RegCloseKey(CH);
    printf("nsucesso !n");
    system("PAUSE");
    return 0;
     system("PAUSE");
}

现在我想这样做:

 if(key already exist) {
            //don't make nothing 
} else
     Create key
      ... 

我需要做什么功能? 因为如果没有,我将创建一个已经存在的密钥。如果我能避免,那就太好了。

使用 RegCreateKeyEx .如果密钥已存在,它会打开密钥,如果不存在,它会创建密钥。 lpdwDisposition参数告诉您这两种效果中的哪一种实际发生。 例如:

DWORD disposition = 0;
RegCreateKeyEx(..., &disposition);
if (disposition == REG_CREATED_NEW_KEY) {
    /* new key was created */
} else {
    /* existing key was opened */
}