Sign in to follow this  
Followers 0
erik7426

Copy Registry Key/Values in HKCU to Another User

7 posts in this topic

I am familiar with the basic usage of RegRead and RegWrite. I also have a UDF that I got from somewhere in the Forums that enables me to write a specific registry key to all users which I have used multiple times in the past.

Here is what I need help with...

The registry key in question is HKCUSoftwareAdobeAcrobat Reader11.0GeneralcPrintAsImage

This key can have multiple values. I need to read those values into variables so that I can use them in the RegWrite command.

 

I'm sure there's a better way to get the data, but the following code finds the "Name" and the "Data" of the values located in the key...

#include <Array.au3>

Dim $myArray[9]
Dim $myVals[9]

$myKey = "HKCU\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage"

For $j = 1 To 10
    Local $myVar = RegEnumVal($myKey, $j)
    If @error <> 0 Then ExitLoop
    _ArrayInsert($myArray, $j - 1, $myVar)
Next

_ArrayDisplay($myArray)

For $k = 0 To 9
    $testing = RegRead($myKey, $myArray[$k])
    If @error <> 0 Then ExitLoop
    _ArrayInsert($myVals, $k, $testing)
Next

_ArrayDisplay($myVals)

Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites



HKU has access to all user hives listed by SID.

To match a SID to a user profile, you can use something like this:

#include <Array.au3>
Local $aUsers[100][2]
Local $i = 1
While 1
    $aUsers[$i - 1][0] = RegEnumKey("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", $i)
    If @error Then ExitLoop
    $aUsers[$i - 1][1] = RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & $aUsers[$i - 1][0], "ProfileImagePath")
    $i += 1
WEnd
ReDim $aUsers[$i - 1][2]
_ArrayDisplay($aUsers, "User SID to user profile", -1, 0, "", "|", "#|SID|Profile")

Share this post


Link to post
Share on other sites

 

HKU has access to all user hives listed by SID.

To match a SID to a user profile, you can use something like this:

#include <Array.au3>
Local $aUsers[100][2]
Local $i = 1
While 1
    $aUsers[$i - 1][0] = RegEnumKey("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", $i)
    If @error Then ExitLoop
    $aUsers[$i - 1][1] = RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & $aUsers[$i - 1][0], "ProfileImagePath")
    $i += 1
WEnd
ReDim $aUsers[$i - 1][2]
_ArrayDisplay($aUsers, "User SID to user profile", -1, 0, "", "|", "#|SID|Profile")

 

Thanks for the reply, but I already have a solution for writing the key(s) to other users. I need help with getting the data gathered with RegRead into a usable format for RegWrite...

Share this post


Link to post
Share on other sites

It is the same concept:

