gpt4 book ai didi

c - 从 RegEnumKeyEx 函数返回字符串数组

转载 作者:行者123 更新时间:2023-11-30 15:18:11 24 4
gpt4 key购买 nike

我尝试运行,但我的程序因 RegEnumKeyEx 出现段错误。

我不知道如何从 RegEnumKeyEx 函数返回指向 char 的指针。

在 hives() 函数中,我得到了 hive 的数量。

在 gethives() 函数中,我得到了 hive 的 char 数组。

#include <windows.h>
#include <stdio.h>

#define REG_ADD_1 HKEY_LOCAL_MACHINE
#define R_PATH "SOFTWARE\\Microsoft\\Windows\\CurrentVersion"

int hives ()
{

HKEY RegTO;

long RegO;
long RC;

char Temp[255];
DWORD TMP=255;

int a=0;

printf ("EXAMPLE OF RegEnumKeyEx\n\n");

RegO= RegOpenKeyEx(REG_ADD_1,R_PATH,0,KEY_ALL_ACCESS, &RegTO);

if (RegO==ERROR_SUCCESS) {

printf ("OK, Key opened: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\n\n");
printf ("Querying Sub-Keys...\n\n");
Sleep (1000);

while (RC!=ERROR_NO_MORE_ITEMS) {

RC=RegEnumKeyEx (RegTO,a,Temp,&TMP,0,NULL,NULL,NULL);
TMP=sizeof (Temp);
if (RC==ERROR_SUCCESS) {

printf ("%s\n", Temp);

}
a++;
}

} else {
printf ("ERROR, couldn't open Key");
return -1;

}

RegCloseKey (RegTO);
return a;
}

char** gethives ()

{

int a, i;

i=hives();

HKEY RegTO;

long RegO;
long RC;

char* Temp[255];

char** strings = Temp;

char** test = malloc(i * sizeof(char));
DWORD TMP=255;

a=0;

printf ("EXAMPLE OF RegEnumKeyEx\n\n");

RegO= RegOpenKeyEx(REG_ADD_1,R_PATH,0,KEY_ALL_ACCESS, &RegTO);

printf ("OK, Key opened: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\n\n");
printf ("Querying Sub-Keys...\n\n");
Sleep (1000);

for (a=1; a < i; a++) {

printf ("RegEnumKeyEx start \n");

RegEnumKeyEx (RegTO,a,*Temp,&TMP,0,NULL,NULL,NULL);

printf ("RegEnumKeyEx end \n");

TMP=sizeof (Temp);

test[a] = malloc(255 * sizeof(char));

strcpy( test[a], *strings);

printf ("test = %s\n", *test);

printf ("%s\n", *Temp);

strings++;

}

RegCloseKey (RegTO);
return test;
}

int main()
{
char** str = gethives();
printf("\n1-st hive is - %s \n", str[1]);
return 0;
}

最佳答案

char **gethives(void){
int a, i;

i=hives();

HKEY RegTO;

long RegO;
long RC;

char Temp[255];//not pointer
char **test = malloc(i * sizeof(char *));//!
DWORD TMP=255;

a=0;

printf ("EXAMPLE OF RegEnumKeyEx\n\n");

RegO= RegOpenKeyEx(REG_ADD_1,R_PATH,0,KEY_ALL_ACCESS, &RegTO);

printf ("OK, Key opened: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\n\n");
printf ("Querying Sub-Keys...\n\n");
Sleep (1000);

for (a=0; a < i; a++) {//start 0
RegEnumKeyEx (RegTO, a, Temp, &TMP,0, NULL, NULL, NULL);
test[a] = strdup(Temp);//malloc & strcpy
}
RegCloseKey (RegTO);
return test;
}

关于c - 从 RegEnumKeyEx 函数返回字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31625366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com