473,327 Members | 1,976 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,327 software developers and data experts.

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

283 100+
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
4 2023
jg007
283 100+
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
217 100+
HI,
Your problem' solution is in "developerfusion.com"...
Your problem will be solved deffinetely....


-malav.
Apr 7 '08 #3
dip_developer
648 Expert 512MB
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
jg007
283 100+
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

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

Similar topics

5
by: John | last post by:
Hi, In a nutshell, why am I getting the results I am getting? I would have expected a call to method1( n) to return an 'n' length string. At first I thought it was a problem in my function,...
2
by: Skybuck Flying | last post by:
Hi, This is a somewhat cleaned up version 0.02 of the nrand48() routine. There are still a few issues: This code needs brackets but where ? X = (uint64) xsubi << 32 | (uint32) xsubi << 16...
4
by: andy.mcvicker | last post by:
Hi Gang I'm not sure what needs to be changed when converting an asp to vbs. I'm not sure if I can with my code below. Can someone PLEASE convert the text below to vbs? Thanks a million....
1
by: shapper | last post by:
Hello, I am trying to convert an Asp.Net XML sitemap file in a Google XMl sitemap file using a XSL file using an HttpHandler. Everything seems well in my code but I am getting an error: XML...
14
by: bbawa1 | last post by:
It says invalid expression term && protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if...
7
by: Lynn | last post by:
Hello, I have a website that is working fine. I have just turned on "option strict" and am getting an error with the parts of my code. I have fixed everything but this section, which has me...
14
by: cool.vimalsmail | last post by:
i dont know how to convert a txt file into a zip file (i.e.,)i have a file named packages and i want the packages file with a ".gz" extension by implementing a python program
5
by: Sunfire | last post by:
Can somebody put this code into c#? Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim imageFolder As String Dim imageText As String Dim bm As...
9
by: ahilar12 | last post by:
1. <head> 2. <script type="text/javascript"> 3. </script> 4. </head> 5. <body> 6. <form> 7. <select name="team" id="mylist" > 8. <option></option> 9....
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
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.