473,836 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

283 Contributor
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 2053
jg007
283 Contributor
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


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 New Member
HI,
Your problem' solution is in "developerfusio n.com"...
Your problem will be solved deffinetely....


-malav.
Apr 7 '08 #3
dip_developer
648 Recognized Expert Contributor
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


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 Contributor
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
1323
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, but after finding an article showing how to do the salt, I noticed, they had basically the same code as me. So obviously, I'm so lost, I didn't even know I was lost. Can somebody please direct me to an online article explaining what I don't
2
1608
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 | xsubi;
4
3899
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. Andy
1
2215
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 Parsing Error: not well-formed Location: http://localhost:1132/WebSite/Google Line Number 2, Column 62:<urlset
14
1419
by: bbawa1 | last post by:
It says invalid expression term && protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if ((Convert.ToInt32(e.Row.Cells.Text)>=15))&&(Convert.ToInt32(e.Row.Cells.Text)>=17); {
7
3382
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 baffled. I am getting the error "option Strict On disallows late binding", and the error is referring to this line of code: (3rd line in my sub below) Select Case sender.Parent.ID
14
1316
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
1725
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 Bitmap Dim ms As MemoryStream imageFolder = Request.QueryString(imFolder)
9
3211
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. <option>north</option> 10. <option>south</option>
0
9820
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10551
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10594
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5650
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.