Software / Reginald / Examples / netuser.rex

Search 搜索
Home Home

/* netuser.rex
 * Demonstrates how to get some information about the "Administrator"
 * account on the local computer. We use Reginald REXX's FUNCDEF
 * feature to call some Windows' APIs.


/* Register the Windows' function NetUserGetInfo() and NetApiBufferFree() */
FUNCDEF("NetUserGetInfo", "32, wstr, wstr, 32u, void stor", "Netapi32")
FUNCDEF("NetApiBufferFree", "32, void", "Netapi32")

/* Register a USER_INFO_3 structure. This is what we will ask NetUserGetInfo()
 * to return. And we will use CONVERTDATA to transfer the data in the structure
 * to/from our own REXX stem variable.
FUNCDEF("USER_INFO_3", "wstr *, wstr *, 32u, 32u, wstr *, wstr *, 32u, wstr *, 32u, wstr *, wstr *, wstr *, wstr *, 32u, 32u, 32u, 32u, 32u, char *, 32u, 32u, wstr *, 32u, 32u, 32u, 32u, wstr *, wstr *, 32u")

/* Call NetUserGetInfo() to retrieve the desired information about the
 * desired account.
 * The first arg is the name of the server that the account is on. If
 * you omit this arg, then the local computer is used. Otherwise, it
 * should be the name of the server prefaced with "\\" such as
 * "\\MyServer". NOTE: This must be a UNICODE string if specified.
 * The second arg is the name of the account. Here we pass the string
 * "Administrator" for that particular account. But note that this
 * string needs to be UNICODE (ie, "wstr" FUNCDEF type) and REXX
 * strings are ANSI. So we pass our REXX string to CONVERTSTR() to
 * convert it to UNICODE when we pass it to NetUserGetInfo(). We
 * also want it nul-terminated.
 * The third arg is the level of information we need. By passing a 10,
 * we tell NetUserGetInfo() to allocate and return a structure that
 * has information about the account name, the password, and other
 * information about the account.
 * The fourth arg is the name of a REXX Variable which will be set to
 * the address of the information that NetUserGetInfo() returns. We
 * will need to use CONVERTDATA to stuff this info into some REXX
 * compound variable. Here we let Reginald store the address in a
 * variable named "Address".
 * NetUserGetInfo() returns 0 if success, or some error number if a
 * failure.
status = netusergetinfo(, convertstr("Administrator", 'Z'), 3, address)

/* If the call succeeds, print the user information */
IF status == 0 THEN DO

   /* Stuff the returned information (USER_INFO_3 structure) into our
    * own REXX stem variable named MyInfo

   /* Free the information returned by NetUserGetInfo() now that we've
    * transfered it to our own REXX variable

   /* Display the information. NOTE: Some of the information is in
    * UNICODE string format. But REXX deals only with ANSI strings,
    * so when we SAY them, we use CONVERTSTR to get an ANSI version.
    * For some string fields, NetUserGetInfo() may nul out the field
    * if it has no info to return. You can check if a field has any
    * value by using the EXISTS() function, such as:
    * IF EXISTS('MyInfo.1') THEN SAY "Account name is" MyInfo.1
    * ELSE SAY "There was no Account name returned."
    * But since we only want to display the strings, and don't
    * care if any fields aren't returned, we'll use the 'V'
    * flag with CONVERTSTR, and pass the quoted variable name.
    * In this way, if the field happens to be nul'ed out, then
    * CONVERTSTR will substitute an empty string.
   SAY "09"x || "Account:     " convertstr('MyInfo.1', 'UV')
   SAY "09"x || "Password:    " convertstr('MyInfo.2', 'UV')
   SAY "09"x || "Password age:" myinfo.3
   SAY "09"x || "Priv:        " myinfo.4
   SAY "09"x || "Home dir:    " convertstr('MyInfo.5', 'UV')
   SAY "09"x || "Comment:     " convertstr('MyInfo.6', 'UV')
   SAY "09"x || "Flags:       " myinfo.7
   SAY "09"x || "Script path: " convertstr('MyInfo.8', 'UV')
   SAY "09"x || "Auth. Flags: " myinfo.9
   SAY "09"x || "Full name:   " convertstr('MyInfo.10', 'UV')
   SAY "09"x || "user comment:" convertstr('MyInfo.11', 'UV')
   SAY "09"x || "Parms:       " convertstr('MyInfo.12', 'UV')
   SAY "09"x || "Workstations:" convertstr('MyInfo.13', 'UV')
   SAY "09"x || "Last logon:  " myinfo.14
   SAY "09"x || "Last logoff: " myinfo.15
   SAY "09"x || "Expires:     " myinfo.16
   SAY "09"x || "Max storage: " myinfo.17
   SAY "09"x || "Weekly units:" myinfo.18
   SAY "09"x || "Logon hours: " myinfo.19
   SAY "09"x || "PW count:    " myinfo.20
   SAY "09"x || "Num logons:  " myinfo.21
   SAY "09"x || "Logon server:" convertstr('MyInfo.22', 'UV')
   SAY "09"x || "Country code:" myinfo.23
   SAY "09"x || "Code page:   " myinfo.24
   SAY "09"x || "User ID:     " myinfo.25
   SAY "09"x || "Group ID:    " myinfo.26
   SAY "09"x || "Profile:     " convertstr('MyInfo.27', 'UV')
   SAY "09"x || "Home drive:  " convertstr('MyInfo.28', 'UV')
   SAY "09"x || "Password exp:" myinfo.29

/* An error. Display an error message */
   SELECT status
	WHEN 5 THEN str = "This script is being run under an account that has insufficient privilege for this operation."
	WHEN 50 THEN str = "This network request isn't supported."
	WHEN 53 THEN str = "The network path wasn't found."
	WHEN 65 THEN str = "Network access is denied."
	WHEN 124 THEN str = "Invalid level parameter."
	WHEN 234 THEN str = "More data available. Supplied buffer too small."
	WHEN 2102 THEN str = "Device driver not installed."
	WHEN 2106 THEN str = "This operation can be performed only on a server."
	WHEN 2123 THEN str = "Buffer too small for fixed-length data."
	WHEN 2138 THEN str = "The Workstation service isn't started."
	WHEN 2141 THEN str = "The server isn't configured for this transaction; IPC$ is not shared."
	WHEN 2219 THEN str = "LAN Manager can't find the user accounts database file accounts.lmx."
	WHEN 2221 THEN str = "Account not found."
	WHEN 2227 THEN str = "Server not running in user-level security."
	WHEN 2229 THEN str = "Error encountered in accessing accounts database."
	WHEN 2247 THEN str = "The user accounts database file accounts.lmx is corrupted."
	WHEN 2351 THEN str = "Invalid server name specified."
		str = "Unknown error."

   SAY "NetUserGetInfo() ERROR" status || ':' str

© Thu 2017-6-22  Guidance Laboratory Inc. Hits:0 Last modified:2013-06-18 23:35:12