| |
DLLBinder Example - Registry Access
This example enables the user to read and write to the registry.
This code has documentation within the comments.
To use this code, make sure you have downloaded the
DLLBinder or DLLBinder Pro XTRA.
Then simply
copy and paste the Lingo script below into Director.
Feel free to make use of this code in any way you want. If you make any great
improvements, please send us a copy. Or if you have any other examples you
would like to appear in these LIBRARY pages, please send it to
info@the-mindseye.co.uk
Minds Eye Visualisation Services accepts no responibility for the reliability
of any of the source code appearing in these pages. Use it at your own risk.
|
|
|
|
------------------------------------------------------------------------
------------------------------------------------------------------------
-- Name RegistryAccess
-- Script Type Parent
-- Description wrapper for some Win32 Registry access functions
-- Creation Date 31/10/2004
-- Author Minds Eye Visualisation
------------------------------------------------------------------------
------------------------------------------------------------------------
--registry constants. see below for definitions
property ERROR_SUCCESS
property HKEY_CLASSES_ROOT
property HKEY_CURRENT_USER
property HKEY_LOCAL_MACHINE
property HKEY_USERS
property HKEY_PERFORMANCE_DATA
property HKEY_CURRENT_CONFIG
property HKEY_DYN_DATA
property REG_NONE
property REG_SZ
property REG_EXPAND_SZ
property REG_BINARY
property REG_DWORD
property REG_DWORD_LITTLE_ENDIAN
property REG_DWORD_BIG_ENDIAN
property REG_LINK
property REG_MULTI_SZ
property REG_RESOURCE_LIST
property REG_FULL_RESOURCE_DESCRIPTOR
property REG_RESOURCE_REQUIREMENTS_LIST
property REG_OPTION_RESERVED
property REG_OPTION_NON_VOLATILE
property REG_OPTION_VOLATILE
property REG_OPTION_CREATE_LINK
property REG_OPTION_BACKUP_RESTORE
property REG_OPTION_OPEN_LINK
property REG_CREATED_NEW_KEY
property REG_OPENED_EXISTING_KEY
property KEY_ALL_ACCESS
--property for the name Win32 Dll containing the registry access API
property m_dllFile
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION new
--
-- PURPOSE
--
-- instance creation
------------------------------------------------------------------------
------------------------------------------------------------------------
on new me
Initialise(me)
return me
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION Initialise
--
-- PURPOSE
--
-- initialise the Win32 style constants used in the Win32 Registry API
------------------------------------------------------------------------
------------------------------------------------------------------------
on Initialise me
--//////////////////////////////////////////////////
--// winAPI return values as defined in winerror.h
--//////////////////////////////////////////////////
--NB there are loads of others that really should be included here
-- but this'll do for now
ERROR_SUCCESS = 0
--//////////////////////////////////////////////////
--// Reserved Key Handles as defined in winreg.h
--//////////////////////////////////////////////////
HKEY_CLASSES_ROOT =2147483648 -- 0x80000000
HKEY_CURRENT_USER =2147483649 -- 0x80000001
HKEY_LOCAL_MACHINE =2147483650 -- 0x80000002
HKEY_USERS =2147483651 -- 0x80000003
HKEY_PERFORMANCE_DATA =2147483652 -- 0x80000004
HKEY_CURRENT_CONFIG =2147483653 -- 0x80000005
HKEY_DYN_DATA =2147483654 -- 0x80000006
--////////////////////////////////////////////////////
--// Registry Value Types as defined in winnt.h
--////////////////////////////////////////////////////
REG_NONE = 0
REG_SZ = 1
REG_EXPAND_SZ = 2
REG_BINARY = 3
REG_DWORD = 4
REG_DWORD_LITTLE_ENDIAN = 4
REG_DWORD_BIG_ENDIAN = 5
REG_LINK = 6
REG_MULTI_SZ = 7
REG_RESOURCE_LIST = 8
REG_FULL_RESOURCE_DESCRIPTOR = 9
REG_RESOURCE_REQUIREMENTS_LIST = 10
--///////////////////////////////////////////////////////
--// Open/Create Options as defined in winnt.h
--///////////////////////////////////////////////////////
REG_OPTION_RESERVED = 0
REG_OPTION_NON_VOLATILE = 0
REG_OPTION_VOLATILE = 1
REG_OPTION_CREATE_LINK = 2
REG_OPTION_BACKUP_RESTORE = 4
REG_OPTION_OPEN_LINK = 8
--///////////////////////////////////////////////////////
--// Key creation/open disposition as defined in winnt.h
--///////////////////////////////////////////////////////
REG_CREATED_NEW_KEY = 1
REG_OPENED_EXISTING_KEY = 2
--/////////////////////////////////////////////////////////
--// Registry Specific Access Rights as defined in winnt.h
--/////////////////////////////////////////////////////////
--
-- #define DELETE (0x00010000L)
-- #define READ_CONTROL (0x00020000L)
-- #define WRITE_DAC (0x00040000L)
-- #define WRITE_OWNER (0x00080000L)
-- #define SYNCHRONIZE (0x00100000L)
--
-- #define STANDARD_RIGHTS_REQUIRED (0x000F0000L)
--
-- #define STANDARD_RIGHTS_READ (READ_CONTROL)
-- #define STANDARD_RIGHTS_WRITE (READ_CONTROL)
-- #define STANDARD_RIGHTS_EXECUTE (READ_CONTROL)
--
-- #define STANDARD_RIGHTS_ALL (0x001F0000L)
-- #define SPECIFIC_RIGHTS_ALL (0x0000FFFFL)
--
-- #define KEY_QUERY_VALUE (0x0001)
-- #define KEY_SET_VALUE (0x0002)
-- #define KEY_CREATE_SUB_KEY (0x0004)
-- #define KEY_ENUMERATE_SUB_KEYS (0x0008)
-- #define KEY_NOTIFY (0x0010)
-- #define KEY_CREATE_LINK (0x0020)
--
-- #define KEY_READ ((STANDARD_RIGHTS_READ |
-- KEY_QUERY_VALUE |
-- KEY_ENUMERATE_SUB_KEYS |
-- KEY_NOTIFY) &
-- (~SYNCHRONIZE))
-- #define KEY_WRITE ((STANDARD_RIGHTS_WRITE |
-- KEY_SET_VALUE |
-- KEY_CREATE_SUB_KEY) &
-- (~SYNCHRONIZE))
-- #define KEY_EXECUTE ((KEY_READ) &
-- (~SYNCHRONIZE))
-- #define KEY_ALL_ACCESS ((STANDARD_RIGHTS_ALL |
-- KEY_QUERY_VALUE |
-- KEY_SET_VALUE |
-- KEY_CREATE_SUB_KEY |
-- KEY_ENUMERATE_SUB_KEYS |
-- KEY_NOTIFY |
-- KEY_CREATE_LINK) &
-- (~SYNCHRONIZE))
--
----------------------------------------------------------------------
--
--hence if we want KEY_ALL_ACCESS then we need
--to evaluate the KEY_ALL_ACCESS #definition above.
--lets build a little table to make things easier
--
--access type hex value 32bit binary value
----------------------------------------------------------------------
--STANDARD_RIGHTS_ALL 0x001F0000 00000000000111110000000000000000
--KEY_QUERY_VALUE 0x00000001 00000000000000000000000000000001
--KEY_SET_VALUE 0x00000002 00000000000000000000000000000010
--KEY_CREATE_SUB_KEY 0x00000004 00000000000000000000000000000100
--KEY_ENUMERATE_SUB_KEYS 0x00000008 00000000000000000000000000001000
--KEY_NOTIFY 0x00000010 00000000000000000000000000010000
--KEY_CREATE_LINK 0x00000020 00000000000000000000000000100000
--SYNCHRONIZE 0x00100000 00000000000100000000000000000000
--
--
--and now we can do some logical arithmetic
--
--
-- OR
-- 00000000000111110000000000000000
-- 00000000000000000000000000000001
-- 00000000000000000000000000000010
-- 00000000000000000000000000000100
-- 00000000000000000000000000001000
-- 00000000000000000000000000010000
-- 00000000000000000000000000100000
-- --------------------------------
-- 00000000000111110000000000111111
--
--
-- AND
--
--
-- NOT
-- 00000000000100000000000000000000
-- --------------------------------
-- 11111111111011111111111111111111
--
--
-- =>
--
-- AND
-- 00000000000111110000000000111111
-- 11111111111011111111111111111111
-- --------------------------------
-- 00000000000011110000000000111111
--
--
--and 00000000000011110000000000111111 is of course 983103 in base10.
--
--whew!
--
--all this is just to explain how these kind of bitmasks work =)
--naturally its much simpler to just get the base10 evaluation
--with a single line of C. ie printf("%d", KEY_ALL_ACCESS);
KEY_ALL_ACCESS=983103
--set the name of the Win32 Dll ctg the registry access API
m_dllFile = "advapi32.dll"
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION regCreateKey
--
-- PURPOSE
--
-- create a registry key using the Win32 ANSI function RegCreateKeyExA
------------------------------------------------------------------------
------------------------------------------------------------------------
on regCreateKey me, hKey, strSubKey
--mevCallDllFun parameters
fncName = "RegCreateKeyExA"
argFormat = "D,s128,D,D,D,D,D,*D,*D"
retValFormat = "d"
retValList = []
--build the RegCreateKeyExA parameter list
argList=[]
argList[1] = hKey
argList[2] = strSubKey
argList[3] = 0
argList[4] = 0
argList[5] = REG_OPTION_NON_VOLATILE
argList[6] = KEY_ALL_ACCESS
argList[7] = 0
argList[8] = 0
argList[9] = 0
--call the dll
errVal=mevCallDllFun(m_dllFile,\
fncName,\
argFormat,\
argList,\
retValFormat,\
retValList)
--now lets get the results
--the last two of the input parameters (P8 & P9) are used
--to return values to the caller. we shall put them into
--specific variables for the sake of clarity
--P8 the handle to the newly created subkey
hSubKey=argList[8]
--P9 an integer relating to the create/open disposition
--this return value is for error handling purposes
--basically it should be equal to REG_CREATED_NEW_KEY
dwCreateStatus=argList[9]
--most Win32 API calls return an integer representing the error
--status of the call. In this case we hope it equals ERROR_SUCCESS
dwRet=retValList[1]
--skeleton error handling
if (errVal < 0) then
--the call to mevCallDllFun has failed
else if (dwRet <> ERROR_SUCCESS) then
--RegCreateKeyExA has failed
--check dwRet against other values in winerror.h
else if (dwCreateStatus <> REG_CREATED_NEW_KEY) then
--key has not been created
end if
--return the handle to the newly created key.
return hSubKey
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION regOpenKey
--
-- PURPOSE
--
-- open a registry key using the Win32 ANSI function RegOpenKeyExA
------------------------------------------------------------------------
------------------------------------------------------------------------
on regOpenKey me, hKey, strSubKey
--mevCallDllFun parameters
fncName = "RegOpenKeyExA"
argFormat = "D,s128,D,D,*D"
retValFormat = "d"
retValList = []
--build the RegOpenKeyExA param list
argList=[]
argList[1] = hKey
argList[2] = strSubKey
argList[3] = 0
argList[4] = KEY_ALL_ACCESS
argList[5] = 0
-- call the dll
errVal = mevCallDllFun(m_dllFile,\
fncName,\
argFormat,\
argList,\
retValFormat,\
retValList)
--now lets get the results
--the last input parameter (P5) is used to
--return a value to the caller. we shall put it into
--a specific variable for the sake of clarity
--P5 the last input parameter is used to return handle
--to the opened subkey to caller
hSubKey=argList[5]
--most Win32 API calls return an integer representing the error
--status of the call. In this case we hope it equals ERROR_SUCCESS
dwRet=retValList[1]
--skeleton error handling
if (errVal < 0) then
--the call to mevCallDllFun has failed
else if (dwRet <> ERROR_SUCCESS) then
--RegOpenKeyExA has failed
--check dwRet against other values in winerror.h
else if (hSubKey = 0) then
--key has not been opened
end if
--reurn handle to the opened sub-key
return hSubKey
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION regCloseKey
--
-- PURPOSE
--
-- close a registry key using the Win32 function RegCloseKey
------------------------------------------------------------------------
------------------------------------------------------------------------
on regCloseKey me, hKey
--mevCallDllFun parameters
fncName = "RegCloseKey"
argFormat = "D"
retValFormat = "d"
retValList = []
--build the arg val list
argList = [hKey]
-- call the dll
errVal = mevCallDllFun(m_dllFile,\
fncName,\
argFormat,\
argList,\
retValFormat,\
retValList)
--most Win32 API calls return an integer representing the error
--status of the call. In this case we hope it equals ERROR_SUCCESS
dwRet=retValList[1]
--skeleton error handling
if (errVal < 0) then
--the call to mevCallDllFun has failed
else if (dwRet <> ERROR_SUCCESS) then
--RegCloseKey has failed
--check dwRet against other values in winerror.h
end if
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION regDeleteKey
--
-- PURPOSE
--
-- delete a registry key using the Win32 ANSI function RegDeleteKeyA
------------------------------------------------------------------------
------------------------------------------------------------------------
on regDeleteKey me, hKey, strSubKey
--mevCallDllFun parameters
fncName = "RegDeleteKeyA"
argFormat = "D, s128"
retValFormat = "d"
retValList = []
--build the arg val list
argList = [hKey, strSubKey]
-- call the dll
errVal = mevCallDllFun(m_dllFile, \
fncName, \
argFormat, \
argList, \
retValFormat, \
retValList)
--most Win32 API calls return an integer representing the error
--status of the call. In this case we hope it equals ERROR_SUCCESS
dwRet=retValList[1]
--skeleton error handling
if (errVal < 0) then
--the call to mevCallDllFun has failed
else if (dwRet <> ERROR_SUCCESS) then
--RegDeleteKeyA has failed
--check dwRet against other values in winerror.h
end if
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION regSet32BitIntegerValue
--
-- PURPOSE
--
-- set registry value using the Win32 ANSI function RegSetValueExA
------------------------------------------------------------------------
------------------------------------------------------------------------
on regSet32BitIntegerValue me, hKey, strValueName, n32BitInt
--mevCallDllFun parameters
fncName = "RegSetValueExA"
argFormat = "D,s128,D,D,*D,D"
retValFormat = "d"
retValList = []
--build the RegSetValueExA param list
argList=[]
argList[1] = hKey
argList[2] = strValueName
argList[3] = 0
argList[4] = REG_DWORD
argList[5] = n32BitInt
argList[6] = 4 --the size of a 32bit integer is 4 bytes
-- call the dll
errVal = mevCallDllFun(m_dllFile, \
fncName, \
argFormat, \
argList, \
retValFormat, \
retValList)
--most Win32 API calls return an integer representing the error
--status of the call. In this case we hope it equals ERROR_SUCCESS
dwRet=retValList[1]
--skeleton error handling
if (errVal < 0) then
--the call to mevCallDllFun has failed
else if (dwRet <> ERROR_SUCCESS) then
--RegSetValueExA has failed
--check dwRet against other values in winerror.h
end if
end
------------------------------------------------------------------------
------------------------------------------------------------------------
-- FUNCTION regGet32BitIntegerValue
--
-- PURPOSE
--
-- retrieve registry value using the Win32 ANSI function RegSetValueExA
------------------------------------------------------------------------
------------------------------------------------------------------------
on regGet32BitIntegerValue me, hKey, strValueName
--mevCallDllFun parameters
dllFile = "advapi32.dll"
fncName = "RegQueryValueExA"
argFormat = "D,s128,D,*D,*D,*D"
retValFormat = "d"
retValList = [0]
--build the RegQueryValueExA param list
argList=[]
argList[1] = hKey
argList[2] = strValueName
argList[3] = 0
argList[4] = 0
argList[5] = 0
argList[6] = 4 --the size of a 32bit integer is 4 bytes
-- call the dll
errVal = mevCallDllFun(dllFile, \
fncName, \
argFormat, \
argList, \
retValFormat, \
retValList)
--now lets get the results
--three of the input parameters (P4,P5,P6) are used
--to return values to the caller. we shall put them into
--specific variables for the sake of clarity
--P4 the data type of the registry value - we hope its REG_DWORD
dwRetType=argList[4]
--P6 the size of the returned data -in this case
--we don't really need to worry about it.
dwRetSize=argList[6]
--P5 the actual value that we require
dwRetVal=argList[5]
--most Win32 API calls return an integer representing the error
--status of the call. In this case we hope it equals ERROR_SUCCESS
dwRet=retValList[1]
--skeleton error handling
if (errVal < 0) then
--the call to mevCallDllFun has failed
else if (dwRet <> ERROR_SUCCESS) then
--RegSetValueExA has failed
--check dwRet against other values in winerror.h
else if (dwRetType <> REG_DWORD) then
--oh dear. we've not got the expected data type
end if
--return the retrived registry value
return dwRetVal
end
------------------------------------------------------------------------
------------------------------------------------------------------------
--end of script
|