By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

Please can Somebody help convert from C# to VB .Net

100+
P: 283
Hi,

I have some code that somebody has vritten in C and although I have got it to roughly work it is hard going and I can't get it to do exactly what I want as I know even less about c than I do about VB :)

I have chopped the code a bit so I hope it makes enough sense, what I am trying to do is load a remote registry in from a server , make some changes and then unload it.later.

I have google'd quite a bit but I can only find C or VB6 code for this and I cannot get it to work properly in VB .NET

Expand|Select|Wrap|Line Numbers
  1.  
  2.   [StructLayout(LayoutKind.Sequential)]
  3.         public struct LUID
  4.         {
  5.             public int LowPart;
  6.             public int HighPart;
  7.         }
  8.         [StructLayout(LayoutKind.Sequential)]
  9.         public struct TOKEN_PRIVILEGES
  10.         {
  11.             public LUID Luid;
  12.             public int Attributes;
  13.             public int PrivilegeCount;
  14.         }
  15.  
  16.         [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  17.         public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,
  18.         ref int tokenhandle);
  19.  
  20.         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  21.         public static extern int GetCurrentProcess();
  22.  
  23.         [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  24.         public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,
  25.         [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
  26.  
  27.         [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  28.         public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs,
  29.         [MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGES Newstate, int bufferlength,
  30.         int PreivousState, int Returnlength);
  31.  
  32.         [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  33.         public static extern int RegLoadKey(uint hKey, string lpSubKey, string lpFile);
  34.  
  35.         [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  36.         public static extern int RegUnLoadKey(uint hKey, string lpSubKey);
  37.  
  38.         public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  39.         public const int TOKEN_QUERY = 0x00000008;
  40.         public const int SE_PRIVILEGE_ENABLED = 0x00000002;
  41.         public const string SE_RESTORE_NAME = "SeRestorePrivilege";
  42.         public const string SE_BACKUP_NAME = "SeBackupPrivilege";
  43.         public const uint HKEY_USERS = 0x80000003;
  44.         public string shortname;
  45.  
  46.  
  47.  
  48.  
  49.         [StructLayout(LayoutKind.Sequential)]
  50.  
  51.  
  52.  private void Registry_Hive_Load (object sender, EventArgs e)
  53.         {
  54.  
  55.             try
  56.             {
  57.                 int token = 0;
  58.                 int retval = 0;
  59.  
  60.  
  61.                 TOKEN_PRIVILEGES TP = new TOKEN_PRIVILEGES();
  62.                 TOKEN_PRIVILEGES TP2 = new TOKEN_PRIVILEGES();
  63.                 LUID RestoreLuid = new LUID();
  64.                 LUID BackupLuid = new LUID();
  65.  
  66.                 retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token);
  67.                 retval = LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestoreLuid);
  68.                 retval = LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackupLuid);
  69.                 TP.PrivilegeCount = 1;
  70.                 TP.Attributes = SE_PRIVILEGE_ENABLED;
  71.                 TP.Luid = RestoreLuid;
  72.                 TP2.PrivilegeCount = 1;
  73.                 TP2.Attributes = SE_PRIVILEGE_ENABLED;
  74.                 TP2.Luid = BackupLuid;
  75.  
  76.                 retval = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0);
  77.                 retval = AdjustTokenPrivileges(token, 0, ref TP2, 1024, 0, 0);
  78.  
  79.  
  80.                 if (RegLoadKey(HKEY_USERS, "Tempload", label2.Text) == 0)
  81.                 {
  82.  
  83.                     profileloaded = true;
  84.                     richTextBox1.AppendText("Profile Loaded"+"\n");
  85.                 }
  86.             }
  87.  
  88.  
Apr 5 '08 #1
Share this Question
Share on Google+
4 Replies


100+
P: 283
no replies yet so I have started to try and convert the code but am completely stuck with 'OpenProcessToken' , i've Tried everyting and spent ages on google but keep on getting Error 998 which I checked and is ERROR_NOACCESS when I check the last dll error although the c# code works fine, can anybody help please


Expand|Select|Wrap|Line Numbers
  1. Public Declare Function OpenProcessToken Lib "advapi32.dll" _ (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _ ByVal TokenHandle As Long) As Long
  2.  
  3.     Private Const TOKEN_ADJUST_PRIVLEGES = &H20
  4.     Private Const TOKEN_QUERY = &H8
  5.     Private Const SE_PRIVILEGE_ENABLED = &H2
  6.  
  7. MyToken = 0
  8. Retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVLEGES Or TOKEN_QUERY, MyToken)
  9.  
  10.    MsgBox("OpenProcess: " & Err.LastDllError)
  11.  
Apr 6 '08 #2

malav123
100+
P: 217
HI,
Your problem' solution is in "developerfusion.com"...
Your problem will be solved deffinetely....


-malav.
Apr 7 '08 #3

dip_developer
Expert 100+
P: 648
no replies yet so I have started to try and convert the code but am completely stuck with 'OpenProcessToken' , i've Tried everyting and spent ages on google but keep on getting Error 998 which I checked and is ERROR_NOACCESS when I check the last dll error although the c# code works fine, can anybody help please


Expand|Select|Wrap|Line Numbers
  1. Public Declare Function OpenProcessToken Lib "advapi32.dll" _ (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _ ByVal TokenHandle As Long) As Long
  2.  
  3. Private Const TOKEN_ADJUST_PRIVLEGES = &H20
  4. Private Const TOKEN_QUERY = &H8
  5. Private Const SE_PRIVILEGE_ENABLED = &H2
  6.  
  7. MyToken = 0
  8. Retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVLEGES Or TOKEN_QUERY, MyToken)
  9.  
  10. MsgBox("OpenProcess: " & Err.LastDllError)
  11.  
see here for conversion of your code...

http://labs.developerfusion.co.uk/co...to-csharp.aspx
Apr 7 '08 #4

100+
P: 283
see here for conversion of your code...

http://labs.developerfusion.co.uk/co...to-csharp.aspx
I will have to keep hold of the URL for that site but unfortunately it has some issues converting the code to import DLL's but I might be able to work round that so will have to try later when I have time.
Apr 7 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.