Guidance
指路人
g.yi.org
Software / Reginald / Examples / registry.rex

Register 
新用户注册
Search 搜索
首页 
Home Home
Software
Upload

  
/* An example of FUNCDEF'ing various Windows registry
 * functions.
 */

OPTIONS "WINFUNC NOSOURCE C_CALL"
NUMERIC DIGITS 10



/* =======================================================
 * Functions used by all tests below.
 * RegCloseKey() is used a lot to close a key below.
 * RegOpenKeyEx() is used when querying keys/values.
 * RegCreateKeyEx() is used when creating keys.
 * =======================================================
 */
/* Register RegOpenKeyEx once */
FUNCDEF("RegOpenKeyEx", "32, void, str, 32u, 32u, void stor", 'advapi32')

/* Register RegCloseKey */
FUNCDEF("RegCloseKey", "32, void", 'advapi32')

/* Register RegCreateKeyEx once */
FUNCDEF("RegCreateKeyEx", "32, void, str, 32u, str, 32u, 32u, 32u, void stor, 32u stor", 'advapi32')





/* =======================================================
 * Querying binary characters
 * =======================================================
 */

/* Register RegQueryValueEx once, so we can call it
 * as many times as we like. We'll define it once callable
 * as RegQueryBinary to query a binary value. Note: We set
 * the char stor to hold 260 bytes. You should make it as
 * large as the largest binary string you expect to read from
 *  the registry
 */
FUNCDEF("RegQueryBinary", "32, void, str, void, 32 *, char[260] stor, 32 * dual", 'advapi32', 'RegQueryValueEx')

/* The first arg to RegopenKeyEx is either the return from
 * RegOpenKeyEx() for the parent key, or one of the following
 * numbers:
 *
 * 2147483648 for HKEY_CLASSES_ROOT
 * 2147483649 for HKEY_CURRENT_USER
 * 2147483650 for HKEY_LOCAL_MACHINE
 * 2147483651 for HKEY_USERS
 * 2147483652 for HKEY_PERFORMANCE_DATA
 * 2147483653 for HKEY_CURRENT_CONFIG
 * 2147483654 for HKEY_DYN_DATA
 *
 * Second arg is the name of the key to open.
 *
 * Third arg can be omitted.
 *
 * Fourth arg is one of the following:
 *
 * 983103 = all types of access/notification.
 * 131097 = read a value, and enumerate keys, or be notified when a key/value changes.
 * 131078 = write a value or create a key.
 * 1 = read a value only.
 * 2 = write a value only.
 * 4 = create a key only.
 * 8 = enumerate keys only.
 * 16 = Be notified when a key/value changes.
 * 32= Create a symbolic link only.
 *
 * The fifth arg is the name of the REXX variable where you
 * want the key handle stored.
 *
 * RETURNS: 0 if success, or an error number. If the key doesn't
 * exist, an error is returned.
 */

/* Open the "HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices" key for reading */
err = regopenkeyex(2147483650, "SYSTEM\MountedDevices", , 131097, handle) 
IF err == 0 THEN DO

   /* The first arg to RegQueryValue is the return value from
    * RegOpenKeyEx() for the key that contains this value:
    *
    * Second arg is the value name.
    *
    * Third arg can be omitted.
    *
    * Fourth arg is the name of a REXX variable with one of the following values:
    *
    * 0 = No defined value type
    * 1 = Null terminated string
    * 2 = A null-terminated string that contains unexpanded references to
    *     environment variables (for example, "%PATH%").
    * 3 = Binary data in any form
    * 4 = 32-bit number in Intel format (little endian)
    * 5 = 32-bit number in big endian format
    * 6 = Symbolic Link
    * 7 = An array of null-terminated strings, terminated by two null characters
    * 8 = Resource list in the resource map
    * 9 = Resource list in the hardware description
    * 10 = REG_RESOURCE_REQUIREMENTS_LIST
    *
    * Fifth arg is the name of the variable where you want the value stored.
    *
    * Sixth arg is the name of the variable where you store the max size of the
    * string buffer. We set this to 260 when we FUNCDEF'd RegQueryValueEx.
    * After the function returns successfully, this variable will be set to how
    * long the returned value is (in bytes).
    *
    * If success, 0 is returned, otherwise an error number. If the buffer we
    * specified is too small, we'll get an error, and the above variable will
    * be set to how big a buffer we need.
    */

	/* Read the binary value "\DosDevices\C:" */
	valuetype = 3  /* Binary type */
	myvaluesize = 260
	err = regquerybinary(handle, "\DosDevices\C:", , valuetype, myvalue, myvaluesize)
	IF err \== 0 THEN SAY "Error reading binary value:" err
	ELSE DO
		SAY "Length of binary data is" myvaluesize "bytes"

		/* For reading a binary value, we have to trim off only what we need */
		myvalue = LEFT(myvalue, myvaluesize)
	END

	/* Close the key */
	regclosekey(handle)

