January 26, 2023, 09:03:47 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Registry Commons Functions

Started by ExMember001, January 01, 2009, 01:00:43 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ExMember001

Here's a File you can save as RegistryAPI.src
This file contains most commonly used Registry Functions.
To use this functions, add this file to your project then add the extern declaration of the function you want to use where you want to use it.

Happy New Year!!



//RegistryAPI.src
/*
Coded by Andrew.
Translated to Aurora and optimized by KrYpT.
Public Domain
*/

/*
Add these declares in the file you will use these:
extern string ARegGetValue(String Key,opt string ValueName);
extern int ARegSetValue(string Key,string Value,opt string ValueName);
extern int ARegSetDWValue(string Key,int Value,opt string ValueName);
extern int ARegCreateKey(string Key);
extern int ARegDeleteKey(string Key);
extern string ARegEnumKey(string Key,int Index);
extern string ARegEnumValue(string Key,int Index);
extern int ARegCountKeys(string Key);
extern int ARegCountValues(string Key);

Functions are as follows:

ARegGetValue(Key,<ValueName>) - returns the value in the registry at the 'Key' position, in the ValueName category
Example: strA=RegGetValue("HKEY_CURRENT_USER\\Software\\YourSoftware\\Stuff");
Leave ValueName blank to return the (default) value
Returns: the string, or a blank string if errored

ARegSetValue(Key,Value,<ValueName>) - sets a string value at the 'key' position, in the ValueName category
Creates the key if it's not there
Example: RegSetValue("HKEY_CURRENT_USER\\Software\\YourSoftware\\Stuff","Value");
Leave ValueName blank to set the (default) value
Returns: 0 if successful, or non-zero if failed

ARegSetDWValue(Key,Value,<ValueName>) - sets a DWord value at the 'key' position in the ValueName category
Creates the key if it's not there
example: RegSetDWValue("HKEY_CURRENT_USER\\Software\\YourSoftware\\Stuff",12345);
Leave ValueName blank to set the (default) value
Returns: 0 if successful, or non-zero if failed

ARegCreateKey(Key) - Creates a registry key
Example: RegCreateKey("HKEY_CLASSES_ROOT\\.bin\\ShellEx\\Something");
Returns: 0 if successful, or non-zero if failed

ARegDeleteKey(Key) - Deletes a registry key
Example: RegDeleteKey("HKEY_CLASSES_ROOT\\.bin\\ShellEx\\Something");
Returns: 0 if successful, or non-zero if failed

ARegEnumKey(Key,Index) - Enumerates (lists) the subkey at the index position
Example: stTest=RegEnumKey("HKEY_CURRENT_USER\\Software\\YourSoftware",0);
Returns: The key name or a blank string if errored

ARegEnumValue(Key,Index) - Enumerates the value of the key at the index position
Example: stTest=RegEnumValue("HKEY_CURRENT_USER\\Software\\YourSoftware\\File",0);
Returns: The key name or a blank string if errored

ARegCountKeys(Key) - returns the number of subkeys under this key
Example: iCount=RegCountKeys("HKEY_CURRENT_USER\\Software");
Returns: The number of keys

ARegCountValues(Key) - returns the number of values under this key
Example: iCount=RegCountValues("HKEY_CURRENT_USER\\Software\\YourSoftware\\File");
Returns: The number of values
*/

#ifndef Advapi32
    #define Advapi32
  #use "advapi32.lib"
#EndIf
#ifndef RegistryAPI
    #define RegistryAPI
