-
APIs y vb
Estoy haciendo un programa k no importa para k sirve, el hecho es k guardo las opciones en el registro y para eso uso apis, para manejar el registro.
Todo funciona bien en win98 pero cuando lo prove en winXP la api "RegSetValueEx" me devuelve un valor 5, k no es error_success y por lo tanto no hace lo k debiera, escribir un valor.
Tambien prove otras como "RegOpenKeyEx", "RegEnumValue" y otras mas y funcionan bien.
Pero "RegSetValueEx" me devuelve 5, para saber k era el 5 use FormatMessage pero me daba una descripcion como que habia sido bien ejecutada la llmada a la api.
Por k puede ser k me da un 5 el valor de "RegSetValueEx". Por k pasa solo en winXP ?
chau
-
el problema es k el xp es nt, creo k hay otra api para eso, k funciona con permisos de usuario. si no encuentras nada enviame privado pa recordarmelo y te lo miro en casa.
-
Ya se que el post es muy viejo, pero encontre la solucion, sin buscarla (como casi siempre).
Y si alguien tiene el mismo problema bueno ya sabe como solucionarlo.
La api a la que hacia mencion NeoGenessis es AdjustTokenPrivileges, solo funciona en en windows NT o de la familia (2000, xp, 2003, etc)
Esta funcion se encarga de modificar los privilegios de seguridad con que se ejecuta la aplicacion, dando permiso a que pueda modificar el registro, apagar el sistema y otras cosas para las que se necesiten privilegios especiales.
Esta es la declaracion:
Declare Function AdjustTokenPrivileges Lib "advapi32.dll" Alias "AdjustTokenPrivileges" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Parametros:
· TokenHandle
Identifies the access token that contains the privileges to be modified.
· DisableAllPrivileges
Specifies whether the function disables all of the token’s privileges. If this value is TRUE, the function disables all privileges and ignores the NewState parameter. If it is FALSE, the function modifies privileges based on the information pointed to by the NewState parameter.
· NewState
Pointer to a TOKEN_PRIVILEGES structure that specifies an array of privileges and their attributes. If the DisableAllPrivileges parameter is FALSE, AdjustTokenPrivileges enables or disables these privileges for the token. If you set the SE_PRIVILEGE_ENABLED attribute for a privilege, the function enables that privilege; otherwise, it disables the privilege.
If DisableAllPrivileges is TRUE, the function ignores this parameter.
· BufferLength
Specifies the size, in bytes, of the buffer pointed to by the PreviousState parameter. This parameter can be NULL if the PreviousState parameter is NULL.
· PreviousState
Pointer to a buffer that the function fills with a TOKEN_PRIVILEGES structure containing the previous state of any privileges the function modifies. The token must be open for TOKEN_QUERY access to use this parameter. This parameter can be NULL.
If you specify a buffer that is too small to receive the complete list of modified privileges, the function fails and does not adjust any privileges. In this case, the function sets the variable pointed to by the ReturnLength parameter to the number of bytes required to hold the complete list of modified privileges.
· ReturnLength
Pointer to a variable that receives the required size, in bytes, of the buffer pointed to by the PreviousState parameter. This parameter can be NULL if PreviousState is NULL.
Y este es un codigo que la usa, usa apis para manejar el registro:
Código:
'example by Scott Watters ([email protected])
' No rhyme or reason for making some private and some public. Use your own discretion...
Const HKEY_CURRENT_USER = &H80000001
Const TOKEN_QUERY As Long = &H8&
Const TOKEN_ADJUST_PRIVILEGES As Long = &H20&
Const SE_PRIVILEGE_ENABLED As Long = &H2
Const SE_RESTORE_NAME = "SeRestorePrivilege" 'Important for what we're trying to accomplish
Const SE_BACKUP_NAME = "SeBackupPrivilege"
Const REG_FORCE_RESTORE As Long = 8& ' Almost as import, will allow you to restore over a key while it's open!
Const READ_CONTROL = &H20000
Const SYNCHRONIZE = &H100000
Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Const STANDARD_RIGHTS_ALL = &H1F0000
Const SPECIFIC_RIGHTS_ALL = &HFFFF
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_CREATE_LINK = &H20
Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Private Type LUID
lowpart As Long
highpart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges As LUID_AND_ATTRIBUTES
End Type
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long ' Always close your keys when you're done with them!
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long ' Need to open the key to be able to restore to it.
Private Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hKey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As Long ' Main function
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPriv As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long 'Used to adjust your program's security privileges, can't restore without it!
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As Any, ByVal lpName As String, lpLuid As LUID) As Long 'Returns a valid LUID which is important when making security changes in NT.
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, lpSecurityAttributes As Any) As Long
Function EnablePrivilege(seName As String) As Boolean
Dim p_lngRtn As Long
Dim p_lngToken As Long
Dim p_lngBufferLen As Long
Dim p_typLUID As LUID
Dim p_typTokenPriv As TOKEN_PRIVILEGES
Dim p_typPrevTokenPriv As TOKEN_PRIVILEGES
p_lngRtn = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, p_lngToken)
If p_lngRtn = 0 Then
Exit Function ' Failed
ElseIf Err.LastDllError <> 0 Then
Exit Function ' Failed
End If
p_lngRtn = LookupPrivilegeValue(0&, seName, p_typLUID) 'Used to look up privileges LUID.
If p_lngRtn = 0 Then
Exit Function ' Failed
End If
' Set it up to adjust the program's security privilege.
p_typTokenPriv.PrivilegeCount = 1
p_typTokenPriv.Privileges.Attributes = SE_PRIVILEGE_ENABLED
p_typTokenPriv.Privileges.pLuid = p_typLUID
EnablePrivilege = (AdjustTokenPrivileges(p_lngToken, False, p_typTokenPriv, Len(p_typPrevTokenPriv), p_typPrevTokenPriv, p_lngBufferLen) <> 0)
End Function
Public Function RestoreKey(ByVal sKeyName As String, ByVal sFileName As String, lPredefinedKey As Long) As Boolean
If EnablePrivilege(SE_RESTORE_NAME) = False Then Exit Function
Dim hKey As Long, lRetVal As Long
Call RegOpenKeyEx(lPredefinedKey, sKeyName, 0&, KEY_ALL_ACCESS, hKey) ' Must open key to restore it
'The file it's restoring from was created using the RegSaveKey function
Call RegRestoreKey(hKey, sFileName, REG_FORCE_RESTORE)
RegCloseKey hKey ' Don't want to keep the key ope. It causes problems.
End Function
Public Function SaveKey(ByVal sKeyName As String, ByVal sFileName As String, lPredefinedKey As Long) As Boolean
If EnablePrivilege(SE_BACKUP_NAME) = False Then Exit Function
Dim hKey As Long, lRetVal As Long
Call RegOpenKeyEx(lPredefinedKey, sKeyName, 0&, KEY_ALL_ACCESS, hKey) ' Must open key to save it
'Don't forget to "KILL" any existing files before trying to save the registry key!
If Dir(sFileName) <> "" Then Kill sFileName
Call RegSaveKey(hKey, sFileName, ByVal 0&)
RegCloseKey hKey ' Don't want to keep the key ope. It causes problems.
End Function
Private Sub Form_Load()
Const sFile = "c:\test.reg"
SaveKey "SOFTWARE\KPD-Team\API-Guide", sFile, HKEY_CURRENT_USER
RestoreKey "SOFTWARE\KPD-Team\API-Guide", sFile, HKEY_CURRENT_USER
End Sub
El codigo y la declaracion, como la explicacion de los parametros son de Api-Guide. www.allapi.net
Espero que les sea util.
Chau saludos.