@@ -129,9 +129,6 @@ filep(lua_State *L, int i)
129129}
130130
131131
132- static int
133- concat_fname (lua_State * L , const char * fname );
134-
135132static int
136133dirp (lua_State * L , int i )
137134{
@@ -802,7 +799,8 @@ lua_rmdir(lua_State *L)
802799/* uname */
803800
804801
805- static int lua_uname (lua_State * L )
802+ static int
803+ lua_uname (lua_State * L )
806804{
807805#if defined(LUA_WIN )
808806 const char * name ;
@@ -839,7 +837,87 @@ static int lua_uname(lua_State *L)
839837#endif
840838}
841839
842-
840+ static int
841+ lua_getregistryvalue (lua_State * L )
842+ {
843+ #ifdef LUA_WIN
844+ static char * keynames [] = {
845+ "HKEY_CLASSES_ROOT" ,
846+ "HKEY_CURRENT_CONFIG" ,
847+ "HKEY_CURRENT_USER" ,
848+ "HKEY_LOCAL_MACHINE" ,
849+ "HKEY_USERS" ,
850+ NULL };
851+ static HKEY keys [] = {
852+ HKEY_CLASSES_ROOT ,
853+ HKEY_CURRENT_CONFIG ,
854+ HKEY_CURRENT_USER ,
855+ HKEY_LOCAL_MACHINE ,
856+ HKEY_USERS
857+ };
858+
859+ HKEY rkey = keys [ luaL_checkoption (L , 1 , NULL , keynames ) ];
860+ const char * subkey = luaL_checkstring (L , 2 );
861+ const char * value = luaL_checkstring (L , 3 );
862+ HKEY skey ;
863+ DWORD type ;
864+ DWORD len = 0 ;
865+ char * data = "" ;
866+ LONG res ;
867+ res = RegOpenKeyExA (rkey , subkey , 0 , KEY_READ , & skey );
868+ if (res != ERROR_SUCCESS )
869+ {
870+ lua_pushnil (L );
871+ lua_pushinteger (L , res );
872+ if (res == ERROR_FILE_NOT_FOUND )
873+ lua_pushstring (L , "subkey not found" );
874+ if (res == ERROR_ACCESS_DENIED )
875+ lua_pushstring (L , "subkey access denied" );
876+ else
877+ return 2 ;
878+ return 3 ;
879+ }
880+ res = RegQueryValueExA (skey , value , NULL , & type , (LPBYTE )data , & len );
881+ if (res == ERROR_MORE_DATA )
882+ {
883+ len += 8 ;
884+ if ((data = (char * )malloc (len )))
885+ res = RegQueryValueExA (skey , value , NULL , & type , (LPBYTE )data , & len );
886+ }
887+ if (res != ERROR_SUCCESS )
888+ {
889+ RegCloseKey (skey );
890+ lua_pushnil (L );
891+ lua_pushinteger (L , res );
892+ if (res == ERROR_FILE_NOT_FOUND )
893+ lua_pushstring (L , "value not found" );
894+ if (res == ERROR_ACCESS_DENIED )
895+ lua_pushstring (L , "value access denied" );
896+ else
897+ return 2 ;
898+ return 3 ;
899+ }
900+ switch (type )
901+ {
902+ case REG_SZ :
903+ if (((const char * )data )[len - 1 ] == 0 ) len -= 1 ;
904+ case REG_BINARY :
905+ lua_pushlstring (L , (const char * )data , (int )len );
906+ return 1 ;
907+ case REG_DWORD :
908+ lua_pushinteger (L , (lua_Integer )* (const DWORD * )data );
909+ return 1 ;
910+ default :
911+ lua_pushnil (L );
912+ lua_pushinteger (L , res );
913+ lua_pushfstring (L , "getting registry type %d not implemented" , type );
914+ return 3 ;
915+ }
916+ #else
917+ luaL_error (L , "This function exists only on windows" );
918+ return 0 ;
919+ #endif
920+ }
843921
844922/* ------------------------------------------------------ */
845923/* require (with global flag) */
@@ -969,10 +1047,6 @@ path_require(lua_State *L)
9691047
9701048
9711049
972- /* ------------------------------------------------------ */
973- /* uname */
974-
975-
9761050
9771051/* ------------------------------------------------------ */
9781052/* register */
@@ -991,6 +1065,7 @@ static const struct luaL_Reg paths__ [] = {
9911065 {"mkdir" , lua_mkdir },
9921066 {"rmdir" , lua_rmdir },
9931067 {"uname" , lua_uname },
1068+ {"getregistryvalue" , lua_getregistryvalue },
9941069 {"require" , path_require },
9951070 {NULL , NULL }
9961071};
0 commit comments