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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jg007
    Contributor
    • Mar 2008
    • 283

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

    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

    Code:
      [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);
    
            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;
    
    
    
    
            [StructLayout(LayoutKind.Sequential)]
    
    
     private void Registry_Hive_Load (object sender, EventArgs e)
            {
    
                try
                {
                    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);
    
    
                    if (RegLoadKey(HKEY_USERS, "Tempload", label2.Text) == 0)
                    {
    
                        profileloaded = true;
                        richTextBox1.AppendText("Profile Loaded"+"\n");
                    }
                }
  • jg007
    Contributor
    • Mar 2008
    • 283

    #2
    no replies yet so I have started to try and convert the code but am completely stuck with 'OpenProcessTok en' , 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


    Code:
    Public Declare Function OpenProcessToken Lib "advapi32.dll" _ (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _ ByVal TokenHandle As Long) As Long
     
        Private Const TOKEN_ADJUST_PRIVLEGES = &H20
        Private Const TOKEN_QUERY = &H8
        Private Const SE_PRIVILEGE_ENABLED = &H2
     
    MyToken = 0
    Retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVLEGES Or TOKEN_QUERY, MyToken)
     
       MsgBox("OpenProcess: " & Err.LastDllError)

    Comment

    • malav123
      New Member
      • Feb 2008
      • 217

      #3
      HI,
      Your problem' solution is in "developerfusio n.com"...
      Your problem will be solved deffinetely....


      -malav.

      Comment

      • dip_developer
        Recognized Expert Contributor
        • Aug 2006
        • 648

        #4
        Originally posted by jg007
        no replies yet so I have started to try and convert the code but am completely stuck with 'OpenProcessTok en' , 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


        Code:
        Public Declare Function OpenProcessToken Lib "advapi32.dll" _ (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _ ByVal TokenHandle As Long) As Long
         
        Private Const TOKEN_ADJUST_PRIVLEGES = &H20
        Private Const TOKEN_QUERY = &H8
        Private Const SE_PRIVILEGE_ENABLED = &H2
         
        MyToken = 0
        Retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVLEGES Or TOKEN_QUERY, MyToken)
         
        MsgBox("OpenProcess: " & Err.LastDllError)
        see here for conversion of your code...

        Comment

        • jg007
          Contributor
          • Mar 2008
          • 283

          #5
          Originally posted by dip_developer
          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.

          Comment

          Working...