Windowsレジストリの保護されたレジストリキーの最終更新時刻を取得したいとします。例FRO私は、レジストリキーを開くしようとしています:RegOpenKeyExがエラーコード5を返します
HKLM\SYSTEM\ControlSet001\Enum\USBSTOR\Disk&Ven_SanDisk&Prod_Ultra&Rev_1.00\4C531001530301123274&0\Properties
はRegOpenKeyEx
を使用していますが、アクセスが拒否されたことを意味するerror code 5
を返します。
私のプログラムは、保護されていないレジストリキーで正常に動作します。私は無効にUACを持っているし、ここで、「管理者として実行」
が私のコードであるとして、私のプログラムを実行します:あなたが言ったように
public DateTime GetKeyModifiedTime(string computerName, string BaseKey, string SubKey)
{
int remoteKeyResult = -1;
try
{
if (BaseKey.Equals("HKEY_LOCAL_MACHINE"))
remoteKeyResult = RegConnectRegistry(@"\\" + computerName.ToUpper(), Convert.ToInt32(Hives.HKEY_LOCAL_MACHINE), ref longResult);
if (BaseKey.Equals("HKEY_CURRENT_USER"))
remoteKeyResult = RegConnectRegistry(@"\\" + computerName.ToUpper(), Convert.ToInt32(Hives.HKEY_CURRENT_USER), ref longResult);
if (BaseKey.Equals("HKEY_USERS"))
remoteKeyResult = RegConnectRegistry(@"\\" + computerName.ToUpper(), Convert.ToInt32(Hives.HKEY_USERS), ref longResult);
int abasekey = 0;
abasekey = ParseInput(BaseKey);
//parse just the base key part and return the Integer enum value of the base key
int BaseKeyValue = 0;
//if the value of abasekey is not -1(used for error) then set BaseKeyValue to the returned vaue
if (!(abasekey == -1))
{
BaseKeyValue = abasekey;
}
else
{
//if abasekey does = -1 then bail out because input is not correct.
}
int regkeyptr = 0;
IntPtr p = new IntPtr(regkeyptr);
int openregkeyResult = RegOpenKeyEx(longResult, SubKey, 0, KEY_QUERY_VALUE, ref p);
//third param is Reserved and must be 0(worked as "Nothing" also)
//strbldr.AppendLine("Open RegKey Pointer " + regkeyptr.ToString());
// strbldr.AppendLine("Open RegKey Result " + openregkeyResult.ToString());
//create a filetime structure to recieve the returned time
System.Runtime.InteropServices.ComTypes.FILETIME lpftLastWriteTime = default(System.Runtime.InteropServices.ComTypes.FILETIME);
int returnvalue = 0;
returnvalue = RegQueryInfoKey(p.ToInt32(), null, 0, 0, 0, 0, 0, 0, 0, 0, 0, ref lpftLastWriteTime);
//strbldr.AppendLine("RegQueryInfoKey Result " + returnvalue);
//returnvalue is the HResult call of RegQueryInfoKey
//strbldr.AppendLine();
//strbldr.AppendLine("Filetime High " + lpftLastWriteTime.dwHighDateTime.ToString() + " " + "Filetime Low " + lpftLastWriteTime.dwLowDateTime.ToString());
//use the api function to convert the filetime to a date time This ine returns in local time, File TIme is UTC 0 based.
DateTime dt = FileTimeToDateTime(lpftLastWriteTime);
return dt;
}
catch (Exception ex)
{
return DateTime.Now;
}
}
質問はプログラム的にアクセスする方法です – qasali
@qasaliの下で実行しているアカウントにキーにアクセスする権限がない場合、ユーザーを偽装しないか、権限を追加しない限り鍵に – SledgeHammer
どうすればいいですか? – qasali