自动打开注册表地址 定位注册表地址 源码

----

----

时间仓促,来不及优化。。。请见谅。

使用时该函数时,请去除最后的“\”,且请进行大写字母处理。

还有,UNICODE版没有实现。。。

//============================================
//++
#define STR_HKCU        TEXT( "HKCU" )
#define STR_HKCU_FULL    TEXT( "HKEY_CURRENT_USER" )
#define STR_HKLM        TEXT( "HKLM" )
#define STR_HKLM_FULL    TEXT( "HKEY_LOCAL_MACHINE" )
#define STR_HKCC        TEXT( "HKCC" )
#define STR_HKCC_FULL    TEXT( "HKEY_CURRENT_CONFIG" )
#define STR_HKCR        TEXT( "HKCR" )
#define STR_HKCR_FULL    TEXT( "HKEY_CLASSES_ROOT" )
#define STR_HKU            TEXT( "HKU" )
#define STR_HKU_FULL    TEXT( "HKEY_USERS" )

#define STR_CLASS_NAME        TEXT( "RegEdit_RegEdit" )
#define STR_CLASS_NAME_SUB    TEXT( "SysTreeView32" )
#define STR_CLASS_NAME_LIST    TEXT( "SysListView32" )


const TCHAR* g_pszVerb = TEXT( "open" );
const TCHAR* g_pszFile = TEXT( "regedit.exe" );

void
DebugMsg(
         
const TCHAR* pszMsg
         )
{
#ifdef _DEBUG
    MessageBox( NULL, pszMsg, NULL, MB_OK 
| MB_TOPMOST );
#endif
}

