注册表良好做法(C)

时间:2012-08-06 16:02:25

标签: c winapi registry

如果我想用RegGetValue读取注册表中的变量字符串,那么这样做是不错的做法?

DWORD dwByteCount=20,dwType;
char *value = (char *)malloc(dwByteCount);
HKEY hKey;
LONG lResult;

while(TRUE){
  lResult = RegGetValue(hKey,
              "somesubkey",
              "somevalue",
              RRF_RT_REG_SZ,
              &dwType,
              value,
              &dwBytesAllocated);
  if(lResult == ERROR_SUCCESS){
    break;
  }
  else if(lResult == ERROR_MORE_DATA){
    free(value);
    dwBytesAllocated+=20;
    value = (char *)malloc(dwBytesAllocated);
  }
  else {
    free(value);
    return NULL;
  }

}

0 个答案:

没有答案