如何用C ++打开regedit

时间:2013-07-25 16:47:33

标签: c++ windows winapi

我正在寻找打开注册表编辑器并显示一些特定键或值的方法。例如,如果我通过"HKLM\\SOFTWARE\\Skype\\Installer"我想得到这样的结果:enter image description here

欢迎除system()电话以外的所有建议。

2 个答案:

答案 0 :(得分:1)

只需致电system即可。使用Raymond Chen的话:It rather involved being on the other side of this airtight hatchway。任何相关的攻击都需要让机器受到危害,以致system电话完全无关紧要。事实上,任何可以更改RegEdit的攻击者都可以更改您的计划,因此他可以添加system次呼叫。 (他不会,因为它无论如何都是毫无意义的)

答案 1 :(得分:0)

这就是我需要的东西。

String GetFullHKEY (HKEY hKeyRoot)
{
    if (HKEY_LOCAL_MACHINE == hKeyRoot) return _T("HKEY_LOCAL_MACHINE\\");
    if (HKEY_CLASSES_ROOT == hKeyRoot) return _T("HKEY_CLASSES_ROOT\\");
    if (HKEY_CURRENT_CONFIG == hKeyRoot) return _T("HKEY_CURRENT_CONFIG\\");
    if (HKEY_CURRENT_USER == hKeyRoot) return _T("HKEY_CURRENT_USER\\");
    if (HKEY_USERS == hKeyRoot) return _T("HKEY_USERS\\");
}

bool RegistryGoTo (HKEY hKeyRoot, const String &lpctPath, String lpctValue)
{
    if (lpctPath.empty() || 0 == hKeyRoot) 
        return false;
    if( lpctValue.empty() && lpctValue.empty() == 0)
    {
        lpctValue.clear();
    }

    SHELLEXECUTEINFO shi = { 0 };
    DEVMODE dm = { 0 };

    HWND hWndRegedit = ::FindWindow (_T("RegEdit_RegEdit"), NULL);
    if (NULL == hWndRegedit)
    {
        shi.cbSize = sizeof(SHELLEXECUTEINFO);
        shi.fMask  = SEE_MASK_NOCLOSEPROCESS; 
        shi.lpVerb = _T("open"); 
        shi.lpFile = _T("regedit.exe"); 
        shi.nShow  = SW_SHOWNORMAL; 

        ShellExecuteEx (&shi);
        if( GetLastError() != 0 )
        {
            Sleep(200);
            ShellExecuteEx (&shi);
        }
        WaitForInputIdle (shi.hProcess, INFINITE);

        hWndRegedit = ::FindWindow (_T("RegEdit_RegEdit"), NULL);
    }

    if (NULL == hWndRegedit) return FALSE;

    SetForegroundWindow (hWndRegedit);
    ShowWindow (hWndRegedit, SW_SHOWNORMAL);

    HWND hWndTreeView = FindWindowEx (hWndRegedit, NULL, _T ("SysTreeView32"), NULL);
    SetForegroundWindow (hWndTreeView);
    SetFocus (hWndTreeView);

    for (int i = 0; i < 30; i++)
    {
        SendMessage (hWndTreeView, WM_KEYDOWN, VK_LEFT, 0);
    } 

    dm.dmSize = sizeof (DEVMODE);
    EnumDisplaySettings (NULL, ENUM_CURRENT_SETTINGS, &dm);

    if (8 < dm.dmBitsPerPel) Sleep (100);

    // the path must start with a backslash
    String stRegPath = String (_T("\\")) + GetFullHKEY(hKeyRoot) + lpctPath;

    // open path
    for (int iIndex = 0; iIndex < (int) stRegPath.length (); iIndex++)
    {
        if (_T('\\') == stRegPath [iIndex])  
        {
            SendMessage (hWndTreeView, WM_KEYDOWN, VK_RIGHT, 0);

            if (8 < dm.dmBitsPerPel) 
                Sleep (100);
        } 
        else SendMessage (hWndTreeView, WM_CHAR, toupper (stRegPath [iIndex]), 0);
    } 

    SetForegroundWindow (hWndRegedit);
    SetFocus (hWndRegedit);

    if (lpctValue.length())
    {
        HWND hWndListView = FindWindowEx (hWndRegedit, NULL, _T("SysListView32"), NULL);
        SetForegroundWindow (hWndListView);
        SetFocus (hWndListView);

        Sleep (100); 

        SendMessage (hWndListView, WM_KEYDOWN, VK_HOME, 0);

        String stValue = lpctValue;

        for (String::iterator it = stValue.begin (); it != stValue.end (); ++it)
        {
            SendMessage (hWndListView, WM_CHAR, toupper (*it), 0);
        } 
    } 

    return true;
}