Click here to Skip to main content
15,915,093 members
Home / Discussions / C#
   

C#

 
GeneralRe: Disposing of values in foreach loop Pin
PIEBALDconsult14-Oct-11 16:43
mvePIEBALDconsult14-Oct-11 16:43 
AnswerRe: Disposing of values in foreach loop Pin
Luc Pattyn14-Oct-11 16:58
sitebuilderLuc Pattyn14-Oct-11 16:58 
QuestionNeed help reading 64bit registry keys from a 32bit application, remotely Pin
turbosupramk314-Oct-11 7:01
turbosupramk314-Oct-11 7:01 
AnswerRe: Need help reading 64bit registry keys from a 32bit application, remotely Pin
André Kraak14-Oct-11 8:55
André Kraak14-Oct-11 8:55 
GeneralRe: Need help reading 64bit registry keys from a 32bit application, remotely Pin
turbosupramk317-Oct-11 8:15
turbosupramk317-Oct-11 8:15 
GeneralRe: Need help reading 64bit registry keys from a 32bit application, remotely Pin
turbosupramk318-Oct-11 6:25
turbosupramk318-Oct-11 6:25 
GeneralRe: Need help reading 64bit registry keys from a 32bit application, remotely Pin
turbosupramk318-Oct-11 11:54
turbosupramk318-Oct-11 11:54 
GeneralRe: Need help reading 64bit registry keys from a 32bit application, remotely Pin
turbosupramk318-Oct-11 12:49
turbosupramk318-Oct-11 12:49 
How does this code look to everyone?


C#
private void button6_Click(object sender, EventArgs e)
 {

     string result = _64to32bit.reg64reader("server name", "hive", @"key path", "key name");
     MessageBox.Show(result);

 }



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Windows.Forms;

namespace Updator
{
    class _64to32bit
    {
        public enum RegSAM
        {
            QueryValue = 0x0001,
            SetValue = 0x0002,
            CreateSubKey = 0x0004,
            EnumerateSubKeys = 0x0008,
            Notify = 0x0010,
            CreateLink = 0x0020,
            WOW64_32Key = 0x0200,
            WOW64_64Key = 0x0100,
            WOW64_Res = 0x0300,
            Read = 0x00020019,
            Write = 0x00020006,
            Execute = 0x00020019,
            AllAccess = 0x000f003f
        }

        [DllImport("Advapi32.dll")]
        static extern uint RegOpenKeyEx(
            UIntPtr hKey,
            string lpSubKey,
            uint ulOptions,
            int samDesired,
            out int phkResult);

        [DllImport("Advapi32.dll")]
        static extern uint RegCloseKey(int hKey);

        [DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx")]
        public static extern int RegQueryValueEx(
            int hKey,
            string lpValueName,
            int lpReserved,
            ref RegistryValueKind lpType,
            StringBuilder lpData,
            ref uint lpcbData);

        [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegQueryValueEx")]
        private static extern int RegQueryValueEx(
            int hKey,
            string lpValueName,
            int lpReserved,
            ref RegistryValueKind lpType,
            [Out] byte[] lpData,
            ref uint lpcbData);

        [DllImport("advapi32")]
        static extern int RegConnectRegistry(string machine, UIntPtr hKey, out
        IntPtr pRemKey);
        [DllImport("advapi32")]
        static extern int RegCloseKey(IntPtr hKey);
        [DllImport("advapi32")]
        static extern int RegSaveKey(IntPtr hKey, string fileout, IntPtr
        secdesc);

        const uint HKEY_CLASSES_ROOT = 0x80000000;
        const uint HKEY_CURRENT_USER = 0x80000001;
        const uint HKEY_LOCAL_MACHINE = 0x80000002;
        const uint HKEY_USERS = 0x80000003;
        const uint HKEY_CURRENT_CONFIG = 0x80000005;
        const uint HKEY_DYN_DATA = 0x80000006;
        const uint HKEY_PERFORMANCE_DATA = 0x80000007;
        //const uint  = 0x8000000;

        public static string reg64reader(string serverName, string hive, string keyPath, string stringValue)
        {
            uint hiveConst = HKEY_LOCAL_MACHINE;
            switch (hive)
            {
                case "hkcr":
                    hiveConst = HKEY_CLASSES_ROOT;
                    break;

                case "hkey_classes_root":
                    hiveConst = HKEY_CLASSES_ROOT;
                    break;

                case "HKCR":
                    hiveConst = HKEY_CLASSES_ROOT;
                    break;

                case "HKEY_CLASSES_ROOT":
                    hiveConst = HKEY_CLASSES_ROOT;
                    break;

                case "hkcu":
                    hiveConst = HKEY_CURRENT_USER;
                    break;

                case "hkey_current_user":
                    hiveConst = HKEY_CURRENT_USER;
                    break;

                case "HKCU":
                    hiveConst = HKEY_CURRENT_USER;
                    break;

                case "HKEY_CURRENT_USER":
                    hiveConst = HKEY_CURRENT_USER;
                    break;

                case "hklm":
                    hiveConst = HKEY_LOCAL_MACHINE;
                    break;

                case "hkey_local_machine":
                    hiveConst = HKEY_LOCAL_MACHINE;
                    break;

                case "HKLM":
                    hiveConst = HKEY_LOCAL_MACHINE;
                    break;

                case "HKEY_LOCAL_MACHINE":
                    hiveConst = HKEY_LOCAL_MACHINE;
                    break;

                case "hku":
                    hiveConst = HKEY_USERS;
                    break;

                case "hkey_users":
                    hiveConst = HKEY_USERS;
                    break;

                case "HKU":
                    hiveConst = HKEY_USERS;
                    break;

                case "HKEY_USERS":
                    hiveConst = HKEY_USERS;
                    break;

                case "hkcc":
                    hiveConst = HKEY_CURRENT_CONFIG;
                    break;

                case "hkey_current_config":
                    hiveConst = HKEY_CURRENT_CONFIG;
                    break;

                case "HKCC":
                    hiveConst = HKEY_CURRENT_CONFIG;
                    break;

                case "HKEY_CURRENT_CONFIG":
                    hiveConst = HKEY_CURRENT_CONFIG;
                    break;

                case "hkdd":
                    hiveConst = HKEY_DYN_DATA;
                    break;

                case "hkey_dyn_data":
                    hiveConst = HKEY_DYN_DATA;
                    break;

                case "HKDD":
                    hiveConst = HKEY_DYN_DATA;
                    break;

                case "HKEY_DYN_DATA":
                    hiveConst = HKEY_DYN_DATA;
                    break;

                case "hkpd":
                    hiveConst = HKEY_PERFORMANCE_DATA;
                    break;

                case "hkey_performance_data":
                    hiveConst = HKEY_PERFORMANCE_DATA;
                    break;

                case "HKPD":
                    hiveConst = HKEY_PERFORMANCE_DATA;
                    break;

                case "HKEY_PERFORMANCE_DATA":
                    hiveConst = HKEY_PERFORMANCE_DATA;
                    break;

                default:
                    MessageBox.Show("Default");
                    break;
            }

            UIntPtr key = new UIntPtr(hiveConst);
            IntPtr remKey;
            int ret = RegConnectRegistry(serverName, key, out remKey);
            UIntPtr remKeyUIntPtr = unchecked((UIntPtr)(long)(ulong)remKey);
            string result = GetRegKey64(remKeyUIntPtr, keyPath, RegSAM.WOW64_64Key, stringValue);
            //MessageBox.Show(result);
            RegCloseKey(remKey);
            return (result);
        }

        static public string GetRegKey64(UIntPtr inHive, String inKeyName, RegSAM in32or64key, String inPropertyName)
        {
            int hkey = 0;
            RegistryKey remoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "ph-mb-05");
            try
            {
                uint lResult = RegOpenKeyEx(inHive, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey);
                if (0 != lResult) return null;
                RegistryValueKind lpType = 0;
                uint lpcbData = 1024;
                StringBuilder strBuffer = new StringBuilder(1024);
                RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, strBuffer, ref lpcbData);
                string value = strBuffer.ToString();
                return value;
            }
            finally
            {
                if (0 != hkey) RegCloseKey(hkey);
            }
        }

    }
}

