5 Replies - 4979 Views - Last Post: 13 April 2008 - 11:53 PM Rate Topic: -----

#1 jg007   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 23-March 08

Please can somebody help convert C# to VB

Posted 12 April 2008 - 12:42 PM

I have tried to convert some C# code I have got to VB as I don't know much C# it is awkward working with it.

[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");
				}
			}






I have tried tro convert this but am getting stuck at trying to change the process privleges as I keep on getting access denied messages although the c# code works fine.

The code is designed to load the registry hive for an offlie user, ammend it and then unload it.

I have also tried using a code converter but it keeps on failing at to convert all the 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)




Is This A Good Question/Topic? 0
  • +

Replies To: Please can somebody help convert C# to VB

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Please can somebody help convert C# to VB

Posted 12 April 2008 - 04:13 PM

Are you trying to convert this to VB.Net or VB6?
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Please can somebody help convert C# to VB

Posted 13 April 2008 - 12:30 AM

Most of your code is just made up of PInvoke signatures of Win32 API Functions.
I contribute a lot to www.pinvoke.net .
Check it out and you'll get the VB.NET equivalent for every API Function.

If you want the VB6 Function Declarations, the API Text Viewer should suffice.
Was This Post Helpful? 0
  • +
  • -

#4 jg007   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 23-March 08

Re: Please can somebody help convert C# to VB

Posted 13 April 2008 - 12:42 PM

View Postborn2c0de, on 13 Apr, 2008 - 12:30 AM, said:

Most of your code is just made up of PInvoke signatures of Win32 API Functions.
I contribute a lot to www.pinvoke.net .
Check it out and you'll get the VB.NET equivalent for every API Function.

If you want the VB6 Function Declarations, the API Text Viewer should suffice.


Thanks, I am trying to convert to VB. Net . I'm not sure waht you mean by ' pinvoke signatures ' but I will have a look at 'pinvoke' and see if it helps.

although I been doing computer admin for 10+ years now I have only really started trying to write code in the last month and although I have writen various simple programs in various basic interpretations it is quite hard going trying to work out what a lot of the code does and at present I am mostly just mashing things together till they work :)
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Please can somebody help convert C# to VB

Posted 13 April 2008 - 05:41 PM

Moved to VB.Net :)
Was This Post Helpful? 0
  • +
  • -

#6 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Please can somebody help convert C# to VB

Posted 13 April 2008 - 11:53 PM

PInvoke stands for Platform Invoke Services.
It is used for allowing managed code to use unmanaged code and viceversa.
You can find more information about it here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1