struct FileTime
{
INT dwLowDateTime;
INT dwHighDateTime;
}
IMPORT INT RegOpenKeyExA(INT hKey,POINTER lpSubKey,UNSIGNED INT ulOptions,INT samDesired,POINTER *phkResult);
IMPORT INT RegQueryValueExA(INT hKey,POINTER lpValueName,POINTER *lpReserved,POINTER *lpType,POINTER *lpData,POINTER *lpcbData);
IMPORT INT RegCloseKey (INT hKey);
IMPORT INT RegCreateKeyExA(INT hKey,POINTER lpSubKey,UNSIGNED INT Reserved,POINTER lpClass,UNSIGNED INT dwOptions,INT samDesired,POINTER *lpSecurityAttributes,POINTER *phkResult,POINTER *lpdwDisposition);
IMPORT INT RegSetValueExA(INT hKey,POINTER lpValueName,UNSIGNED INT Reserved,UNSIGNED INT dwType,byte *lpData,UNSIGNED INT cbData);
IMPORT INT RegDeleteKeyA(INT hKey,POINTER lpSubKey);
IMPORT INT RegEnumKeyExA(INT hKey,UNSIGNED INT dwIndex,POINTER lpName,POINTER *lpcchName,POINTER *lpReserved,POINTER lpClass,POINTER *lpcchClass,FILETIME *lpftLastWriteTime);
IMPORT INT RegEnumValueA(INT hKey,UNSIGNED INT dwIndex,POINTER   lpValueName,POINTER *lpcchValueName,POINTER *lpReserved,POINTER *lpType,POINTER *lpData,POINTER *lpcbData);
IMPORT INT RegQueryInfoKeyA(INT hKey,POINTER lpClass,POINTER *lpcchClass,POINTER *lpReserved,POINTER *lpcSubKeys,POINTER *lpcbMaxSubKeyLen,POINTER *lpcbMaxClassLen,POINTER *lpcValues,POINTER *lpcbMaxValueNameLen,POINTER *lpcbMaxValueLen,POINTER *lpcbSecurityDescriptor,FILETIME *lpftLastWriteTime);
#define HKEY_CLASSES_ROOT     0x80000000
#define HKEY_CURRENT_USER     0x80000001
#define HKEY_LOCAL_MACHINE    0x80000002
#define HKEY_USERS            0x80000003
#define HKEY_PERFORMANCE_DATA 0x80000004
#define HKEY_CURRENT_CONFIG   0x80000005
#define HKEY_DYN_DATA         0x80000006
#define KEY_ALL_ACCESS        0xF003F
#define REG_SZ                1
#define REG_BINARY            3
#define REG_DWORD             4
    #endif

global Sub AGetRoot(STRING Key),UNSIGNED INT
{
UNSIGNED INT retval=0;
//Split the root folder from the key
STRING rf=strleft(Key,strfind(Key,"\\")-1);
select rf
{
case "HKEY_CLASSES_ROOT":
retval= HKEY_CLASSES_ROOT;
case "HKEY_CURRENT_USER":
retval= HKEY_CURRENT_USER;
case "HKEY_LOCAL_MACHINE":
retval= HKEY_LOCAL_MACHINE;
case "HKEY_USERS":
retval= HKEY_USERS;
case "HKEY_PERFORMANCE_DATA":
retval= HKEY_PERFORMANCE_DATA;
case "HKEY_CURRENT_CONFIG":
retval= HKEY_CURRENT_CONFIG;
case "HKEY_DYN_DATA":
retval= HKEY_DYN_DATA;
}
return retval;
}

Global Sub ARegGetValue(STRING Key,opt STRING ValueName),STRING
{
UNSIGNED INT ret,typ;
STRING retval="";
UNSIGNED INT ln=255;

//Split the root folder from the key
STRING rf=strleft(Key,strfind(Key,"\\")-1);
STRING ky=strright(Key,(len(Key)-1)-len(rf));

//Get the value
if RegOpenKeyExA(AGetRoot(Key),ky,0,KEY_ALL_ACCESS,ret) = 0
{
RegQueryValueExA(Ret,ValueName,NULL,typ,retval,ln);
if typ=REG_DWORD
{
INT getdw;
ln=4;
RegQueryValueExA(Ret,ValueName,NULL,typ,getdw,ln);
retval=numtostr(getdw);
}
}
RegCloseKey(ret);
//Returns a registry value or blank string if errored
return retval;
}

Global Sub ARegSetValue(STRING Key,STRING Value,opt STRING ValueName),INT
{
//Sets a value in the key

UNSIGNED INT ret,typ;
INT tmp=0;
UNSIGNED INT ln=255;
//Split the root folder from the key
STRING rf=strleft(Key,strfind(Key,"\\")-1);
STRING ky=strright(Key,(len(Key)-1)-len(rf));
//Create the key and get a handle
RegCreateKeyExA(AGetRoot(Key),ky,0,"",0,KEY_ALL_ACCESS,0,ret,tmp);
//Set the value
tmp=0;
ln=len(value)+1;
INT retval=RegSetValueExA(ret,ValueName,tmp,REG_SZ,Value,ln);
RegCloseKey(ret);
//returns 0 for no error, 1 for error
return retval;
}

