C、Windows API在注册表项后创建文件夹结构

标签 c windows api directory registry

所以我尝试在注册表项后创建一个文件夹结构。这就是我到目前为止所做的

#include "stdafx.h"
#include "windows.h"
#define MAX_KEY_LENGTH 200
#define MAX_VALUE_NAME 16383
DWORD retCode;
void RecursiveQueryKey(HKEY hKey)
{
    HKEY nextKey;
    WCHAR    achKey[MAX_KEY_LENGTH];
    DWORD    cbName;             
    DWORD retCode = NULL;
    DWORD i=0;

    while(retCode !=ERROR_NO_MORE_ITEMS)
    {
        cbName = MAX_KEY_LENGTH;
        retCode = RegEnumKeyEx(hKey, i,achKey,&cbName,NULL,NULL,NULL,NULL);
        if (retCode == ERROR_SUCCESS)
        {
            wprintf(L"(%d) %s\n", i+1, achKey);

            WCHAR path[MAX_KEY_LENGTH];
            wchar_t *wcsncat(wchar_t *path, const wchar_t *achKey, size_t n);


            if(CreateDirectoryEx(TEXT("D:\\csso\\"),achKey, NULL) != 0){
                wprintf(L"Directory created in D:\\csso\\%s\n", achKey); 
            } else {
                printf("Directory failed with the error:");
            }
            wprintf(L"%d\n", GetLastError());
            if(RegOpenKeyEx(hKey, achKey,  0, KEY_READ | KEY_WOW64_64KEY,  &nextKey) == ERROR_SUCCESS)
            {
                RecursiveQueryKey(nextKey);
            }
        }
        i++;    
    }

}

int _tmain(int argc, _TCHAR* argv[])
{

         HKEY hKey;
     if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,  TEXT("SOFTWARE\\Apple Inc."),  0, KEY_READ | KEY_WOW64_64KEY,  &hKey) == ERROR_SUCCESS)
     {
         printf("RegOpenKeyEx() is OK!\n");
         RecursiveQueryKey(hKey);   
     }
     else
         printf("RegOpenKeyEx() failed!\n");

     RegCloseKey(hKey);

}

我对此还很陌生,但是当我第一次运行该程序时,它说目录已全部创建,但它们并未创建,当我再次运行它时,我收到错误183(已经存在) )。

我真的不确定这里出了什么问题。

最佳答案

好吧,我明白了,问题来自于我无法创建路径,显然这很简单,这是我的最终代码,也许它会帮助别人。

#include "stdafx.h"
#include "windows.h"
#include "tchar.h"
#include <iostream>

#define KEY_LENGHT 255
#define MAX_VALUE_NAME 16000


#define DEFAULT_KEY L"Apple Inc."
WCHAR path[1024] = L"D:\\csso\\" DEFAULT_KEY;


void Query(HKEY key, WCHAR *path) {
    HKEY  next;
    WCHAR name[KEY_LENGHT];
    DWORD lpcname;
    DWORD returncode = NULL;
    DWORD i = 0;
    WCHAR xpath[1024];

    while(returncode != ERROR_NO_MORE_ITEMS) {
        lpcname = KEY_LENGHT;
        returncode =
            RegEnumKeyEx(key, i, name, &lpcname, NULL, NULL, NULL, NULL);

        if (returncode ==ERROR_SUCCESS) {

            wcscpy(xpath, path);
            wcscat(xpath, L"\\");
            wcscat(xpath, name);


            if(CreateDirectory(xpath, NULL)) {
                wprintf(L"Creat Folder %s\n", xpath);
            } else {
                printf("Folder nu a putut fi creat! \n");
                wprintf(L"%d\n", GetLastError());
            }


            DWORD verif = 
                RegOpenKeyEx(key, name, 0, KEY_READ | KEY_WOW64_64KEY, &next);
            if(verif == ERROR_SUCCESS) {
                Query(next, xpath);
            }
            i++;
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    HKEY key;
    DWORD verif = 
        RegOpenKeyEx(HKEY_LOCAL_MACHINE,  TEXT("SOFTWARE\\" DEFAULT_KEY),  0, KEY_READ | KEY_WOW64_64KEY,  &key);

    if(verif == ERROR_SUCCESS) {
        printf("RegOpenKeyEx SUCCESS! \n");

        if(!CreateDirectory(_T("D:\\csso\\" DEFAULT_KEY), NULL)) {
            printf("Folder nu a putut fi creat! \n");
        } else {
            Query(key, path);
        }
    } else {
        printf("RegOpenKeyEx FAIL! \n");
        wprintf(L"%d\n", GetLastError);
    }

    RegCloseKey(key);
}

干杯,祝你们好运。

关于C、Windows API在注册表项后创建文件夹结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15283586/

相关文章:

c - 在 Windows、Linux、Solaris、HP-UX、IBM AIX、Vxworks、Wind River Linux 上休眠几毫秒?

关于系统托盘最小化 - WIN API 的几个问题

c++ - Qt - 如何获取|编译Mysql驱动

REST API - 按 ID 获取请求过滤器

c - 链表令人困惑

c++ - 尝试解码音频上编码的类似 FM 的信号

c - 严格的 C Win GUI 编程

c++ - 适用于 Windows 的 Eclipse - 使用 libPNG

javascript - html5 getUserMedia Api 停止网络摄像头按钮不起作用

.net - YouTube API .NET C#编辑视频问题