C/C++ Yönetici yetkisi olduğu halde hata veriyor

Katılım
12 Mayıs 2016
Mesajlar
1.115
Çözümler
4
C++:
#include <Windows.h>
#include <iostream>

int main() {
    HKEY hKey;
    const wchar_t* subKey = L"Software\MyApplication";

    // Anahtarı açma işlemi
    long result = RegOpenKeyEx(HKEY_CURRENT_USER, subKey, 0, KEY_SET_VALUE | KEY_QUERY_VALUE | KEY_WOW64_64KEY, &hKey);
    if (result != ERROR_SUCCESS) {
        std::cerr << "Hata: Anahtar açılamadı." << std::endl;
        return 1;
    }

    // Anahtarın güvenlik özelliklerini değiştirme
    SECURITY_DESCRIPTOR sd;
    if (InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
        if (SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE)) {
            if (SetSecurityDescriptorControl(&sd, SE_DACL_PROTECTED, SE_DACL_PROTECTED)) {
                result = RegSetKeySecurity(hKey, DACL_SECURITY_INFORMATION, &sd);
                if (result == ERROR_SUCCESS) {
                    std::wcout << L"Kayıt defteri anahtarı için güvenlik özellikleri değiştirildi." << std::endl;
                }
                else {
                    std::cerr << "Hata: Güvenlik özellikleri değiştirilemedi." << std::endl;
                }
            }
            else {
                std::cerr << "Hata: Security Descriptor kontrolü ayarlanamadı." << std::endl;
            }
        }
        else {
            std::cerr << "Hata: DACL ayarlanamadı." << std::endl;
        }
    }
    else {
        std::cerr << "Hata: Security Descriptor başlatılamadı." << std::endl;
    }

    // Anahtarın kapatılması
    RegCloseKey(hKey);

    return 0;
}
Kod:
Hata güvenlik özellikleri değiştirilemedi diyor. Changing the registry key permission using RegSetKeySecurity in C++ Bunu yapmaya çalışıyorum.

Bende başka bir yöntem denedim onu yapmayınca.

C#:
using System;
using System.Security.AccessControl;
using Microsoft.Win32;
using System.Security.Principal;
using System.Windows.Forms;

namespace Training
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string keyPath = @"Software\MyApplication";
                RegistryKey baseKey = Registry.CurrentUser;

                // Open the registry key with write access
                RegistryKey targetKey = baseKey.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions);

                if (targetKey == null)
                {
                    MessageBox.Show("Registry key not found. Make sure the key path is correct.");
                    return;
                }

                // Get the security descriptor of the registry key
                RegistrySecurity regSecurity = targetKey.GetAccessControl();

                // Create a security identifier (SID) for the Administrators group
                SecurityIdentifier administratorsSid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);

                // Create a security identifier (SID) for the Everyone group
                SecurityIdentifier everyoneSid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);

                // Create access rules for the Administrators group and Everyone group
                RegistryAccessRule administratorsRule = new RegistryAccessRule(administratorsSid,
                                                                                RegistryRights.FullControl,
                                                                                InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                                                PropagationFlags.None,
                                                                                AccessControlType.Allow);

                RegistryAccessRule everyoneRule = new RegistryAccessRule(everyoneSid,
                                                                        RegistryRights.FullControl,
                                                                        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                                        PropagationFlags.None,
                                                                        AccessControlType.Deny);

                // Apply the access rules to the registry key's security descriptor
                regSecurity.AddAccessRule(administratorsRule);
                regSecurity.AddAccessRule(everyoneRule);

                // Set the modified RegistrySecurity back to the registry key
                targetKey.SetAccessControl(regSecurity);

                MessageBox.Show("Registry key permissions modified successfully.");

                targetKey.Close();
            }
            catch (UnauthorizedAccessException ex)
            {
                MessageBox.Show("Error: Insufficient permissions to modify the registry key. Run the application as an administrator.\n" + ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred:\n" + ex.Message);
            }
        }
    }
}
Sorun C# diliyle çözülmüştür.

Yok işe yaramamış pardon.
 
Son düzenleme:

Geri
Yukarı