VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply

Please can somebody help convert C# to VB Rate Topic: -----

#1 jg007  Icon User is offline

  • New D.I.C Head
  • Pip

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


Dream Kudos: 0

Share |

Please can somebody help convert C# to VB

Posted 12 April 2008 - 11:42 AM

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)



Was This Post Helpful? 0
  • +
  • -


#2 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Please can somebody help convert C# to VB

Posted 12 April 2008 - 03:13 PM

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

#3 born2c0de  Icon User is offline

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

Reputation: 137
  • View blog
  • Posts: 4,622
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Re: Please can somebody help convert C# to VB

Posted 12 April 2008 - 11:30 PM

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  Icon User is offline

  • New D.I.C Head
  • Pip

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


Dream Kudos: 0

Re: Please can somebody help convert C# to VB

Posted 13 April 2008 - 11:42 AM

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  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Please can somebody help convert C# to VB

Posted 13 April 2008 - 04:41 PM

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

#6 born2c0de  Icon User is offline

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

Reputation: 137
  • View blog
  • Posts: 4,622
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Re: Please can somebody help convert C# to VB

Posted 13 April 2008 - 10: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
  • You cannot start a new topic
  • Reply Reply


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users