注册表RegOpenKeyEx、RegQueryValueEx、RegCreateKeyEx和RegSetValueEx编程

来源:互联网 发布:益阳长春淘宝客服 编辑:程序博客网 时间:2024/04/30 07:08

参考程序:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int autorun_register(const char *exe_filename);
  FILE *fp;
  if((fp=fopen("E:\\应用程序\\kwmusic2010.exe","r"))==NULL)
  {
     printf("cannot open file\n");
     exit(0);
     }
  autorun_register("E:\\应用程序\\kwmusic2010.exe");
  system("PAUSE");
  return 0;
}
int autorun_register(const char *exe_filename)
{
    //修改注册表,开机启动
    int return_value=0;
    HKEY hroot = HKEY_LOCAL_MACHINE,hkey;
    const char *subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
    DWORD dwDisposition;
    long result=RegCreateKeyEx(
           hroot,         
           subkey,        
           0,            
           NULL,            
           REG_OPTION_NON_VOLATILE,
           KEY_ALL_ACCESS,
           NULL,          
           &hkey,         
           &dwDisposition
           );

    if(result==ERROR_SUCCESS)
    {
       result=RegSetValueEx(
            hkey,            
            "病毒",   
            0,        
            REG_SZ,         
            (CONST BYTE *)exe_filename,
            (DWORD)strlen(exe_filename)    
             );
      if(result!=ERROR_SUCCESS)return_value=-1;
      RegCloseKey(hkey);
    }
    else return_value=-1;
    if(!return_value)printf("开机启动注册成功!\n");
    else printf("开机启动注册失败...\n");
    return return_value;
}

 

 

自己写的小程序;一个按钮创建注册表,设置键值;另一个表打开注册表读取键值;实现COM口号的写入。

读取键值:

void CTrafficDetectDlg::OnBnClickedOpenclose()
{
 // TODO: Add your control notification handler code here

//从注册表中读取COM口
 HKEY hkey;
 LPCTSTR path;
 DWORD retOpen,retGet;
 DWORD type = REG_SZ;
 LPBYTE comport_Get = new BYTE[5];
 DWORD cbData = 5;
 path = "SOFTWARE\\Microsoft\\Windows\\CarIntegratedInfoDetect";
 retOpen = RegOpenKeyEx(HKEY_LOCAL_MACHINE,path,0,KEY_ALL_ACCESS,&hkey);
 if(retOpen == ERROR_SUCCESS)
 {
  retGet = RegQueryValueEx(hkey,"KEY_PORT",NULL,&type,comport_Get,&cbData);

   if(retGet == ERROR_SUCCESS)
   {
    
    comport = CString(comport_Get);
    RegCloseKey(hkey);//关闭注册表
   }
  
 }
 delete[] comport_Get;

UpdateData(TRUE);//数据更新
 if(m_comstatus == FALSE)
 {
  if(SerialComm.OpenCom(comport,19200))
  {
   m_comstatus = TRUE;
   SetDlgItemText(IDC_VISIBILITY,SerialComm.vsbypack);//显示数据
   szTemp.Format(_T("端口状态:%s端口已打开"),comport);
   m_StatBar->SetText(szTemp,1,0);
   
   //打开按钮的切换
   SetDlgItemText(BTN_OPENCLOSE,_T("(&C)关闭串口"));


  

   
  }
  else
  {
   AfxMessageBox(_T("串口打开失败,请检查!!"));
  }
  
 }

  
 else
 {
  if(SerialComm.CloseCom() == TRUE)
  {
   m_comstatus = FALSE;
   //打开按钮的切换
   SetDlgItemText(BTN_OPENCLOSE,_T("(&O)打开串口"));
   m_StatBar->SetText("端口状态:端口未打开",1,0);
  }
 }
}

 

}

 

 

 

 

创建注册表,设置键名称,键值;

void SetSerial::OnBnClickedSetcomm()
{
 // TODO: Add your control notification handler code here

 //把PORT口端写入注册表
 HKEY hkey;
 LPCTSTR path;
 DWORD retSet,retOpen;
 DWORD dwDisposition;
 
 path = "SOFTWARE\\Microsoft\\Windows\\CarIntegratedInfoDetect";
 retOpen = RegCreateKeyEx(HKEY_LOCAL_MACHINE,path,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hkey,&dwDisposition);

 if(retOpen == ERROR_SUCCESS)
 {
  UpdateData(TRUE);
  retSet = RegSetValueEx(hkey,"KEY_PORT",0,REG_SZ,(CONST BYTE*)(LPCTSTR)m_comport,(m_comport.GetLength() + 1) * sizeof(TCHAR));//需要将CString类型经过两次强制转换
   
  if(retSet == ERROR_SUCCESS)
  {
   RegCloseKey(hkey);//关闭注册表
    
   if(IDOK == MessageBox("设置成功" ,"提示",MB_OK))
   {

    CDialog::OnCancel();
   }
  } 
 }

 

}

 

 

 

 

0 0