Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 131,627 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,819 people online right now. Registration is fast and FREE... Join Now!




Please can somebody help convert C# to VB

 
Reply to this topicStart new topic

Please can somebody help convert C# to VB

jg007
post 12 Apr, 2008 - 11:42 AM
Post #1


New D.I.C Head

*
Joined: 23 Mar, 2008
Posts: 26



Thanked 1 times
My Contributions


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.

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





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

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)



User is offlineProfile CardPM

Go to the top of the page


PsychoCoder
post 12 Apr, 2008 - 03:13 PM
Post #2


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,906



Thanked 116 times

Dream Kudos: 8450

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

My Contributions


Are you trying to convert this to VB.Net or VB6?
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 12 Apr, 2008 - 11:30 PM
Post #3


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

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

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

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

jg007
post 13 Apr, 2008 - 11:42 AM
Post #4


New D.I.C Head

*
Joined: 23 Mar, 2008
Posts: 26



Thanked 1 times
My Contributions


QUOTE(born2c0de @ 13 Apr, 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.


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 smile.gif
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 13 Apr, 2008 - 04:41 PM
Post #5


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,906



Thanked 116 times

Dream Kudos: 8450

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

My Contributions


Moved to VB.Net smile.gif
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 13 Apr, 2008 - 10:53 PM
Post #6


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

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

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

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 05:25AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month