Global Sub ARegSetDWValue(STRING Key,INT Value,opt STRING ValueName),INT
{
//Sets a Dword value in the key

UNSIGNED INT ret,typ;
INT tmp=0;
UNSIGNED INT ln=255;

//Split the root folder from the key
STRING rf=strleft(Key,strfind(Key,"\\")-1);
STRING ky=strright(Key,(len(Key)-1)-len(rf));

//Create the key and get a handle
RegCreateKeyExA(AGetRoot(Key),ky,0,"",0,KEY_ALL_ACCESS,0,ret,tmp);
//Set the value
tmp=0;
ln=4;
INT retval=RegSetValueExA(ret,ValueName,tmp,REG_DWORD,Value,ln);
RegCloseKey(ret);
//returns 0 for no error, 1 for error
return retval;
}

Global Sub ARegCreateKey(STRING Key),INT
{
//Creates a registry key

UNSIGNED INT ret;
UNSIGNED INT ln=255;
//Split the root folder from the key
STRING rf=strleft(Key,strfind(Key,"\\")-1);
STRING ky=strright(Key,(len(Key)-1)-len(rf));
//Create the key and get a handle
INT retval=RegCreateKeyExA(AGetRoot(Key),ky,0,"",0,KEY_ALL_ACCESS,0,ret,0);
RegCloseKey(ret);
//Returns 0 for no error, non-zero for error
return retval;
}

Global Sub ARegDeleteKey(STRING Key),INT
{
//Deletes a registry key

//Split the root folder from the key
STRING rf=strleft(Key,strfind(Key,"\\")-1);
STRING ky=strright(Key,(len(Key)-1)-len(rf));
INT retval=RegDeleteKeyA(AGetRoot(Key),ky);
//Returns 0 for no error, non-zero for error
return retval;
}

Global Sub ARegEnumKey(STRING Key,INT Index),STRING
{
FileTime ft;
STRING retval="";
UNSIGNED INT ret;
UNSIGNED INT ln=255;
STRING rf=strleft(Key,strfind(Key,"\\")-1);
STRING ky=strright(Key,(len(Key)-1)-len(rf));
if RegOpenKeyExA(AGetRoot(Key),ky,0,KEY_ALL_ACCESS,ret) = 0
{
RegEnumKeyExA(ret,Index,retval,ln,NULL,NULL,ln,ft);
}
RegCloseKey(ret);
//Returns the key at the index specified
return retval;
}

Global Sub ARegEnumValue(STRING Key,INT Index),STRING
{
//Enumerates a registry value
UNSIGNED INT ret;
STRING retval;
INT ln=255;
STRING rf=strleft(Key,strfind(Key,"\\")-1);
STRING ky=strright(Key,(len(Key)-1)-len(rf));
if RegOpenKeyExA(AGetRoot(Key),ky,0,KEY_ALL_ACCESS,ret) = 0
{
RegEnumValueA(ret,Index,retval,ln,NULL,NULL,NULL,NULL);
}
RegCloseKey(ret);
//Returns the value at the index specified
return retval;
}

Global Sub ARegCountKeys(STRING Key),INT
{
FileTime ft;
INT ln=255;
INT retval;
UNSIGNED INT ret;
STRING lpClass="";
STRING rf=strleft(Key,strfind(Key,"\\")-1);
STRING ky=strright(Key,(len(Key)-1)-len(rf));
if RegOpenKeyExA(AGetRoot(Key),ky,0,KEY_ALL_ACCESS,ret) = 0
{
RegQueryInfoKeyA(ret,lpClass,ln,NULL,retval,NULL,NULL,NULL,NULL,NULL,NULL,ft);
}
RegCloseKey(ret);
//Returns the number of sub-keys under this key, or 0 for error
return retval;
}

Global Sub ARegCountValues(STRING Key),INT
{
FileTime ft;
UNSIGNED INT ret;
INT ln=255;
INT retval;
STRING lpClass="";
STRING rf=strleft(Key,strfind(Key,"\\")-1);
STRING ky=strright(Key,(len(Key)-1)-len(rf));
if RegOpenKeyExA(AGetRoot(Key),ky,0,KEY_ALL_ACCESS,ret) = 0
{
RegQueryInfoKeyA(ret,lpClass,ln,NULL,NULL,NULL,NULL,retval,NULL,NULL,NULL,ft);
}
RegCloseKey(ret);
//Returns the number of values under this key
return retval;
}