写注册表自动运行

写注册表自动运行

BOOL   Register()  
{  
 char   FileName[256];  
 HKEY   hKey;  
 GetModuleFileName(NULL,   FileName,   MAX_PATH);  
 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,  
  "Software//Microsoft//Windows//CurrentVersion//Run",  
  NULL,  
  KEY_WRITE,  
  &hKey)   ==   ERROR_SUCCESS)  
 {  
  if(RegSetValueEx(hKey,  
   "MyApp", //键名  
   NULL,  
   REG_SZ,  
   (const   unsigned   char*)FileName,  
   MAX_PATH)   !=   ERROR_SUCCESS)  
  {  
   RegCloseKey(hKey);  
   return   FALSE;  
  }  
  RegCloseKey(hKey);  
 }  
 else  
  return   FALSE;  
 return   TRUE;  
}  
 

 

 

BOOL SetAutoRun(CString strPath)//开机自动运行
{
   CString str;
   HKEY hRegKey;
   BOOL bResult;
   str=_T("Software//Microsoft//Windows//CurrentVersion//Run");
   if(RegOpenKey(HKEY_LOCAL_MACHINE, str, &hRegKey) != ERROR_SUCCESS) 
       bResult=FALSE;
   else
   {
       _splitpath(strPath.GetBuffer(0),NULL,NULL,str.GetBufferSetLength(MAX_PATH+1),NULL);
       strPath.ReleaseBuffer();
       str.ReleaseBuffer();
       if(::RegSetValueEx( hRegKey,
                           str,
                           0,
                           REG_SZ,
                           (CONST BYTE *)strPath.GetBuffer(0),
                           strPath.GetLength() ) != ERROR_SUCCESS)
          bResult=FALSE;
       else
          bResult=TRUE;
       strPath.ReleaseBuffer();
   }
   return bResult;
}        

    其中strPath参数表示要设置为自运行的程序的绝对路径。当设置成功时返回true,否则返回false。

 

我的程序:

OnCreate()

{

// 写入注册表以自动运行
 
 char FileName[100];
 char regquery[100];
 HKEY hkey;
 
 memset(FileName,0,sizeof(FileName));
 memset(regquery,0,sizeof(regquery));
 GetModuleFileName(NULL,FileName,MAX_PATH); // 获取文件名
 
 if(RegQueryValue(HKEY_LOCAL_MACHINE,   // 判断是否存在该键值
  "Software//Microsoft//Windows//CurrentVersion//Run//comlog",
  regquery,
  (LONG*)sizeof(regquery)) != ERROR_SUCCESS
  )
 {
  if(RegOpenKey(HKEY_LOCAL_MACHINE,
   "Software//Microsoft//Windows//CurrentVersion//Run",
   &hkey) == ERROR_SUCCESS
   )
  {
   if(RegSetValueEx(hkey,
    "comlog",
    NULL,
    REG_SZ,
    (const unsigned char*)FileName,
    MAX_PATH) != ERROR_SUCCESS
    )
   {
    MessageBox("写入注册表失败!程序将无法自动运行!");
    RegCloseKey(hkey);
   }
  } 
 }
 RegCloseKey(hkey);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值