BOOL
ShowSpecifiedRegAddr(
     
const TCHAR* pszRegAddr
     )
{
    DEVMODE devMode 
= { 0 };
    HKEY hKey, hRootKey; 
// the key
    SHELLEXECUTEINFO execInfo = { 0 };
    
const TCHAR* pszAddr = pszRegAddr;
    
const TCHAR* pszTemp = NULL;
    TCHAR szDest[ MAX_PATH ] 
= { 0 };
    
const TCHAR* pszSub = NULL;
    HWND hwnd 
= NULL, hwndSub = NULL, hwndList = NULL;
    DWORD dwProcessId;

    devMode.dmSize 
= sizeof( DEVMODE );
    
if ( !::EnumDisplaySettings( NULL, ENUM_CURRENT_SETTINGS, &devMode ) ) {
        DebugMsg( TEXT( 
"获取显示设备信息失败" ) );
        
return FALSE;
    }

    
if ( pszAddr == NULL ) {
        DebugMsg( TEXT( 
"输入地址为空!" ) );
        
return FALSE;
    }

    
if ( _tcsncmp( pszAddr, STR_HKLM, 4 ) == 0 || _tcsncmp( pszAddr, STR_HKLM_FULL, 18 ) == 0 ) {
        hRootKey 
= HKEY_LOCAL_MACHINE;
        _stprintf( szDest, TEXT( 
"\\%s" ), STR_HKLM_FULL );
    } 
else if ( _tcsncmp( pszAddr, STR_HKCU, 4 ) == 0 || _tcsncmp( pszAddr, STR_HKCU_FULL, 17 ) == 0 ) {
        hRootKey 
= HKEY_CURRENT_USER;
        _stprintf( szDest, TEXT( 
"\\%s" ), STR_HKCU_FULL );
    } 
else if ( _tcsncmp( pszAddr, STR_HKCC, 4 ) == 0 || _tcsncmp( pszAddr, STR_HKCC_FULL, 19 ) == 0 ) {
        hRootKey 
= HKEY_CURRENT_CONFIG;
        _stprintf( szDest, TEXT( 
"\\%s" ), STR_HKCC_FULL );
    } 
else if ( _tcsncmp( pszAddr, STR_HKCR, 4 ) == 0 || _tcsncmp( pszAddr, STR_HKCR_FULL, 17 ) == 0 ) {
        hRootKey 
= HKEY_CLASSES_ROOT;
        _stprintf( szDest, TEXT( 
"\\%s" ), STR_HKCR_FULL );
    } 
else if ( _tcsncmp( pszAddr, STR_HKU, 3 ) == 0 || _tcsncmp( pszAddr, STR_HKU_FULL, 10 ) == 0 ) {
        hRootKey 
= HKEY_USERS;
        _stprintf( szDest, TEXT( 
"\\%s" ), STR_HKU_FULL );
    } 
else {
        DebugMsg( TEXT( 
"regedit地址有误" ) );
        
return FALSE;
    }

    pszSub 
= _tcschr( pszAddr, TEXT( '\\' ) );
    
if ( pszSub ) ++pszSub;

    
if ( ::RegOpenKey( hRootKey, pszSub, &hKey ) == ERROR_SUCCESS ) {
        ::RegCloseKey( hKey );
        _tcscat( szDest, TEXT( 
"\\" ) );
        if (pszSub) _tcscat( szDest, pszSub );
        _tcscat( szDest, TEXT( 
"\\" ) );
    } 
else {
        TCHAR
* pszPos = NULL;
        _tcscat( szDest, TEXT( 
"\\" ) );
        if (pszSub) _tcscat( szDest, pszSub );
        pszPos 
= _tcsrchr( szDest, TEXT( '\\' ) );
        pszTemp 
= pszPos + 1;
        
*pszPos = TEXT( '\0' );
    }

    hwnd 
= ::FindWindow( STR_CLASS_NAME, NULL );
    
if ( hwnd ) {
        ::GetWindowThreadProcessId( hwnd, 
&dwProcessId );
        execInfo.hProcess 
= ::OpenProcess( 0, NULL, dwProcessId );
    } 
else {
        execInfo.cbSize 
= sizeof( SHELLEXECUTEINFO );
        execInfo.fMask 
= SEE_MASK_NOCLOSEPROCESS;
        execInfo.lpVerb 
= g_pszVerb;
        execInfo.lpFile 
= g_pszFile;
        execInfo.nShow 
= 1;
        ::ShellExecuteEx( 
&execInfo );
        ::WaitForInputIdle( execInfo.hProcess, INFINITE );
        hwnd 
= ::FindWindow( STR_CLASS_NAME, NULL );
        
if ( hwnd == NULL ) {
            DebugMsg( TEXT( 
"无法打开Regedit" ) );
            
return FALSE;
        }
    }
    ::ShowWindow( hwnd, SW_SHOW );
    ::SetForegroundWindow( hwnd );
    hwndSub 
= ::FindWindowEx( hwnd, NULL, STR_CLASS_NAME_SUB, NULL );
    ::SetForegroundWindow( hwndSub );
    ::SetFocus( hwndSub );
    ::WaitForInputIdle( execInfo.hProcess, INFINITE );
    ::SendMessage( hwnd, WM_COMMAND, ( WPARAM )
0x10288, NULL );
    ::WaitForInputIdle( execInfo.hProcess, INFINITE );

    
for ( dwProcessId = 30; dwProcessId != 0--dwProcessId ) {
        ::SendMessage( hwndSub, WM_KEYDOWN, ( WPARAM )
0x250 );
        ::WaitForInputIdle( execInfo.hProcess, INFINITE );
    }

    
if ( szDest[ 0 ] != TEXT( '\0' ) ) {
        
int i;
        
for ( i = 0; szDest[ i ] != TEXT( '\0' ); ++i ) {
            
if ( szDest[ i ] == TEXT( '\\' ) ) {
                ::SendMessage( hwndSub, WM_KEYDOWN, ( WPARAM )
0x270 );
            } 
else {
                TCHAR ch 
= toupper( szDest[ i ] );
                ::SendMessage( hwndSub, WM_CHAR, ( WPARAM )ch, 
0 );
            }
            ::WaitForInputIdle( execInfo.hProcess, INFINITE );
        }
    }
    ::WaitForInputIdle( execInfo.hProcess, INFINITE );

    
if ( pszTemp != NULL ) {
        hwndList 
= ::FindWindowEx( hwnd, NULL, STR_CLASS_NAME_LIST, NULL );
        ::SetForegroundWindow( hwndList );
        ::SetFocus( hwndList );
    }

    
if ( devMode.dmBitsPerPel > 8 ) {
        ::Sleep( 
750 );
    } 
    
    
bool f = false;
    
if ( pszTemp != NULL ) {
        ::SendMessage( hwndList, WM_KEYDOWN, ( WPARAM )
0x240 );
        f 
= true;
    }
    
while ( pszTemp && *pszTemp ) {
        ::SendMessage( hwndList, WM_CHAR, toupper( 
*pszTemp++ ), 0 );
    }

    
if ( f ) {
        ::SetForegroundWindow( hwnd );
        ::SetFocus( hwndList );
    }

    
return TRUE;
}
原文地址:https://www.cnblogs.com/lin1270/p/2010050.html