UniqueDisplayName-Adam

Hopefully this is the correct place to post. I have an XP application that I am porting to Vista. The app calls DllImport on advapi32.dll, and kernel32.dll.

I am executing the following methods:

  • OpenProcessToken
  • GetCurrentProcess
  • LookupPrivilegeValue
  • AdjustTokenPrivileges
  • RegLoadKey
  • RegUnLoadKey

I am receiving a null pointer in a method running the following code:

///////////////////////////////////////////////////////////////////////////////////////////////////

int token = 0;

int retval = 0;

TOKEN_PRIVILEGES TP = new TOKEN_PRIVILEGES();

TOKEN_PRIVILEGES TP2 = new TOKEN_PRIVILEGES();

LUID RestoreLuid = new LUID();

LUID BackupLuid = new LUID();

retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);

retval = LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestoreLuid);

retval = LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackupLuid);

TP.PrivilegeCount = 1;

TP.Attributes = SE_PRIVILEGE_ENABLED;

TP.Luid = RestoreLuid;

TP2.PrivilegeCount = 1;

TP2.Attributes = SE_PRIVILEGE_ENABLED;

TP2.Luid = BackupLuid;

retval = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);

retval = AdjustTokenPrivileges(token, 0, ref TP2, 1024, 0, 0);

String fileLoc = <user's profile directory>

RegLoadKey(HKEY_USERS, "NTUSER.DAT", fileLoc + @"\NTUSER.DAT");

//Do Registry Stuff Here....

RegUnLoadKey(HKEY_USERS, "NTUSER.DAT");

////////////////////////////////////////////////////////////////////////////////////////////////////

Hopefully the code makes sense. This same code works in XP Professional. I assume there are changes in Vista that affect this code. Is it possible to edit this code to work with Vista My primary goal is to be able to load a Registry Hive through C# code.

Thanks is advance.

- Adam



Re: Windows SDK RegLoadKey / RegUnLoadKey in C#

UniqueDisplayName-Adam

Also, Here is my API declaration in C#:

[StructLayout(LayoutKind.Sequential)]

public struct LUID

{

public int LowPart;

public int HighPart;

}

[StructLayout(LayoutKind.Sequential)]

public struct TOKEN_PRIVILEGES

{

public LUID Luid;

public int Attributes;

public int PrivilegeCount;

}

[DllImport("advapi32.dll", CharSet = CharSet.Auto)]

public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,

ref int tokenhandle);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]

public static extern int GetCurrentProcess();

[DllImport("advapi32.dll", CharSet = CharSet.Auto)]

public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,

[MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);

[DllImport("advapi32.dll", CharSet = CharSet.Auto)]

public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs,

[MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGES Newstate, int bufferlength,

int PreivousState, int Returnlength);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]

public static extern int RegLoadKey(uint hKey, string lpSubKey, string lpFile);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]

public static extern int RegUnLoadKey(uint hKey, string lpSubKey);

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]

internal static extern bool ExitWindowsEx(int flg, int rea);

public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

public const int TOKEN_QUERY = 0x00000008;

public const int SE_PRIVILEGE_ENABLED = 0x00000002;

public const string SE_RESTORE_NAME = "SeRestorePrivilege";

public const string SE_BACKUP_NAME = "SeBackupPrivilege";

public const uint HKEY_USERS = 0x80000003;

public string shortname;

bool unloaded = false;





Re: Windows SDK RegLoadKey / RegUnLoadKey in C#

UniqueDisplayName-Adam

More information: The RegLoadKey returns 1314... this indicates that a required permission is missing. What permission and How do I grant it



Re: Windows SDK RegLoadKey / RegUnLoadKey in C#

UniqueDisplayName-Adam

I will answer my own post..... RegLoadKey is working fine. The issue is with the UAC in Vista. My app is not running with Amin privs. I needed to add a manifest to my app to allow admin privs.