END
ELSE SAY "Can't open the key:" err




/* =======================================================
 * Querying a string value
 * =======================================================
 */

/* Register RegQueryValueEx again, but define it for easily
 * querying a nul-terminated string value. Note: We set
 * the str stor to hold 260 bytes. You should make it as
 * large as the largest string value you expect to read from
 *  the registry
 */
FUNCDEF("RegQueryString", "32, void, str, void, 32 *, str[260] stor, 32 * dual", 'advapi32', 'RegQueryValueEx')

/* Open the "HKEY_CURRENT_USER\Software\Reginald" key for reading */
err = regopenkeyex(2147483649, "Software\Reginald", , 131097, handle) 
IF err == 0 THEN DO

	/* Read the string value "OPTIONS" */
	valuetype = 1  /* String type */
	myvaluesize = 260
	err = regquerystring(handle, "OPTIONS", , valuetype, myvalue, myvaluesize)
	IF err \== 0 THEN SAY "Error reading string value:" err
	ELSE SAY "String =" myvalue

	/* Close the key */
	CALL regclosekey(handle)

END
ELSE SAY "Can't open the key:" err




/* =======================================================
 * Create a numeric (DWORD) value
 * =======================================================
 */

/* Register RegSetValueEx, defined to easily
 * save a numeric value (in Intel format)
 */
FUNCDEF("RegWriteNumber", "32, void, str, 32u, 32u, 32 *, 32u", 'advapi32', 'RegSetValueEx')

/* Open the "HKEY_CURRENT_USER\Software\Reginald" key for reading/writing, and create it if it doesn't exist */
err = regcreatekeyex(2147483649, "Software\Reginald", , , , 983103, , handle, result) 
IF err == 0 THEN DO

	/* Write the numeric value "100" to the registry file "MyValue" as a binary DWORD */
	myvalue = 100
	err = regwritenumber(handle, "MyValue", , 4, myvalue, 4)
	IF err \== 0 THEN SAY "Error writing numeric value:" err

	/* Close the key */
	regclosekey(handle)

END
ELSE SAY "Can't open/create the key:" err




/* =======================================================
 * Querying a numeric value
 * =======================================================
 */

/* Register RegQueryValueEx again, but define it for easily
 * querying a numeric value (in Intel format)
 */
FUNCDEF("RegQueryNumber", "32, void, str, void, 32 *, 32 * stor, 32 * dual", 'advapi32', 'RegQueryValueEx')

/* Open the "HKEY_CURRENT_USER\Software\Reginald" key for reading */
err = regopenkeyex(2147483649, "Software\Reginald", , 131097, handle) 
IF err == 0 THEN DO

	/* Read the numeric value "Digits" */
	valuetype = 4  /* Intel number type */
	myvaluesize = 4
	err = regquerynumber(handle, "Digits", , valuetype, myvalue, myvaluesize)
	IF err \== 0 THEN SAY "Error reading numeric value:" err
	ELSE SAY "Number =" myvalue

	/* Close the key */
	regclosekey(handle)

END
ELSE SAY "Can't open the key:" err







RETURN


/* If any of the FUNCDEF statements fail, it will jump to here. */
CATCH FAILURE
	CONDITION("M")
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Thu 2017-6-22  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2013-06-18 23:35:16