从RegEnumKeyEx函数返回字符串数组

时间:2015-07-25 10:10:06

标签: c winapi

我尝试过运行,但我的程序与RegEnumKeyEx分离。

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

在hives()函数中,我得到了一些荨麻疹。

在gethives()函数中,我得到了一些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;
}

1 个答案:

答案 0 :(得分:1)

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