Create a new thread to access the Registry

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.