2

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

#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(已经存在)。

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

4

1 回答 1

2

好的,所以我想通了,问题出在我无法创建路径,显然这很简单,这是我的最终代码,也许它会帮助某人。

#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);
}

祝你们好运,祝你们好运。

于 2013-03-08T09:13:25.530 回答