打开注册表指定位置
CString strRegPath(_T("HKEY_CLASSES_ROOT\\txtfile\\shell\\open")); HWND hReg = NULL, hRegTree = NULL; hReg = ::FindWindow(_T("RegEdit_RegEdit"), _T("注册表编辑器")); if(hReg == NULL) { WinExec("C:\\Windows\\regedit.exe", SW_SHOWNORMAL ); hReg = ::FindWindow(_T("RegEdit_RegEdit"), _T("注册表编辑器")); } if(hReg == NULL) { MessageBox(_T("获取注册表窗口句柄失败!"),_T("ERROR"),NULL); return; } hRegTree = ::FindWindowEx(hReg,0,_T( "SysTreeView32"),NULL); if(hRegTree == NULL) { MessageBox(_T("获取树形窗口句柄失败!"),_T("ERROR"),NULL); return; } ::SendMessage(hRegTree,WM_SETFOCUS,0,0); ::SendMessage(hRegTree,WM_KEYDOWN,VK_HOME,0); UINT uVirtualKey; int i = 0; for (; i < strRegPath.GetLength(); i++) { uVirtualKey = strRegPath[i]; if (_T('\\') == uVirtualKey) { ::SendMessage(hRegTree,WM_KEYDOWN, VK_RIGHT, 0); Sleep(500); } else { uVirtualKey = toupper(uVirtualKey); ::SendMessage(hRegTree,WM_CHAR, uVirtualKey, 0); } }
以前发消息不行,给其他程序发送数据原来是需要一些函数才行。
void ShowError(LPCTSTR lpMsg) { TCHAR szError[100]; _stprintf_s(szError,TEXT("Error(%ld)\n"),GetLastError()); //szError.Format(_T("Error(%s)"),GetLastError()); MessageBox(lpMsg,szError,NULL); } HRESULT LocateOnRegister(TCHAR chKeyPath[], LPCTSTR lpValueName/*= NULL*/) { HWND hReg = NULL,hRegTree = NULL; HRESULT res = 0x0; hReg = ::FindWindow(_T("RegEdit_RegEdit"), _T("注册表编辑器")); if (NULL == hReg) { SHELLEXECUTEINFO Info = {0}; Info.cbSize = sizeof(SHELLEXECUTEINFO); Info.fMask = SEE_MASK_NOCLOSEPROCESS; Info.lpVerb = _T("open"); Info.lpFile = _T("regedit.exe"); Info.nShow = SW_SHOWNORMAL; ShellExecuteEx(&Info); WaitForInputIdle(Info.hProcess, INFINITE); hReg = ::FindWindow(_T("RegEdit_RegEdit"), NULL); } else { if(::IsIconic(hReg)) ::ShowWindow(hReg,SW_SHOWNORMAL); if(hReg != ::GetForegroundWindow()) ::SetForegroundWindow(hReg); } //if(hReg == NULL) //{ //WinExec("C:\\Windows\\regedit.exe", SW_SHOWNORMAL ); //hReg = ::FindWindow(_T("RegEdit_RegEdit"), _T("注册表编辑器")); //} if(hReg == NULL) { MessageBox(_T("获取注册表窗口句柄失败! "),_T("ERROR"),NULL); return 0; } hRegTree = ::FindWindowEx(hReg,0,_T( "SysTreeView32"),NULL);//HWND if(hRegTree == NULL) { MessageBox(_T("获取树形窗口句柄失败! "),_T("ERROR"),NULL); return 0; } ASSERT(::IsWindow(hRegTree)); DWORD lpdwPId; HANDLE hProcess; GetWindowThreadProcessId(hRegTree,&lpdwPId); hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lpdwPId); if(NULL == hProcess) ShowError(_T("OpenProcess() failed!")); else { TCHAR *token = NULL; TCHAR *next_token = NULL; HTREEITEM hItem; ::SendMessage(hRegTree,WM_SETFOCUS,0,0); //::SendMessage(hRegTree,WM_KEYDOWN,VK_HOME,0); //::SendMessage(hRegTree,WM_KEYUP, VK_HOME, 0); hItem = (HTREEITEM)::SendMessage(hRegTree, TVM_GETNEXTITEM, TVGN_ROOT,(LPARAM)0); LPTVITEM ptvItem = (LPTVITEM)VirtualAllocEx(hProcess,NULL,sizeof(TVITEM),MEM_COMMIT,PAGE_READWRITE); TCHAR* pszText = (TCHAR*)VirtualAllocEx(hProcess,NULL,512,MEM_COMMIT,PAGE_READWRITE); if(ptvItem == NULL || pszText == NULL) ShowError(_T("VirtualAllocEx() failed!")); else { TCHAR chText[512] = {0}; // Establish string and get the first token: token = _tcstok_s(chKeyPath,_T("\\"),&next_token); // While there are tokens in "chKeyPath" while(token != NULL) { // Get next token // expand tree nodeand retrieve the childitem: //::SendMessage(hRegTree, WM_KEYDOWN, VK_RIGHT, 0); //::SendMessage(hRegTree, WM_KEYUP, VK_RIGHT, 0); (BOOL)::SendMessage(hRegTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem); hItem = (HTREEITEM)::SendMessage(hRegTree, TVM_GETNEXTITEM, TVGN_CHILD,(LPARAM)hItem); if (token != NULL && hItem != NULL ) { res = 0x0; while(hItem) { TVITEMti = {0}; ti.mask = TVIF_TEXT; ti.cchTextMax = 512; ti.pszText = pszText; ti.hItem = hItem; WriteProcessMemory(hProcess,ptvItem,&ti,sizeof(TVITEM),NULL); (BOOL)::SendMessage(hRegTree, TVM_GETITEM, 0, (LPARAM)ptvItem); ReadProcessMemory(hProcess,pszText,chText,512,NULL); if(StrCmp(token,chText) == 0) { (BOOL)::SendMessage(hRegTree, TVM_ENSUREVISIBLE, 0, (LPARAM)hItem); (BOOL)::SendMessage(hRegTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem); res = 0x2;//Bin:10 = succeed to find the keypath break; } hItem = (HTREEITEM)::SendMessage(hRegTree, TVM_GETNEXTITEM, TVGN_NEXT,(LPARAM)hItem); } TRACE(_T("%s\n"), token ); token = _tcstok_s( NULL, _T("\\"), &next_token); } } } BOOL bb = VirtualFreeEx(hProcess,ptvItem,0,MEM_RELEASE); VirtualFreeEx(hProcess,pszText,0,MEM_RELEASE); CloseHandle(hProcess); //CloseHandle(hRegTree); } if(lpValueName) { HWND hRegList = ::FindWindowEx(hReg, NULL, _T("SysListView32"), NULL); ::SendMessage(hRegList,WM_SETFOCUS,0,0);//::SetFocus(hRegList); // have to wait for Regedit to update listview Sleep(500); // select the value //::SendMessage(hRegList,WM_KEYDOWN, VK_HOME, 0); //::SendMessage(hRegList,WM_KEYUP, VK_HOME, 0); GetWindowThreadProcessId(hRegTree,&lpdwPId); hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lpdwPId);
if(NULL == hProcess)
ShowError(_T("OpenProcess() failed!"));
else
{
LPLVITEM plvItem = (LPLVITEM)VirtualAllocEx(hProcess,NULL,sizeof(LVITEM),MEM_COMMIT,PAGE_READWRITE);
TCHAR* pText = (TCHAR*)VirtualAllocEx(hProcess,NULL,80,MEM_COMMIT,PAGE_READWRITE);
if(plvItem == NULL || pText == NULL)
ShowError(_T("VirtualAllocEx() failed!"));
else
{
int iCount = (int) ::SendMessage(hRegList, LVM_GETITEMCOUNT, 0, 0L);
int item = 0;
TCHAR chText[80] = {0};
while(item < iCount)
{
LVITEM li = {0};
li.mask = LVIF_TEXT;
li.iItem = item;
li.iSubItem = 0;
li.cchTextMax = 80;
li.pszText = pText;
if(WriteProcessMemory(hProcess,plvItem,&li,sizeof(LVITEM),NULL) == 0)
ShowError(_T("WriteProcessMemory() failed!"));
(BOOL)::SendMessage(hRegList, LVM_GETITEM, 0, (LPARAM)plvItem);
if(ReadProcessMemory(hProcess,pText,chText,80,NULL) == 0)
ShowError(_T("ReadProcessMemory() failed!"));
if(StrCmp(lpValueName,chText) == 0)
{
ZeroMemory(&li,sizeof(li));
li.mask = LVIF_STATE;
li.iItem = item;
li.state = LVIS_FOCUSED | LVIS_SELECTED;
li.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
if(WriteProcessMemory(hProcess,plvItem,&li,sizeof(LVITEM),NULL) == 0)
ShowError(_T("WriteProcessMemory() failed!"));
(BOOL)::SendMessage(hRegList, LVM_ENSUREVISIBLE, item, MAKELPARAM(TRUE, 0));
if((BOOL)::SendMessage(hRegList,LVM_SETITEMSTATE, item, (LPARAM)plvItem) ==FALSE)
ShowError(_T("LVM_SETITEMSTATE failed!"));;
TRACE(_T("OK\n"));
res |= 0x1;//Bin:01 = succeed to find the valuename
break;
}
item = (int) ::SendMessage(hRegList, LVM_GETNEXTITEM, item, MAKELPARAM(LVNI_BELOW, 0));
}
}
if(VirtualFreeEx(hProcess,plvItem,0,MEM_RELEASE) == NULL)
ShowError(_T("VirtualFreeEx() Failed!"));
VirtualFreeEx(hProcess,pText,0,MEM_RELEASE);
CloseHandle(hProcess);
}
}
TRACE(_T("%ld\n"),res);
return res;
使用
//CString strRegPath(_T("HKEY_CLASSES_ROOT\\txtfile\\shell\\open"));
TCHAR strRegPath[] = _T("HKEY_CURRENT_USER\\Software\\BatchRenaming");
LocateOnRegister(strRegPath,_T("Version"));