Correction winqueryreg on Windows x64
Allan Cornet [Thu, 24 May 2007 19:23:07 +0000 (19:23 +0000)]
try to read "Wow64" x64 Windows registry first and after try to read "Wow32" Windows registry

scilab/modules/wintools/src/c/registry.c

index 15e666a..6d7bd3c 100644 (file)
@@ -1,7 +1,10 @@
 /*-----------------------------------------------------------------------------------*/
-/* --- Windows only ---- */
+/* INRIA */
+/* Allan CORNET */
+/*-----------------------------------------------------------------------------------*/
 #include <windows.h>
 #include "win_mem_alloc.h"
+#include "../../../../libs/GetWindowsVersion/GetWindowsVersion.h"
 /*-----------------------------------------------------------------------------------*/
 #define MAX_KEY_LENGTH 255
 #define MAX_VALUE_NAME 16383
@@ -16,39 +19,50 @@ BOOL WindowsQueryRegistry(char *ParamIn1,char *ParamIn2,char *ParamIn3,char *Par
        BOOL bOK=TRUE;
        HKEY key;
        HKEY hKeyToOpen=NULL;
+       DWORD OpensKeyOptions = 0;
+       DWORD type=0;
 
        hKeyToOpen =GetHkeyrootFromString(ParamIn1);
-
-       if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY , &key) == ERROR_SUCCESS )
+       
+       if (IsWow64())
        {
-               DWORD type=0;
+               OpensKeyOptions = KEY_QUERY_VALUE | KEY_WOW64_64KEY;
+               if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, OpensKeyOptions, &key) != ERROR_SUCCESS) 
+               {
+                       OpensKeyOptions = KEY_QUERY_VALUE | KEY_WOW64_32KEY;
+                       if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, OpensKeyOptions, &key) != ERROR_SUCCESS) return FALSE;
+               }
+       }
+       else
+       {
+               OpensKeyOptions = KEY_QUERY_VALUE;
+               if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, OpensKeyOptions, &key) != ERROR_SUCCESS) return FALSE;
+       }
 
-               if ( RegQueryValueEx(key, ParamIn3, NULL, &type, NULL, NULL) == ERROR_SUCCESS )
+       if ( RegQueryValueEx(key, ParamIn3, NULL, &type, NULL, NULL) == ERROR_SUCCESS )
+       {
+               if ( (type == REG_EXPAND_SZ) || (type == REG_SZ) )
                {
-                       if ( (type == REG_EXPAND_SZ) || (type == REG_SZ) )
+                       DWORD Length=MAX_PATH;
+                       char Line[MAX_PATH];
+                       if (RegQueryValueEx(key, ParamIn3, NULL, &type, (LPBYTE)&Line, &Length) == ERROR_SUCCESS )
                        {
-                               DWORD Length=MAX_PATH;
-                               char Line[MAX_PATH];
-                               if (RegQueryValueEx(key, ParamIn3, NULL, &type, (LPBYTE)&Line, &Length) == ERROR_SUCCESS )
-                               {
-                                       wsprintf(ParamOut1,"%s",Line);
-                                       *OuputIsREG_SZ=TRUE;
-                               }
+                               wsprintf(ParamOut1,"%s",Line);
+                               *OuputIsREG_SZ=TRUE;
                        }
-                       else
+               }
+               else
+               {
+                       DWORD size=4;
+                       int Num=0;
+                       if (RegQueryValueEx(key, ParamIn3, NULL, &type, (LPBYTE)&Num, &size) == ERROR_SUCCESS )
                        {
-                               DWORD size=4;
-                               int Num=0;
-                               if (RegQueryValueEx(key, ParamIn3, NULL, &type, (LPBYTE)&Num, &size) == ERROR_SUCCESS )
-                               {
-                                       *ParamOut2=Num;
-                                       *OuputIsREG_SZ=FALSE;
-                               }
+                               *ParamOut2=Num;
+                               *OuputIsREG_SZ=FALSE;
                        }
                }
-               RegCloseKey(key);
        }
-       else bOK=FALSE;
+       RegCloseKey(key);
 
        return bOK;
 }
@@ -58,48 +72,59 @@ BOOL WindowsQueryRegistryList(char *ParamIn1,char *ParamIn2,int dimMax,char **Li
        BOOL bOK=TRUE;
        HKEY key;
        HKEY hKeyToOpen=NULL;
+       DWORD OpensKeyOptions = 0;
+       int i=0;
 
        hKeyToOpen =GetHkeyrootFromString(ParamIn1);
 
-       if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, KEY_READ | KEY_WOW64_64KEY , &key) == ERROR_SUCCESS )
+       if (IsWow64())
        {
-               int i=0;
-               for (i=0; i<dimMax; i++) 
-               { 
-                       TCHAR    achKey[MAX_KEY_LENGTH];
-                       DWORD    cbName = MAX_KEY_LENGTH;
-                       DWORD   Type;
-                       DWORD retCode;
-
-                       retCode=RegEnumValue(key,i,
-                               achKey,
-                               &cbName,
-                               NULL,
-                               &Type,
-                               NULL,
-                               NULL);
-
-                       if (retCode != ERROR_SUCCESS) 
-                       {
-                               bOK=FALSE;                      
-                               RegCloseKey(key);
-                               return bOK;
-                       }
-                       else
-                       {
-                               char *chaine=NULL;
-                               chaine=(char*) MALLOC(sizeof(char)*255);
-                               strcpy(chaine,achKey);
-                               *ListKeys=chaine;
-                               *ListKeys++;
-                       }
+               OpensKeyOptions = KEY_READ  | KEY_WOW64_64KEY;
+               if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, OpensKeyOptions, &key) != ERROR_SUCCESS ) 
+               {
+                       OpensKeyOptions = KEY_READ  | KEY_WOW64_32KEY;
+                       if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, OpensKeyOptions, &key) != ERROR_SUCCESS ) return FALSE;
                }
