1. First, create an MFC (exe) project, select the dialog mode, add a button on the panel, and a ListBox control. ListBox adds an object clistbox m_listbox.
2. Click the query button and create a new process in the response function. The process completes access to the registry.
3. The new process accesses the registry, obtains the access results, and sends the response to the regopen interface. For more information about adding custom messages to MFC, see the first two articles.
The Code is as follows:
void CRegOpenDlg::OnBnClickedReach(){// TODO: Add your control notification handler code hereUpdateData(TRUE);hThread = CreateThread(NULL, 0, /*(LPTHREAD_START_ROUTINE)*/threadProc, this ,0, &threadID);UpdateData(FALSE);CloseHandle(hThread);//hThread - INVALID_HANDLE_VALUE;} DWORD __stdcall threadProc(LPVOID lparam){HKEY hKey;CRegOpenDlg *cReg = (CRegOpenDlg *)lparam;if(ERROR_SUCCESS != ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, SUB_KEY, 0, KEY_READ, &hKey) ){// MessageBox("failed in second");return -1;}DWORD dwIndex = 0;LONG lRet;DWORD cbName = KEY_LEN;TCHAR *lpszSubKeyName = new TCHAR[KEY_LEN];while((lRet = ::RegEnumKeyEx(hKey, dwIndex, szSubKeyName, &cbName, NULL, NULL, NULL, NULL)) != ERROR_NO_MORE_ITEMS){ if(lRet != ERROR_SUCCESS) {//MessageBox("failed in second");continue;; } // m_aPrograms.Add(szSubKeyName); SendMessage(cReg->m_hWnd, MY_MESSAGE , NULL, (LPARAM)lpszSubKeyName); ++dwIndex; cbName = KEY_LEN;}::RegCloseKey(hKey);delete[] lpszSubKeyName;return 0;} LRESULT CRegOpenDlg::OnMyMessage(WPARAM wParam, LPARAM lParam) { int n = 1; TCHAR rec[KEY_LEN]; memcpy(rec, (LPVOID)lParam, KEY_LEN); m_listBox.AddString(rec); return 0; }
In the processing function DWORD _ stdcall threadproc (lpvoid lparam), specify the call method __stdcall to search for and send the my_message message. After receiving the message, onmymessage () the tchar array containing the file name is included. Redefines a tchar rec [key_len] array and copies it using the mencpy function,
Memcpy (REC, (lpvoid) lparam, key_len); lparam refers to the first address of the source array, and the copy length of the key_len name.