Exemple #1
0
 public RegistryKey FromHandle(SafeRegistryHandle handle)
 {
     // At this point we can't tell whether the key is writable
     // or not (nor the name), so we let the error check code handle it later, as
     // .Net seems to do.
     return(new RegistryKey(handle.DangerousGetHandle(), String.Empty, true));
 }
Exemple #2
0
    public FileAssociationInfo(String fileExtension)
    {
        int  retVal = 0;
        uint lpType = 0;

        if (!fileExtension.StartsWith("."))
        {
            fileExtension = "." + fileExtension;
        }
        ext = fileExtension;

        IntPtr hExtension = IntPtr.Zero;

        // Get the file extension value.
        retVal = RegOpenKeyEx(new IntPtr(HKEY_CLASSES_ROOT), fileExtension, 0, KEY_QUERY_VALUE, out hExtension);
        if (retVal != ERROR_SUCCESS)
        {
            throw new Win32Exception(retVal);
        }
        // Instantiate the first SafeRegistryHandle.
        hExtHandle = new SafeRegistryHandle(hExtension, true);

        string appId       = new string(' ', MAX_PATH);
        uint   appIdLength = (uint)appId.Length;

        retVal = RegQueryValueEx(hExtHandle.DangerousGetHandle(), String.Empty, 0, out lpType, appId, ref appIdLength);
        if (retVal != ERROR_SUCCESS)
        {
            throw new Win32Exception(retVal);
        }
        // We no longer need the hExtension handle.
        hExtHandle.Dispose();

        // Determine the number of characters without the terminating null.
        appId = appId.Substring(0, (int)appIdLength / 2 - 1) + @"\shell\open\Command";

        // Open the application identifier key.
        string exeName       = new string(' ', MAX_PATH);
        uint   exeNameLength = (uint)exeName.Length;
        IntPtr hAppId;

        retVal = RegOpenKeyEx(new IntPtr(HKEY_CLASSES_ROOT), appId, 0, KEY_QUERY_VALUE | KEY_SET_VALUE,
                              out hAppId);
        if (retVal != ERROR_SUCCESS)
        {
            throw new Win32Exception(retVal);
        }

        // Instantiate the second SafeRegistryHandle.
        hAppIdHandle = new SafeRegistryHandle(hAppId, true);

        // Get the executable name for this file type.
        string exePath       = new string(' ', MAX_PATH);
        uint   exePathLength = (uint)exePath.Length;

        retVal = RegQueryValueEx(hAppIdHandle.DangerousGetHandle(), String.Empty, 0, out lpType, exePath, ref exePathLength);
        if (retVal != ERROR_SUCCESS)
        {
            throw new Win32Exception(retVal);
        }

        // Determine the number of characters without the terminating null.
        exePath = exePath.Substring(0, (int)exePathLength / 2 - 1);
        // Remove any environment strings.
        exePath = Environment.ExpandEnvironmentVariables(exePath);

        int position = exePath.IndexOf('%');

        if (position >= 0)
        {
            args = exePath.Substring(position);
            // Remove command line parameters ('%0', etc.).
            exePath = exePath.Substring(0, position).Trim();
        }
        openCmd = exePath;
    }
Exemple #3
0
 static SafeRegistryHandle CreateKey(SafeRegistryHandle rootkey, string path, AttributeFlags flags, KeyCreateOptions options)
 {
     using (ObjectAttributes obja = new ObjectAttributes(path, flags | AttributeFlags.CaseInsensitive, rootkey != null ? rootkey.DangerousGetHandle() : IntPtr.Zero))
     {
         IntPtr handle;
         int    disposition = 0;
         StatusToNtException(NtCreateKey(out handle, GenericAccessRights.MaximumAllowed, obja, 0, null, options, out disposition));
         return(new SafeRegistryHandle(handle, true));
     }
 }