Announcement

Collapse
No announcement yet.

Registry problem setting a policy in HKEY_LOCAL_MACHINE

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Registry problem setting a policy in HKEY_LOCAL_MACHINE

    Im having problems with RegSetValueEx, it is returing error 6 (ERROR_INVALID_HANDLE), but im using the (valid!!) handle that RegCreateKeyEx is returning!! (lRetVal), i am correctly registering my permissions, with AdjustTokenPrivileges... so im clueless.

    As the name of the topic says, it only happens with HKEY_LOCAL_MACHINE,
    and it doesnt show with HKEY_CURRENT_USER.

    Any ideas?

    Code:
    '------------------------------------------------------------------------------------------------------------------------
    Function GetLogoffState() As Long 
    ON ERROR RESUME NEXT
    DIM RetCode AS LONG
    Dim hKey As Long
    Local Value As Dword
         
         RetCode = RegOpenKeyEx(%HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Policies\Explorer", 0&, %KEY_READ, hKey)
         
         If RetCode = %ERROR_SUCCESS Then       
           'SetLastError(0)       
           RetCode = RegQueryValueEx(ByVal hkey, "NoLogoff", ByVal 0, %REG_DWORD, Value, 4)       
           'MsgBox Systemerrormessagetext(RetCode)         
           Function = Value                                                                         
           Exit Function
         END IF
         Function = 0
    End Function
    
    '------------------------------------------------------------------------------------------------------------------------
    Function ManageLogOff(CState As Dword) As Long 
    
      Local lKey As Long, lSubKey As Asciiz * 128, lKeyHandle As Long, lRetVal As Dword, lDisPos As Long  
      Local RData As String, lRet As Long  
      lKey          = %HKEY_LOCAL_MACHINE   
      lSubKey       = "SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer"                  
                       
      SetLastError(0)
      lKeyHandle    = RegCreateKeyEx (lKey, lSubKey, 0, "", %REG_OPTION_NON_VOLATILE, %KEY_WRITE, _                                  
      ByVal %Null, lRetVal, lDisPos)
      MsgBox Systemerrormessagetext(lRet)    '<---- No errors here.
     
      If Cstate Then 
        RData   = MkDwd$(1) & Chr$(0)
      Else
        RData   = MkDwd$(0) & Chr$(0)        
      End If  
      
      SetLastError(0)
      
      lRet = RegSetValueEx (lRetVal, "NoLogoff", %Null, %REG_DWORD, ByVal StrPtr(RData), Len(RData))        
       
      MsgBox Systemerrormessagetext(lRet)  '<--- Error here. refuses a handle created with a process that reported no errors...
      
      RegCloseKey lRetVal
      
    End Function

  • #2
    Code:
    lKeyHandle    = RegCreateKeyEx (lKey, lSubKey, 0, "", %REG_OPTION_NON_VOLATILE, %KEY_WRITE, _                                  
      ByVal %Null, lRetVal, lDisPos)
      MsgBox Systemerrormessagetext(lRet)    '<---- No errors here.
    That's because 'lret' was never set, meaning it equals zero (ERROR_SUCCESS).

    I think here you wanted the message associated with the return from RegCreateKEyEx, which is 'lkeyhandle' (which BTW is a TERRIBLE name for this variable... it's not a key, a handle OR a 'key handle' )


    MCM
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      I knew it was going to be that simple!! Thanx Michael.

      Comment

      Working...
      X