我Hook了RegCreateKey函数, 得到一个HKEY值
我想根据这个HKEY值求得他所对应的键名即:  Software\Editplus\AAA    用RegOpenKey  打开得到一个HKEY值  
     在RegCreateKey函数内,我得到了这个HKEY值,
     现在想在我想根据这个HKEY值 获得 Software\Editplus\AAA 能不能实现?

解决方案 »

  1.   

    http://community.csdn.net/Expert/TopicView.asp?id=3756597
      

  2.   

    .NET有专门的封装,比较好做。
      

  3.   

    To pomelowu(羽战士) 
       兄弟真是热心啊,上次由文件句柄求得文件名的问题还要多谢兄弟帮忙呢 :)看了你刚才提供的帖子,不过他也还没解决问题。
    不知道RegMon是怎么实现的  奇怪中
      

  4.   

    总算解决了When Regmon sees an open, create or close call, it updates an internal hash table that serves as the mapping between key handles and registry path names. Whenever it sees calls that are handle based, it looks up the handle in the hash table to obtain the full name for display. If a handle-based access references a key opened before Regmon started, Regmon will fail to find the mapping in it hash table and will simply present the key's value instead. 这是www.sysinternals.com上的原话(http://www.sysinternals.com/ntw2k/source/regmon.shtml)根据他所说的做法,在Create 和Open 和Close Key的时候更新Hash表,而在其他函数操作的时候查询这个Hash表。我模仿了写了一个,可以实现。根据这个原理,同样也可以解决由文件句柄求文件名的问题了。hehe , 一下子解决了2个问题, 爽~~
      

  5.   

    呵呵, 好的阿
    基本原理就像上面所说的。首先在Dll内定义全局变量        map<HKEY, char*> g_mapRegKeyPathA;
    然后在Dll的DLL_PROCESS_ATTACH事件中执行下面的代码
    // 添加预定义的注册表键
    HKEY hKey[] = {
    HKEY_CLASSES_ROOT,
    HKEY_CURRENT_USER,
    HKEY_LOCAL_MACHINE,
    HKEY_USERS,
    HKEY_PERFORMANCE_DATA,
    HKEY_CURRENT_CONFIG,
    HKEY_DYN_DATA
    }; LPCSTR lpPathA[] = {
    "HKEY_CLASSES_ROOT",
    "HKEY_CURRENT_USER",
    "HKEY_LOCAL_MACHINE",
    "HKEY_USERS",
    "HKEY_PERFORMANCE_DATA",
    "HKEY_CURRENT_CONFIG",
    "HKEY_DYN_DATA"
    };
    for(int i = 0 ; i < sizeof(hKey)/sizeof(hKey[0]) ; i++)
    {
    int iLenA = strlen(lpPathA[i]);
    char* tmpA = new char[iLenA+1];
    strcpy(tmpA, lpPathA[i]); g_mapRegKeyPathA.insert(std::make_pair(hKey[i], tmpA));
    }在Dll的DLL_PROCESS_DETACH事件中清空申请的资源
    map<HKEY, char*>::iterator itA = g_mapRegKeyPathA.begin();
    for(; itA != g_mapRegKeyPathA.end() ; itA++)
    {
    char *tmp = itA->second;
    delete [] tmp;
    }
    g_mapRegKeyPathA.clear();然后Hook RegOpenKey, RegCreateKey , RegCloseKey 中更新hash表中的内容
    Eg:在RegCreateKeyA函数中
    /* begin the hook fun */
    LONG lResult; // Call the original function
    lResult = RegCreateKeyANext(hKey, lpSubKey, phkResult);

    if(ERROR_SUCCESS == lResult)
    {
    if(g_mapRegKeyPathA[*phkResult] == NULL)
    {
    char *path = g_mapRegKeyPathA[hKey];
    if(path != NULL)
    {
    char *pKeyPath = new char[strlen(path)+strlen(lpSubKey)+2];
    strcpy(pKeyPath, path);
    strcat(pKeyPath, "\\");
    strcat(pKeyPath, lpSubKey);
    g_mapRegKeyPathA[*phkResult] = pKeyPath;
    }
    }
    } // Return the result back to the caller
    return lResult;而在RegSetValueA之类的函数中,就根据HKEY从Hash中取出对应的路径
    char *path = g_mapRegKeyPathA[hKey];
             if(path != NULL)   {.....}基本上就是这样了, 文件操作的话也可以类似的模仿。