+       }
+       else
+       {
+               OpensKeyOptions = KEY_READ ;
+               if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, OpensKeyOptions, &key) != ERROR_SUCCESS ) return FALSE;
+       }
 
-               RegCloseKey(key);
-
+       for (i=0; i<dimMax; i++) 
+       { 
+               TCHAR    achKey[MAX_KEY_LENGTH];
+               DWORD    cbName = MAX_KEY_LENGTH;
+               DWORD   Type;
+               DWORD retCode;
+
+               retCode=RegEnumValue(key,i,
+                       achKey,
+                       &cbName,
+                       NULL,
+                       &Type,
+                       NULL,
+                       NULL);
+
+               if (retCode != ERROR_SUCCESS) 
+               {
+                       bOK=FALSE;                      
+                       RegCloseKey(key);
+                       return bOK;
+               }
+               else
+               {
+                       char *chaine=NULL;
+                       chaine=(char*) MALLOC(sizeof(char)*255);
+                       strcpy(chaine,achKey);
+                       *ListKeys=chaine;
+                       *ListKeys++;
+               }
        }
-       else bOK=FALSE;
 
+       RegCloseKey(key);
+       
        return bOK;
 }
 /*-----------------------------------------------------------------------------------*/
@@ -122,24 +147,39 @@ BOOL WindowsQueryRegistryNumberOfElementsInList(char *ParamIn1,char *ParamIn2,in
 
        HKEY hKeyToOpen;
        HKEY hTestKey;
+       DWORD OpensKeyOptions = 0;
+
+       DWORD retCode; 
+
+       TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name 
+       DWORD    cchClassName = MAX_PATH;  // size of class string 
+       DWORD    cSubKeys=0;               // number of subkeys 
+       DWORD    cbMaxSubKey;              // longest subkey size 
+       DWORD    cchMaxClass;              // longest class string 
+       DWORD    cValues;              // number of values for key 
+       DWORD    cchMaxValue;          // longest value name 
+       DWORD    cbMaxValueData;       // longest value data 
+       DWORD    cbSecurityDescriptor; // size of security descriptor 
+       FILETIME ftLastWriteTime;      // last write time 
 
        hKeyToOpen =GetHkeyrootFromString(ParamIn1);
-       if( RegOpenKeyEx(hKeyToOpen,ParamIn2, 0, KEY_READ | KEY_WOW64_64KEY, &hTestKey) == ERROR_SUCCESS )
+
+       if (IsWow64())
+       {
+               OpensKeyOptions = KEY_READ  | KEY_WOW64_64KEY;
+               if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, OpensKeyOptions, &hTestKey) != ERROR_SUCCESS ) 
+               {
+                       OpensKeyOptions = KEY_READ  | KEY_WOW64_32KEY;
+                       if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, OpensKeyOptions, &hTestKey) != ERROR_SUCCESS ) return FALSE;
+               }
+       }
+       else
        {
-               DWORD retCode; 
-
-               TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name 
-               DWORD    cchClassName = MAX_PATH;  // size of class string 
-               DWORD    cSubKeys=0;               // number of subkeys 
-               DWORD    cbMaxSubKey;              // longest subkey size 
-               DWORD    cchMaxClass;              // longest class string 
-               DWORD    cValues;              // number of values for key 
-               DWORD    cchMaxValue;          // longest value name 
-               DWORD    cbMaxValueData;       // longest value data 
-               DWORD    cbSecurityDescriptor; // size of security descriptor 
-               FILETIME ftLastWriteTime;      // last write time 
-
-               retCode = RegQueryInfoKey(
+               OpensKeyOptions = KEY_READ ;
+               if ( RegOpenKeyEx(hKeyToOpen, ParamIn2, 0, OpensKeyOptions, &hTestKey) != ERROR_SUCCESS ) return FALSE;
+       }
+
+       retCode = RegQueryInfoKey(
                        hTestKey,                    // key handle 
                        achClass,                // buffer for class name 
                        &cchClassName,           // size of class string 
@@ -153,12 +193,10 @@ BOOL WindowsQueryRegistryNumberOfElementsInList(char *ParamIn1,char *ParamIn2,in
                        &cbSecurityDescriptor,   // security descriptor 
                        &ftLastWriteTime);       // last write time 
 
-               if (retCode != ERROR_SUCCESS) bOK=FALSE;
-               else *Number=cValues;
+       if (retCode != ERROR_SUCCESS) bOK=FALSE;
+       else *Number=cValues;
 
-               RegCloseKey(hKeyToOpen);
-       }
-       else bOK=FALSE;
+       RegCloseKey(hKeyToOpen);
 
        return bOK;
 }