changeset 976:7427fc405ce1 octave-forge

read registry
author aadler
date Fri, 13 Jun 2003 17:36:46 +0000
parents 09f24d8052e8
children 7b69a38a06b4
files extra/Windows/Makefile extra/Windows/win32api.cc extra/Windows/win32api_win32part.cc
diffstat 3 files changed, 116 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/extra/Windows/Makefile	Fri Jun 13 15:12:25 2003 +0000
+++ b/extra/Windows/Makefile	Fri Jun 13 17:36:46 2003 +0000
@@ -6,7 +6,9 @@
   endif
 endif
 
-all: grab.oct win32api.oct
+LINKS= win32_MessageBox.oct win32_ReadRegistry.oct
+
+all: grab.oct win32api.oct $(LINKS)
 
 grab.oct: grab.o grab_win32part.o
 	$(MKOCTFILE) -o $@ $^
@@ -14,3 +16,6 @@
 win32api.oct: win32api.o win32api_win32part.o
 	$(MKOCTFILE) -o $@ $^
 
+$(LINKS): win32api.oct
+	ln -s $< $@
+
--- a/extra/Windows/win32api.cc	Fri Jun 13 15:12:25 2003 +0000
+++ b/extra/Windows/win32api.cc	Fri Jun 13 17:36:46 2003 +0000
@@ -21,6 +21,13 @@
 win32_MessageBox( const char * text,
                   const char * title,
                   int boxtype);
+int
+win32_ReadRegistry( const char *key,
+                    const char *subkey,
+                    const char *value,
+                    char * buffer,
+                    int  * buffer_sz
+                  );
 
 #include <octave/oct.h>
 
@@ -97,3 +104,63 @@
     retval(0)= (double) rv;
     return retval;
 }
+
+DEFUN_DLD (win32_ReadRegistry, args, ,
+           "[rv,code]= win32_ReadRegistry (key,subkey,value)\n"
+           "\n"
+           "Usage:\n"
+           "   key='SOFTWARE\\\\Cygnus Solutions\\\\Cygwin\\\\mounts v2';\n"
+           "   win32_ReadRegistry('HKLM',key,'cygdrive prefix')\n"
+           "\n"
+           "key must be one of the following strings\n"
+           "  HKCR  % -> HKEY_CLASSES_ROOT\n"
+           "  HKCU  % -> HKEY_CURRENT_USER\n"
+           "  HKLM  % -> HKEY_LOCAL_MACHINE\n"
+           "  HKU   % -> HKEY_USERS\n"
+           "\n"
+           "'rv' is an octave string of the returned bytes.\n"
+           "This is a natural format for REG_SZ data; however, \n"
+           "if the registry data was in another format, REG_DWORD\n"
+           "then the calling program will need to process them\n"
+           "\n"
+           "'code' is the success code. Values correspond to the\n"
+           "codes in the winerror.h header file. The code of 0 is\n"
+           "success, while other codes indicate failure\n"
+           "In the case of failure, 'rv' will be empty\n"
+          )
+{
+    octave_value_list retval;
+    int nargin = args.length();
+    if( nargin != 3 ||
+        !args(0).is_string() ||
+        !args(1).is_string() ||
+        !args(2).is_string()
+      ) {
+        print_usage("win32_ReadRegistry");
+        return retval;
+    }
+
+    const char * key   = args(0).string_value().c_str();
+    const char * subkey= args(1).string_value().c_str();
+    const char * value = args(2).string_value().c_str();
+
+    // call registry first time to get size and existance
+    int buffer_sz=0;
+    int retcode=
+    win32_ReadRegistry(key,subkey,value,NULL, &buffer_sz);
+    if (retcode != 0) {
+        retval(0)= new Matrix(0,0);
+        retval(1)= (double) retcode;
+        error("asdf");
+    } else {
+        char * buffer= new char[ buffer_sz ];
+        int retcode=
+        win32_ReadRegistry(key,subkey,value,buffer, &buffer_sz);
+        retval(0)= string_vector( buffer );
+        retval(1)= (double) retcode;
+        retval(2)= (double) buffer_sz;
+        delete buffer;
+    }
+
+    return retval;
+}
--- a/extra/Windows/win32api_win32part.cc	Fri Jun 13 15:12:25 2003 +0000
+++ b/extra/Windows/win32api_win32part.cc	Fri Jun 13 17:36:46 2003 +0000
@@ -27,3 +27,46 @@
     return
     MessageBox( NULL, text, title, boxtype | MB_SETFOREGROUND );
 }
+
+int
+win32_ReadRegistry( const char *key,
+                    const char *subkey,
+                    const char *value,
+                    char * buffer,
+                    int  * buffer_sz
+                  )
+{
+    HKEY hprimkey, hsubkey;
+    if ( 0== strcmp(key, "HKEY_CLASSES_ROOT") ||
+         0== strcmp(key, "HKCR")) {
+        hprimkey= HKEY_CLASSES_ROOT;
+    } else
+    if ( 0== strcmp(key, "HKEY_CURRENT_USER") ||
+         0== strcmp(key, "HKCU")) {
+        hprimkey= HKEY_CURRENT_USER;
+    } else
+    if ( 0== strcmp(key, "HKEY_LOCAL_MACHINE") ||
+         0== strcmp(key, "HKLM")) {
+        hprimkey= HKEY_LOCAL_MACHINE;
+    } else
+    if ( 0== strcmp(key, "HKEY_USERS") ||
+         0== strcmp(key, "HKU")) {
+        hprimkey= HKEY_USERS;
+    } else {
+        return -1; // We can't handle this key
+    }
+    int retval;
+
+    retval=
+    RegOpenKeyEx(hprimkey, subkey, 0, KEY_READ, &hsubkey);
+    if (retval == NO_ERROR) {
+        DWORD dwBuffSz= *buffer_sz;
+        retval=
+        RegQueryValueEx(hsubkey, value, NULL, NULL, 
+                (BYTE *) buffer, & dwBuffSz);
+        *buffer_sz = dwBuffSz;
+    }
+
+    RegCloseKey(hsubkey);
+    return retval;
+}