[C#] Problem with accessing the registry

Discussion in 'Mixed Languages' started by jlgager, Feb 3, 2012.

  1. jlgager

    jlgager MDL Developer

    Oct 30, 2009
    365
    3,190
    10
    #1 jlgager, Feb 3, 2012
    Last edited by a moderator: Apr 20, 2017
    Im trying to access the registry to change a file association and I keep getting a null error. The variable FullPath is null.

    Code:
    try
                {
                    Microsoft.Win32.RegistryKey ClassesRoot = Microsoft.Win32.Registry.ClassesRoot;
                    Microsoft.Win32.RegistryKey FullPath = ClassesRoot.OpenSubKey(".ulz", true);
    
                    FullPath.SetValue("", "wud.CompressedUL");
                }
                catch (SystemException ex)
                {
                    MessageBox.Show(ex.Message);
                }
    I know this is probably WOW64 redirection but i don't know how to fix it. I am coding this on a Win7 x64 computer and I have tried running this on a x32 computer and I get the same null reference exception.

    Also i have been trying to figure out how to create a file in the system32 folder in Win7 x64 but it puts it in the SysWOW64 folder instead.

    If anyone can help that would be awesome thanks!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. woot332

    woot332 MDL Senior Member

    Feb 18, 2011
    390
    810
    10
    For your other issue you could try SysNative instead of System32.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,110
    120
    #3 Josh Cell, Feb 4, 2012
    Last edited by a moderator: Apr 20, 2017
    Well, if have an null return, the registry not has been readed, this code will solve the problem, reading the registry, and if not exist, create the sub key, patching the permissions for an secure mod:


    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Win32;
    using System.Security.AccessControl;
    using System.Security.Principal;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                RegistryKey RegKey = null;
                {
                    try
                    {
                        Registry.ClassesRoot.CreateSubKey(".ulz", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryOptions.Volatile);
                        RegKey = Registry.ClassesRoot.OpenSubKey(".ulz", true);
                    }
                    catch { MessageBox.Show("Critical error on registry read"); }
                }
                try
                {
                    RegistryAccessRule Rules = new RegistryAccessRule(WindowsIdentity.GetCurrent().User, RegistryRights.FullControl, AccessControlType.Allow);
                    {
                        RegistrySecurity Security = new RegistrySecurity();
                        Security.SetOwner(WindowsIdentity.GetCurrent().User);
                        Security.AddAccessRule(Rules);
                        RegKey.SetAccessControl(Security);
                    }
                    RegKey.SetValue("", "wud.CompressedUL");
                }
                catch
                {
                    MessageBox.Show("Fails on registry write");
                }
            }
        }
    }
    
    PS: Aparently, the WOW64 keys is present only in HKLM :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,110
    120
    #4 Josh Cell, Feb 4, 2012
    Last edited by a moderator: Apr 20, 2017
    Well, the kernel will recognize the Sysnative as System32 dir in x64 systems...

    This code will help you with native System32 file write with x86 apps:

    Code:
                if (Directory.Exists(Environment.GetEnvironmentVariable("WINDIR") + "\\Sysnative"))
                {
                    File.WriteAllBytes(Environment.GetEnvironmentVariable("WINDIR") + "\\Sysnative", Properties.Resources.MyRes);
                }
                else
                {
                    File.WriteAllBytes(Environment.GetEnvironmentVariable("WINDIR") + "\\System32", Properties.Resources.MyRes);
                }
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. jlgager

    jlgager MDL Developer

    Oct 30, 2009
    365
    3,190
    10
    Thanks everyone! I will try both of those and get back to you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. jlgager

    jlgager MDL Developer

    Oct 30, 2009
    365
    3,190
    10
    #6 jlgager, Feb 14, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Thanks again everyone both things work now.

    I used this code for the second one.
    Code:
    public static string SystemDirectory
            {
                get
                {
                    if (Directory.Exists(Environment.GetEnvironmentVariable("WINDIR") + "\\Sysnative"))
                    {
                        return (Environment.GetEnvironmentVariable("WINDIR") + "\\Sysnative");
                    }
                    else
                    {
                        return (Environment.GetEnvironmentVariable("WINDIR") + "\\System32");
                    }
                }
            }
    Alphawaves sorry for some reason I couldn't get yours to work it still redirected to the other folder. Thanks though.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,891
    20,339
    180
    #7 Alphawaves, Feb 14, 2012
    Last edited: Feb 14, 2012
    Just checked again, its NOT working on my x64 system only x86.. :laie:

    I compiled using AnyCPU, that why it worked before.:D
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,110
    120
    #8 Josh Cell, Feb 14, 2012
    Last edited by a moderator: Apr 20, 2017
    It's strange, the Environment.GetFolderPath(Environment.SpecialFolder.System) gets the System32 folder...

    Anyway, I've been tested this code:

    Code:
            public static string GetSystemDirectory()
            {
                if (!Environment.GetFolderPath(Environment.SpecialFolder.SystemX86).ToUpper().Contains("SYSTEM32"))
                    return Environment.GetEnvironmentVariable("WINDIR") + "\\Sysnative";
                return Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
            }
    Modded the Environment.SpecialFolder.SystemX86 instead of System...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...