473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Load registry hive (AdjustTokenPrivileges error)

Writing an app in VB.NET 2005 which needs to load a registry hive. As far as
I can determine, there isn't any managed code to do this (sigh), so I need to
revert to APIs. Additional privileges need to be added to the current
process token before RegLoadKey can be used successfully. This exception is
thrown when I call AdjustTokenPrivileges: "The parameter is incorrect.
(Exception from HRESULT: 0x80070057 (E_INVALIDARG))".

I'm unable to determine which parameter is incorrent and have tried lots of
things with my API declaration and stuff. Any idea, hints, pointers or
solutions would be much appreciated. Thanks.

Code extract:
----------------
' constants
Public Const TOKEN_ADJUST_PRIVLEGES = &H20
Public Const TOKEN_QUERY = &H8
Public Const SE_PRIVILEGE_ENABLED = &H2
Public Const HKEY_USERS = &H80000003
Public Const SE_RESTORE_NAME = "SeRestorePrivilege"
Public Const SE_BACKUP_NAME = "SeBackupPrivilege"
Public Const ANYSIZE_ARRAY As Int32 = 1

' structures
<StructLayout(LayoutKind.Sequential)_
Public Structure TOKEN_PRIVILEGES
Public PrivilegeCount As Int32
Public Privileges() As LUID_AND_ATTRIBUTES
End Structure

<StructLayout(LayoutKind.Sequential)_
Public Structure LUID
Public LowPart As Int32
Public HighPart As Int32
End Structure

<StructLayout(LayoutKind.Sequential)_
Public Structure LUID_AND_ATTRIBUTES
Public pLuid As LUID
Public Attributes As Int32
End Structure
' API Declarations
<DllImport("kernel32.dll", SetLastError:=True)_
Public Function FormatMessage(ByVal dwFlags As Integer, ByRef lpSource
As IntPtr, _
ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByRef
lpBuffer As [String], _
ByVal nSize As Integer, ByRef Arguments As IntPtr) As Integer
End Function

<DllImport("advapi32.dll", EntryPoint:="RegLoadKeyA",
SetLastError:=True)_
Public Function RegLoadKey(ByVal hKey As Int32, ByVal lpSubKey As
String, ByVal lpFile As String) As Int32
End Function

<DllImport("advapi32.dll", EntryPoint:="RegUnLoadKeyA",
SetLastError:=True)_
Public Function RegUnLoadKey(ByVal hKey As Int32, ByVal lpSubKey As
String) As Int32
End Function

<DllImport("kernel32.dll", SetLastError:=True)_
Public Function GetCurrentProcess() As IntPtr
End Function

Public Declare Function OpenProcessToken Lib "advapi32.dll" _
Alias "OpenProcessToken" _
(ByVal ProcessHandle As Integer, _
ByVal DesiredAccess As Integer, _
ByRef TokenHandle As IntPtr) As Integer

Dim Retval As Long
Dim strKeyName As String
Dim MyToken As IntPtr
Dim TP As TOKEN_PRIVILEGES
Dim RestoreLuid As LUID
Dim BackupLuid As LUID
Dim procHandle As IntPtr = GetCurrentProcess()

Retval = OpenProcessToken(procHandle, TOKEN_ADJUST_PRIVLEGES _
Or TOKEN_QUERY, MyToken)
If Retval = 0 Then MsgBox("OpenProcess: " &
GetErrorMessage(Err.LastDllError))

retval = LookupPrivilegeValue(vbNullString, SE_RESTORE_NAME, _
RestoreLuid)
If retval = 0 Then MsgBox("LookupPrivileges: " & Err.LastDllError)

retval = LookupPrivilegeValue(vbNullString, SE_BACKUP_NAME,
BackupLuid)
If retval = 0 Then MsgBox("LookupPrivileges: " & retval)

TP.PrivilegeCount = 2
ReDim TP.Privileges(1)
TP.Privileges(0).pLuid = RestoreLuid
TP.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
TP.Privileges(1).pLuid = BackupLuid
TP.Privileges(1).Attributes = SE_PRIVILEGE_ENABLED

Retval = AdjustTokenPrivileges(MyToken, False, TP, 0, Nothing,
Nothing) '***** ERROR OCCURS HERE *****
If retval = 0 Then MsgBox("AdjustTokenPrivileges: " &
Err.LastDllError)
Retval = RegLoadKey(RegistryHive.Users, "TempHive", "C:\Documents
and Settings\Default User\NTUSER.DAT")
If retval <0 Then
Dim strErrorMessage As String = GetErrorMessage(retval)
MsgBox(strErrorMessage)
End If

<DllImport("advapi32.dll", EntryPoint:="LookupPrivilegeValueA",
SetLastError:=True)_
Public Function LookupPrivilegeValue(ByVal lpSystemName As String, _
ByVal lpName As String, ByRef lpLuid As LUID) As Long
End Function

<DllImport("advapi32.dll", SetLastError:=True)_
Public Function AdjustTokenPrivileges(ByRef TokenHandle As IntPtr, _
<MarshalAs(UnmanagedType.Bool)ByVal DisableAllPrivileges As
Boolean, _
ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Long)
As Long
End Function

Feb 16 '07 #1
0 2050

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian Campbell | last post by:
Gretings... I have written a Windows Service that accesses a Web Service. Both run fine on my development machine.... After installing both the Windows Service and Web Service on my staging...
10
by: Clint | last post by:
Hey all - I'm having a really confusing problem concerning a web service. Right now, I have an application that needs to call a web service that does nothing but return "true" (this will...
21
by: Kevin Swanson | last post by:
I'm attempting some remote registry manipulation via C#. I've written a test app to simply grab a specified key from a specified hive on a specified machine. The call to OpenSubKey is throwing...
3
by: techmaccoy | last post by:
I am trying to access a entry under HKEY_Current_USER/Software. In My code it List seven subkeys under there. Actually there are lot more and the entry i wanted to access in seen in the Registry,...
6
by: stephen coleman | last post by:
Hi all and thanks for reading this. I can read anywhere in the registry, but I can't write to the local machine (XP Pro, I am not the admin.). My application needs to write to the registry the...
8
by: Shawn B. | last post by:
Greetings, I'm creating a little program that acts as a glorified Registry explorer. The TreeView doesn't allow you to show a plus sign unless a node has child nodes, so I need to determine...
3
by: Ahmad Jalil Qarshi | last post by:
Hi! I am developing an application in C# as a windows NT Service. This application needs to check for eventlog using EventLog.Exists("System") But unfortunately it generates exception...
6
by: vovan | last post by:
I'm asking for help one more time. Below are 2 procedures. The first one is used in VB 2005 Windows Form project. It works fine on XP, Vista. Private Sub RegistryReadingTest() Try Dim f As New...
11
by: Unknown Hero | last post by:
Tim Golden wrote: The first link which points to the Python documentation for the _winreg module I already checked, even before coming here. I am wondering how I should do the loop I need (go...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.