MFC 设置开机自启动

在MFC 中设置开机自启

1)需要知道的基础:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run是控制计算机启动项的注册表信息,

 如果要找到它在何处的话:可以`点屏幕左下角(开始)→(运行)→输入(regedit)→这个时候你会看见注册表弹出来了,点选(HKEY_LOCAL_MACHINE)→(SOFTWARE)→(Microsoft)→(Windows)→(CurrentVersion)→(Run)``此时你可以看见右面有一些信息``这就是启动项信息``最好不要修改

2)RegOpenKey的使用

下面是MSDN中的:

LONG RegOpenKey(
  HKEY hKey,        // handle to open key
  LPCTSTR lpSubKey, // name of subkey to open
  PHKEY phkResult   // handle to open key
);
hKey
[in] Handle to a currently open key or one of the following predefined keys:
HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
Windows NT/2000/XP: HKEY_PERFORMANCE_DATA
Windows 95/98/Me: HKEY_DYN_DATA

The key opened by the RegOpenKey function is a subkey of the key identified by hKey.


这五个设置是计算机注册表的信息,分别表示如下:


1.HKEY_CLASSES_ROOT
管理文件系统。根据在Windows 98中安装的应用程序的扩展名,该根键指明其文件类型的名称,相应打开该文件所要调用的程序等等信息。

2.HKEY_CURRENT_USER
管理系统当前的用户信息。在这个根键中保存了本地计算机中存放的当前登录的用户信息,包括用户登录用户名和暂存的密码。在用户登录Windows 98时,其信息从HKEY_USERS中相应的项拷贝到HKEY_CURRENT_USER中。

3.HKEY_LOCAL_MACHINE
管理当前系统硬件配置。在这个根键中保存了本地计算机硬件配置数据,此根键下的子关键字包括在SYSTEM.DAT中,用来提供HKEY_LOCAL_MACHINE所需的信息,或者在远程计算机中可访问的一组键中。
这个根键里面的许多子键与System.ini文件中设置项类似。

4.HKEY_USERS
管理系统的用户信息。在这个根键中保存了存放在本地计算机口令列表中的用户标识和密码列表。同时每个用户的预配置信息都存储在HKEY_USERS根键中。HKEY_USERS是远程计算机中访问的根键之一。

5.HKEY_CURRENT_CONFIG
管理当前用户的系统配置。在这个根键中保存着定义当前用户桌面配置(如显示器等等)的数据,该用户使用过的文档列表(MRU),应用程序配置和其他有关当前用户的Windows 98中文版的安装的信息。

6.HKEY_DYN_DATA
管理系统运行数据。在这个根键中保存了系统在运行时的动态数据,此数据在每次显示时都是变化的,因此,此根键下的信息没有放在注册表中。

 


lpSubKey

[in] Pointer to a null-terminated string containing the name of the key to open. This key must be a subkey of the key identified by the hKey parameter. If this parameter is NULL or a pointer to an empty string, the function returns the same handle that was passed in.
这里是说需要打开键的整个路径,而且lpSubKey 是 hKey的subKey
phkResult
[out] Pointer to a variable that receives a handle to the opened key. When you no longer need the returned handle, call the RegCloseKey function to close it
输出值,为要打开键值的句柄
Return Values
If the function succeeds, the return value is ERROR_SUCCESS.

3)


LONG RegSetValueEx(
HKEY hKey,
LPCWSTR lpValueName,
DWORD Reserved,
DWORD dwType,
const BYTE *lpData,
DWORD cbData );
解释:
  hKey 当前打开的键句柄,或者以下的选择
HKEY_CLASSES_ROOT HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_USERS
  lpValueName:MSDN 解释:Pointer to a string containing the name of the value to set. If a value with this name is not already present in the key, the function adds it to the key. If this parameter is NULL or an empty string, the function sets the type and data for the key’s unnamed value. Registry keys do not have default values, but they can have one unnamed value, which can be of any type. The maximum length of a value name is 255, not including the terminating NULL character.(其实就是在注册表中的名字)

Reserved; must be zero.

dwType :REG_SZ A null-terminated string.

lpData
[in] Pointer to a buffer containing the data to be stored with the specified value name. (其实打开注册表看下,你就知道这些信息了)

cbData
[in] Specifies the size, in bytes, of the information pointed to by the lpData parameter. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character. The maximum size of data allowed in Windows CE is 4 KB.
了解了这些,下面给出代码实现:

在这之前还要在说明一个函数的使用,我们注册时,是想要在注册表中显示我们自己文件名的,这样方便看

那么我们需要在之前获得的路径名strPath中提取出文件名字,那么使用_splitpath.

在网上看到的使用方法,很简单理解:

char path_buffer[_MAX_PATH];

char drive[_MAX_DRIVE];

char dir[_MAX_DIR];

 char fname[_MAX_FNAME];

 char ext[_MAX_EXT];

 _makepath( path_buffer, "c", "//sample//crt//", "makepath", "c" );   //路径的合并

 printf( "Path created with _makepath: %s/n/n", path_buffer );     //路径的分解

 _splitpath( path_buffer, drive, dir, fname, ext );

 printf( "Path extracted with _splitpath:/n" );

 printf( " Drive: %s/n", drive );

 printf( " Dir: %s/n", dir );

printf( " Filename: %s/n", fname );

printf( " Ext: %s/n", ext );

实例实现:

TCHAR path[MAX_PATH];
 CString keyStr;
 CString fileName;
 HKEY hRegKey;
 GetModuleFileName(NULL,path,sizeof(path));
 CString strPath = path;
 keyStr =_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
 if(RegOpenKey(HKEY_LOCAL_MACHINE,keyStr,&hRegKey) !=  ERROR_SUCCESS)
  return ;
 else
 {
  //获得fileName
   _wsplitpath(strPath.GetBuffer(0),NULL,NULL,fileName.GetBufferSetLength(MAX_PATH+1),NULL);
  if(!::RegSetValueEx(hRegKey,
               fileName,
      0,
      REG_SZ,
      (CONST BYTE *)strPath.GetBuffer(0),
      strPath.GetLength())!= ERROR_SUCCESS)
      return;
  strPath.ReleaseBuffer();
 }

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值