Windows writes to the registry since the startup key-value pair disappears

Windows writes to the registry since the startup key-value pair disappears:

  When a Windows program writes the program path into the registry and starts automatically, sometimes it may encounter the problem that the writing is successful but the registry is still blank. And random strings can be written normally, but the keys and values ​​will disappear directly when writing the program path.

Problem Description:

  I encountered the above situation when I first wrote MFC. Finally, by writing RunOnce, and writing a marked key value under Run, I bypassed the need to start automatically through Run, but each time I started it would retrieve the marked key-value pair of Run. To judge whether to rewrite RunOnce again is laborious and not straightforward.
  Later, when writing Qt, the method provided by Qt can be used to directly write Run, but then the same problem as MFC occurred. Finally, through various attempts, it was found that when writing ordinary strings, you can write directly. When writing the startup program path, first create a ""key with a value , and finally set the key to the actual value. Using this method can solve the problem of Run writing blank.

Cause Analysis:

  Later, I accidentally discovered that if there is already a key-value pair written again, the value will be updated and the key-value pair will remain, but sometimes it will not be added when using the program to delete and add, and basically follow the situation, so guess whether it is to write to the startup program again. Does the path require a specific key-value pair first?

solution:

  First create a value ""key under Run, and finally set the key to the actual value. Using this method can solve the problem of Run writing blank.

//参    数:(int) 参数int 0为取消自启动,1为设置自启动,2为查询自启动,自启动返回true,不是自启动返回false
bool tool::OperationAutoStart(int operation)
{
    
    
	//程序名称
	QString appName = QApplication::applicationName();
	// 程序路径
	QString application_path = QApplication::applicationFilePath();
	tool::PathConvert(application_path);

	application_path = "\"" + application_path + "\" auto";
	QSettings reg(
		"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",
		QSettings::NativeFormat);
	// 如果此键不存在,则返回的是空字符串
	QString val = reg.value(appName).toString();
	switch (operation) {
    
    
		//取消自启动
	case 0:
		//移除键
		reg.remove(appName);
		break;

	case 1:
		//设置自启动
		if (val != application_path) {
    
    
			//写入随意的字符串都能写入,但唯独自启动的程序路径写入自动成空,加入这个先添加空的值得解决了
			reg.setValue(appName, "\"\"");
			//设置键
			reg.setValue(appName, application_path);
		}
		break;

	case 2:
		//查询自启动
		if (val.compare(application_path) == 0) {
    
    
			reg.deleteLater();
			return true;
		}
		else {
    
    
			reg.deleteLater();
			return false;
		}
		break;
	default:
		reg.deleteLater();
		return false;
	}
	return false;
}

  Personally guess that different versions of the system may have different mechanisms for writing to the registry to start automatically. When the API writing of MFC or Windows system encounters this kind of problem, the solution should be the same, and I have not tested it again.

Guess you like

Origin blog.csdn.net/qq_44575789/article/details/111824042