#include <Array.au3>
Local $aUsers[100][100]
Local $i = 1, $i2 = 1, $iMax = 1
While 1
    $aUsers[$i - 1][0] = RegEnumKey("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", $i)
    If @error Then ExitLoop
    $aUsers[$i - 1][1] = RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & $aUsers[$i - 1][0], "ProfileImagePath")
    $i2 = 1
    $sTemp = ""
    While 1
        $aUsers[$i - 1][$i2 + 1] = RegEnumVal("HKU\" & $aUsers[$i - 1][0] & "\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage", $i2)
        If @error Then ExitLoop
        $aUsers[$i - 1][$i2 + 1] &= "=" & RegRead("HKU\" & $aUsers[$i - 1][0] & "\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage", $aUsers[$i - 1][$i2 + 1])
        $i2 += 1
        If $i2 > $iMax Then $iMax = $i2
    WEnd
    $i += 1
WEnd
ReDim $aUsers[$i - 1][$iMax + 1]
_ArrayDisplay($aUsers)

 

Once you have the values, I don't see the issue... why can't you just regwrite them back to the user of your choice?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Here, this includes a function to add a printer to a user for cPrintAsImage.

It will make sure the user exists, the printer isn't already added, and will find the next available t# value name to use:

The bit after the function is from what I posted above just showing that the data was added as expected.  Hope this helps, as I'm still not entirely sure where you were running in to a problem.

#include <Array.au3>

Local $sMsg, $sTitle, $iIcon
;_AddcPrintAsImage(<USER>, <Printer>)
_AddcPrintAsImage("JoeBlow", "Test Printer 4")

If @error Then
    $iIcon = 16
    $sTitle = "Error"
    If @error = 1 Then $sMsg = "User not found"
    If @error = 2 Then $sMsg = "Printer already exists"
    If @error = 3 Then $sMsg = "Error writing to registry.  RegWrite @error code: " & @extended
Else
    $iIcon = 64
    $sTitle = "Success"
    $sMsg = "Successfully added printer to cPrintAsImage for the specified user"
EndIf
MsgBox($iIcon, $sTitle, $sMsg)


Func _AddcPrintAsImage($sUser, $sPrinter)
    ;Enum SIDs and user profiles
    Local $aUsers[100][2], $sVal
    Local $i = 1
    While 1
        $aUsers[$i - 1][0] = RegEnumKey("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", $i)
        If @error Then ExitLoop
        $aUsers[$i - 1][1] = RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & $aUsers[$i - 1][0], "ProfileImagePath")
        $i += 1
    WEnd
    ReDim $aUsers[$i - 1][2]

    For $a = 1 To UBound($aUsers)
        ;Look for user (Quick example is simply using StringInStr)
        If StringInStr($aUsers[$a - 1][1], $sUser) <> 0 Then
            $i = 0
            ;Find next available t#
            While 1
                $sVal = RegRead("HKU\" & $aUsers[$a - 1][0] & "\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage", "t" & $i)
                If @error Then ExitLoop
                ;Check if printer is already added for this user
                If $sVal = $sPrinter Then Return SetError(2, 0, 0)
                $i += 1
            WEnd
            RegWrite("HKU\" & $aUsers[$a - 1][0] & "\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage", "t" & $i, "REG_SZ", $sPrinter)
            If @error Then
                Return SetError(3, @error, 0)
            Else
                ;Success
                Return 1
            EndIf
        EndIf
    Next
    ;User was not found
    Return SetError(1, 0, 0)
EndFunc   ;==>_AddcPrintAsImage


;Display all users security IDs with all values for all instances of cPrintAsImage
Local $aUsers[100][100]
Local $i = 1, $i2 = 1, $iMax = 1
While 1
    $aUsers[$i - 1][0] = RegEnumKey("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", $i)
    If @error Then ExitLoop
    $aUsers[$i - 1][1] = RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & $aUsers[$i - 1][0], "ProfileImagePath")
    $i2 = 1
    While 1
        $aUsers[$i - 1][$i2 + 1] = RegEnumVal("HKU\" & $aUsers[$i - 1][0] & "\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage", $i2)
        If @error Then ExitLoop
        $aUsers[$i - 1][$i2 + 1] &= "=" & RegRead("HKU\" & $aUsers[$i - 1][0] & "\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage", $aUsers[$i - 1][$i2 + 1])
        $i2 += 1
        If $i2 > $iMax Then $iMax = $i2
    WEnd
    $i += 1
WEnd
ReDim $aUsers[$i - 1][$iMax + 1]
_ArrayDisplay($aUsers)
Edited by danwilli

Share this post


Link to post
Share on other sites

 

Here, this includes a function to add a printer to a user for cPrintAsImage.

It will make sure the user exists, the printer isn't already added, and will find the next available t# value name to use:

The bit after the function is from what I posted above just showing that the data was added as expected.  Hope this helps, as I'm still not entirely sure where you were running in to a problem.

#include <Array.au3>

Local $sMsg, $sTitle, $iIcon
;_AddcPrintAsImage(<USER>, <Printer>)
_AddcPrintAsImage("JoeBlow", "Test Printer 4")

If @error Then
    $iIcon = 16
    $sTitle = "Error"
    If @error = 1 Then $sMsg = "User not found"
    If @error = 2 Then $sMsg = "Printer already exists"
    If @error = 3 Then $sMsg = "Error writing to registry.  RegWrite @error code: " & @extended
Else
    $iIcon = 64
    $sTitle = "Success"
    $sMsg = "Successfully added printer to cPrintAsImage for the specified user"
EndIf
MsgBox($iIcon, $sTitle, $sMsg)


Func _AddcPrintAsImage($sUser, $sPrinter)
    ;Enum SIDs and user profiles
    Local $aUsers[100][2], $sVal
    Local $i = 1
    While 1
        $aUsers[$i - 1][0] = RegEnumKey("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", $i)
        If @error Then ExitLoop
        $aUsers[$i - 1][1] = RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & $aUsers[$i - 1][0], "ProfileImagePath")
        $i += 1
    WEnd
    ReDim $aUsers[$i - 1][2]

    For $a = 1 To UBound($aUsers)
        ;Look for user (Quick example is simply using StringInStr)
        If StringInStr($aUsers[$a - 1][1], $sUser) <> 0 Then
            $i = 0
            ;Find next available t#
            While 1
                $sVal = RegRead("HKU\" & $aUsers[$a - 1][0] & "\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage", "t" & $i)
                If @error Then ExitLoop
                ;Check if printer is already added for this user
                If $sVal = $sPrinter Then Return SetError(2, 0, 0)
                $i += 1
            WEnd
            RegWrite("HKU\" & $aUsers[$a - 1][0] & "\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage", "t" & $i, "REG_SZ", $sPrinter)
            If @error Then
                Return SetError(3, @error, 0)
            Else
                ;Success
                Return 1
            EndIf
        EndIf
    Next
    ;User was not found
    Return SetError(1, 0, 0)
EndFunc   ;==>_AddcPrintAsImage


;Display all users security IDs with all values for all instances of cPrintAsImage
Local $aUsers[100][100]
Local $i = 1, $i2 = 1, $iMax = 1
While 1
    $aUsers[$i - 1][0] = RegEnumKey("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", $i)
    If @error Then ExitLoop
    $aUsers[$i - 1][1] = RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & $aUsers[$i - 1][0], "ProfileImagePath")
    $i2 = 1
    While 1
        $aUsers[$i - 1][$i2 + 1] = RegEnumVal("HKU\" & $aUsers[$i - 1][0] & "\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage", $i2)
        If @error Then ExitLoop
        $aUsers[$i - 1][$i2 + 1] &= "=" & RegRead("HKU\" & $aUsers[$i - 1][0] & "\Software\Adobe\Acrobat Reader\11.0\General\cPrintAsImage", $aUsers[$i - 1][$i2 + 1])
        $i2 += 1
        If $i2 > $iMax Then $iMax = $i2
    WEnd
    $i += 1
WEnd
ReDim $aUsers[$i - 1][$iMax + 1]
_ArrayDisplay($aUsers)

 

Thanks, I'll give it a try. I noticed in your RegWrite statement you are manually specifying the REG_SZ.  I was approaching from the standpoint where I could use this in other applications where the value type might not be known REG_SZ, REG_DWORD, etc...

Share this post


Link to post
Share on other sites

Thanks, I'll give it a try. I noticed in your RegWrite statement you are manually specifying the REG_SZ.  I was approaching from the standpoint where I could use this in other applications where the value type might not be known REG_SZ, REG_DWORD, etc...

 

Here is a quick example of using @extended to determine the type of the value without having to manually specify it. Just whipped up the following function to copy a value in a key, to another key, without specifying the type.  I'm sure this has been done before, but I hope it helps.

#include <Constants.au3>
_RegCopy("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir", "HKCU\SOFTWARE\TESTING\Test")
If @error Then MsgBox(0,'Error', @error)

Func _RegCopy($sKey, $sValueName, $sNewKey)
    Local $sValue = RegRead($sKey, $sValueName)
    If @error Then Return SetError(1, @error, 0)
    Local $sType = @extended
    Switch $sType
        Case $REG_SZ
            $sType = "REG_SZ"
        Case $REG_EXPAND_SZ
            $sType = "REG_EXPAND_SZ"
        Case $REG_BINARY
            $sType = "REG_BINARY"
        Case $REG_DWORD
            $sType = "REG_DWORD"
        Case $REG_MULTI_SZ
            $sType = "REG_MULTI_SZ"
        Case Else
            Return SetError(2, $sType, 0)
    EndSwitch
    RegWrite($sNewKey, $sValueName, $sType, $sValue)
    If @error Then Return SetError(3, @error, 0)
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0