PDA

View Full Version : VB API Information


sage45
07-09-2003, 12:22 AM
OK, we don't have a forum for this so I thought I'd post it here and see what you guys can come up with...

Currently I am trying to find a way to load a profile on a local workstation for a local application... This application is a VB application...

In looking through the different API's I have stumbled across the various functions available in the ADVAPI32 Library... No problem if you want to open or create or edit a reg value... But I can not find a single piece of documentation that gives proper usage or examples encompassing the RegLoadKey function...

Here is what I have so far, any guidance or pointers would be most appreciated:


Option Explicit

Dim nwkey As String, nwkeypth As String, res As Long

Private Declare Function RegLoadKey Lib _
"advapi32" Alias "RegLoadKeyA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal lpFile As String) As Long

Private Declare Function RegUnloadKey Lib _
"advapi32" Alias "RegUnloadKeyA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String) As Long

Const READ_CONTROL = &H20000
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 SYNCHRONIZE = &H100000
Const KEY_READ = ((READ_CONTROL Or KEY_QUERY_VALUE Or _
KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And _
(Not SYNCHRONIZE)
Const HKEY_USERS = &H80000003
Const HKEY_LOCAL_MACHINE = &H80000002

Private Sub Form_Load()
nwkey = "NTUSER"
nwkeypth = "Administrator\NTUSER.DAT" 'Im lost here dont know what to put in
res = RegLoadKey(HKEY_USERS, nwkey, nwkeypath)
If res <> 0 Then
MsgBox "Unable to load registry key."
Exit Sub
Else
MsgBox "Key loaded successfully"
End If
End Sub


I always get the error message... I never get the loaded successfully message... A quick check of the registry also shows that the key did not load...

HELP!!! :)

Thanks for any help you can provide guys...

-sage-

Spookster
07-09-2003, 12:40 AM
Well I have done some VB programming but I am definitely not into it enough for that. :)

You can post this in the Computer Programming forum. VB is just another programming language like C++ or Java.

sage45
07-09-2003, 01:09 AM
OMG, I totally brain blocked on the fact that we have a computer programming forum... GAH... :( LOL Sorry spookster...

-sage-

oracleguy
07-09-2003, 01:17 AM
I've never been able to get that to work either. :)

I tried for a while a long time ago and just gave up. I assume you've googled it?

jkd
07-09-2003, 01:58 AM
:: moving thread ::

sage45
07-09-2003, 07:15 AM
Just to let everyone know... I got it working... :D

WHOO HOOO!!!! :D

When I get to work tommorow, I'll post my completed script...

-sage-

sage45
07-09-2003, 04:10 PM
Okay, here it is:

Create a new Standard EXE
On Form1 place the following elements:
2 Command Buttons (Command1 and Command2)
1 TextBox (Text1)

Module:
Option Explicit
'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 Type LUID
LowPart As Long
HighPart As Long
End Type

Public Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type

Public Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(1) As LUID_AND_ATTRIBUTES
End Type

Public Declare Function GetCurrentProcess Lib _
"kernel32" () As Long

Public Declare Function OpenProcessToken Lib _
"advapi32.dll" _
(ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long

Public Declare Function LookupPrivilegeValue Lib _
"advapi32.dll" Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, _
ByVal lpName As String, _
lpLuid As LUID) As Long

Public Declare Function AdjustTokenPrivileges Lib _
"advapi32.dll" _
(ByVal TokenHandle As Long, _
ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, _
ByVal BufferLength As Long, _
ByVal PreviousState As Long, _
ByVal ReturnLength As Long) As Long

Public Declare Function RegLoadKey Lib _
"advapi32.dll" Alias "RegLoadKeyA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal lpFile As String) As Long

Public Declare Function RegUnLoadKey Lib _
"advapi32.dll" Alias "RegUnLoadKeyA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String) As Long

Public Retval As Long
Public strKeyName As String
Public MyToken As Long
Public TP As TOKEN_PRIVILEGES
Public RestoreLuid As LUID
Public BackupLuid As LUID

Form1:
Option Explicit

Private Sub Form_Load()
On Error Resume Next
With Me
.Caption = "Load Profile"
.Height = "1380"
.Width = "4800"
End With
With Text1
.Height = "285"
.Left = "240"
.Top = "120"
.Width = "4095"
End With
With Command1
.Caption = "Load Hive"
.Height = "400"
.Left = "240"
.Top = "520"
.Width = "1800"
End With
With Command2
.Caption = "Unload Hive"
.Height = "400"
.Left = "2400"
.Top = "520"
.Width = "1800"
End With
strKeyName = "NTUSER"
' Path to file on Windows NT: C:\WinNT\Profiles\<Profile Name>\NtUser.Dat
' Path to file on Windows 2000: C:\Documents and Settings\<Profile Name>\NtUser.Dat

Text1.Text = "<Path to File>"
Command2.Enabled = False

Retval = OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVLEGES _
Or TOKEN_QUERY, MyToken)
If Retval = 0 Then MsgBox "OpenProcess: " & 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
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, vbFalse, TP, Len(TP), 0&, 0&)
If Retval = 0 Then MsgBox "AdjustTokenPrivileges: " & Err.LastDllError
End Sub

Private Sub Command1_Click()
Retval = RegLoadKey(HKEY_USERS, strKeyName, Text1.Text)
If Retval <> 0 Then MsgBox "RegLoadKey: " & Retval
Command2.Enabled = True
End Sub

Private Sub Command2_Click()
Retval = RegUnLoadKey(HKEY_USERS, strKeyName)
If Retval <> 0 Then MsgBox "RegUnloadKey: " & Retval
End Sub

Private Sub Form_Unload(Cancel As Integer)
Retval = AdjustTokenPrivileges(MyToken, vbTrue, TP, Len(TP), 0&, 0&)
If Retval = 0 Then MsgBox "AdjustTokenPrivileges: " & Err.LastDllError
End Sub


After doing this, press F5 to run...

Enter the full path, i.e. - C:\Documents and Settings\Administrator\NTUSER.DAT -, to the profile in the text box and press the Load Hive button...

From what I can tell, it is not case sensitive...

Enjoy,

-sage-