2011-11-21 70 views
6

在閱讀this post之後,我嘗試編寫一個小型應用程序,我需要讀取和寫入隱藏的註冊表項/值。
我檢查了Registry Manipulation using NT Native APIsCreating "Hidden" Registry Values的鏈接。
第一個給了我一些工作,但它是用C++編寫的,而第二個是Delphi項目運行良好。
我不能夠先轉換,我可以嘗試轉換第二,但我需要找到一些代碼來讀取鍵/值。出於這個原因,我想知道是否有一些「準備好」並在C#中進行了測試。
我也下載了Proces Hacker v1.11源代碼,並用它來部分轉換Delphi示例,如下所示,但隱藏的註冊表鍵可以訪問(而在Delphi中則不是),並且沒有API來編寫值。隱藏的註冊表項/值

static void Main(string[] args) 
{ 
    string KeyNameBuffer = @"\Registry\User\S-1-5-21-3979903645-2167650815-2353538381-1001\SOFTWARE"; 
    string NewKeyNameBuffer = "Systems Internals"; 
    string HiddenKeyNameBuffer = "Can't touch me\0"; 
    string HiddenValueNameBuffer = "Hidden Value"; 

    // Apro la chiave di registro 
    IntPtr SoftwareKeyHandle = CreateKey(KeyNameBuffer, IntPtr.Zero); 
    if (SoftwareKeyHandle != IntPtr.Zero) 
    { 
     IntPtr SysKeyHandle = CreateKey(NewKeyNameBuffer, SoftwareKeyHandle); 
     if (SysKeyHandle != IntPtr.Zero) 
     {   
      // This key shouldn't be accessible, but it is    
      IntPtr HiddenKeyHandle = CreateKey(HiddenKeyNameBuffer, SysKeyHandle); 
      if (HiddenKeyHandle != IntPtr.Zero) 
      { 
       // I don't have APIs to write values 
      } 
     } 
    } 
} 

static IntPtr CreateKey(string keyName, IntPtr rootKey) 
{ 
    IntPtr res; 
    KeyCreationDisposition disp; 
    ObjectAttributes attributes = new ObjectAttributes(keyName, 
     ObjectFlags.CaseInsensitive, 
     new NativeHandle(rootKey)); 
    NtStatus st = Win32.NtCreateKey(out res, KeyAccess.All, 
     ref attributes, 0, 
     IntPtr.Zero, RegOptions.NonVolatile, out disp); 
    return st == NtStatus.Success ? res : IntPtr.Zero; 
} 

最後:從Vista上,你不能,如果你沒有運行你的應用程序作爲管理員,所以這個例子我用我的用戶的註冊表項寫\Registry\Machine一部分。如果我需要存儲每臺計算機的值,有沒有辦法讓我們的本機API編寫該註冊表的一部分?

回答

1

如果你想在HKLM中使用它,並且權限不允許你使用,那麼你使用的是哪個API層,Nt *的Reg *函數 - 它不會讓你在拒絕訪問的時候這麼做錯誤。