SuggestionRe: Need help reading 64bit registry keys from a 32bit application, remotely Pin
André Kraak19-Oct-11 9:19
André Kraak19-Oct-11 9:19 
GeneralRe: Need help reading 64bit registry keys from a 32bit application, remotely Pin
turbosupramk319-Oct-11 12:24
turbosupramk319-Oct-11 12:24 
QuestionCodeParser for C# source code Pin
Bernhard Hiller14-Oct-11 3:27
Bernhard Hiller14-Oct-11 3:27 
AnswerRe: CodeParser for C# source code Pin
PIEBALDconsult14-Oct-11 4:34
mvePIEBALDconsult14-Oct-11 4:34 
GeneralRe: CodeParser for C# source code Pin
Bernhard Hiller16-Oct-11 20:50
Bernhard Hiller16-Oct-11 20:50 
Questionto create a video file from a list of bitmap picture files in C sharp Pin
Member 823309513-Oct-11 17:34
Member 823309513-Oct-11 17:34 
AnswerRe: to create a video file from a list of bitmap picture files in C sharp Pin
AditSheth13-Oct-11 18:58
AditSheth13-Oct-11 18:58 
QuestionWindows 7: Getting The 'preview' file icon Pin
Michael Handschuh13-Oct-11 13:11
Michael Handschuh13-Oct-11 13:11 
AnswerRe: Windows 7: Getting The 'preview' file icon Pin
Ravi Bhavnani13-Oct-11 16:17
professionalRavi Bhavnani13-Oct-11 16:17 
GeneralRe: Windows 7: Getting The 'preview' file icon Pin
Michael Handschuh13-Oct-11 21:12
Michael Handschuh13-Oct-11 21:12 
GeneralRe: Windows 7: Getting The 'preview' file icon Pin
Ravi Bhavnani14-Oct-11 1:40
professionalRavi Bhavnani14-Oct-11 1:40 
QuestionVery basic OOP question Pin
SFORavi13-Oct-11 12:40
SFORavi13-Oct-11 12:40 
AnswerRe: Very basic OOP question Pin
PIEBALDconsult13-Oct-11 12:43
mvePIEBALDconsult13-Oct-11 12:43 
AnswerRe: Very basic OOP question Pin
Michael Handschuh13-Oct-11 13:14
Michael Handschuh13-Oct-11 13:14 
GeneralRe: Very basic OOP question Pin
SFORavi13-Oct-11 13:36
SFORavi13-Oct-11 13:36 
GeneralRe: Very basic OOP question Pin
Michael Handschuh13-Oct-11 13:51
Michael Handschuh13-Oct-11 13:51 
GeneralRe: Very basic OOP question Pin
SFORavi13-Oct-11 14:02
SFORavi13-Oct-11 14:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.