⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.cpp

📁 QQ自动登陆器
💻 CPP
字号:
#include "common.h"

// 得到当前文件路径
void GetCurrentFile(CString& FullName, CString& FileName, CString& NameNoExt)
{
	char szFullPath[MAX_PATH];
	GetModuleFileName(NULL,szFullPath,MAX_PATH);
	char* pszModuleFileName = _tcsrchr(szFullPath, TEXT('\\'));
	char* pszFileNameExt = _tcsrchr(szFullPath, TEXT('.'));
	
	FullName = CString(szFullPath);
	pszModuleFileName++;
	FileName = CString(pszModuleFileName);
	pszFileNameExt[0] = '\0';
	NameNoExt = CString(pszModuleFileName);
}

// 得到当前目录
CString GetCurrentDir()
{
	char szFullPath[MAX_PATH];
	GetModuleFileName(NULL,szFullPath,MAX_PATH);
	char* pszModuleFileName = _tcsrchr(szFullPath, TEXT('\\'));
	pszModuleFileName[1]='\0';
	SetCurrentDirectory(szFullPath);
	CString CurrentDir(szFullPath);
	return CurrentDir;
}

// 得到配置文件地址
CString GetConfFile(LPCTSTR ConfigFileName)
{
	return GetCurrentDir()+ConfigFileName;
}

// 处理命令行
void RemoveQuota(CString& strCMD, BOOL PlainString)
{
	int iCmdIdx = strCMD.Find('/');
	if (-1 != iCmdIdx)
	{
		CString FileName = strCMD.Left(strCMD.Find('/'));
		if (FileName.Find('\"') != FileName.ReverseFind('\"'))
		{
			strCMD.TrimLeft(_T(" "));
			strCMD.TrimRight(_T(" "));
		}
		else
		{
			strCMD.TrimLeft(_T("\" "));
			strCMD.TrimRight(_T(" "));
			if (strCMD.Right(2) == "\"\"")
			{
				strCMD.TrimRight(_T("\""));
				strCMD+="\"";
			}
			else
			{
				strCMD.TrimRight(_T("\""));
			}
		}

	}
	else
	{
		strCMD.TrimLeft(_T("\" "));
		strCMD.TrimRight(_T("\" "));
	}
}

BOOL IsCMDEmpty(CString strCMD)
{
	CString FullName = "";
	CString FileName = "";
	CString NameNoExt = "";
	GetCurrentFile(FullName, FileName, NameNoExt);
	if (!strCMD.CompareNoCase(FullName)||!strCMD.CompareNoCase(FileName)||!strCMD.CompareNoCase(NameNoExt))
	{
		return TRUE;
	}
	return FALSE;
}

// (qqlogon.exe /runos /nownd /logon /exit /config:"D:\登陆器\conf.ini")
BOOL ParseCMD(CString strCMD, CString& RunWithOS, CString& AutoLogon, CString& AutoExit, CString& ConigFile)
{
	if (-1 == strCMD.Find("nownd") && -1 == strCMD.Find("logon") &&
		-1 == strCMD.Find("exit") && -1 == strCMD.Find("config:")&&
		-1 == strCMD.Find("/") )
	{
		return FALSE;
	}
	BOOL bNoWnd = FALSE;
	CStringArray CMDArgsList;
	CString CMDVarArgs = strCMD.Right(strCMD.GetLength() - strCMD.Find('/') -1);
	for (int iSplitPos = 0, i = 0; iSplitPos < CMDVarArgs.GetLength(); i++)
	{
		int iNextPos = CMDVarArgs.Find('/', iSplitPos);
		int iArgLen;
		if (iNextPos<0)
		{
			iArgLen = CMDVarArgs.GetLength() - iSplitPos;
			iNextPos = CMDVarArgs.GetLength();
		}
		else
		{
			iArgLen = iNextPos - iSplitPos-1;
		}
		CString sCMDArg = CMDVarArgs.Mid(iSplitPos, iArgLen);
		sCMDArg.TrimLeft(_T("\" "));
		sCMDArg.TrimRight(_T("\" "));
		for (int n = 0; n < CMDArgsList.GetSize(); n++)
		{
			if (!sCMDArg.CompareNoCase(CMDArgsList[n]))
			{
				// 重复命令参数
				return FALSE;
			}
		}
		CMDArgsList.Add(sCMDArg);
		if (!sCMDArg.CompareNoCase("runos"))
		{
			RunWithOS = "是";
		}
		else if (!sCMDArg.CompareNoCase("nownd"))
		{
			bNoWnd = TRUE;
		}
		else if (!sCMDArg.CompareNoCase("logon"))
		{
			AutoLogon = "是";
		}
		else if (!sCMDArg.CompareNoCase("exit"))
		{
			AutoExit = "是";
		} 
		else if (!(sCMDArg.Left(7)).CompareNoCase("config:"))
		{
			ConigFile = sCMDArg.Right(sCMDArg.GetLength()-7);
			RemoveQuota(ConigFile);
		}
		else
		{
			return FALSE;
		}
		
		iSplitPos = iNextPos+1;
	}
	if (bNoWnd)
	{
		AutoLogon = "是";
		AutoExit = "是";
	}

	return TRUE;
}

BOOL IsFileExist(CString FileToCheck)
{
	HANDLE hFile = CreateFile(FileToCheck,GENERIC_READ,FILE_SHARE_READ,NULL,
		OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if (INVALID_HANDLE_VALUE == hFile)
	{
		return FALSE;
	}
	else
	{
		CloseHandle(hFile);
		return TRUE;
	}
}

BOOL GetIniFileInt(CString SecName, CString KeyName, int& KeyData, CString ConfFile)
{
	KeyData = GetPrivateProfileInt(SecName,KeyName,0,ConfFile);
	if (0==KeyData)
	{
		return FALSE;
	}
	return TRUE;
}

BOOL GetIniFileStr(CString SecName, CString KeyName, CString& KeyData, CString ConfFile)
{
	char tmpStr[1024];
	GetPrivateProfileString(SecName,KeyName,"错误",tmpStr,1024,ConfFile);
	CString StrData(tmpStr);
	StrData.TrimLeft();
	StrData.TrimRight();
	if (StrData != "错误")
	{
		KeyData = StrData;
		return TRUE;
	}
	return FALSE;
}

BOOL WriteIniQQNbr(CString SecName, CString KeyName, int QQIdx, CString QQConfStr,
				   CString ConfFile)
{
	CString KeyString = "";
	KeyString.Format("%s[%d]",KeyName,QQIdx+1);
	if (!WritePrivateProfileString(SecName,KeyString,QQConfStr,ConfFile))
	{
		return FALSE;
	}
	return TRUE;
}

BOOL ReadIniQQList(CString SecName, CString KeyName, vector<QQInfo>& QQList,
				   CString ConfFile, int QQListLen)
{
	for (int i = 0; i < QQListLen; i++)
	{
		char tmpStr[1024];
		CString KeyString = "";
		KeyString.Format("%s[%d]",KeyName,i+1);
		GetPrivateProfileString(SecName,KeyString,"错误",tmpStr,1024,ConfFile);
		CString StrData(tmpStr);
		StrData.TrimLeft();
		StrData.TrimRight();
		if (StrData == "错误")
		{
			QQList.clear();
			return FALSE;
		}
		QQInfo QQNbr(StrData);
		QQList.push_back(QQNbr);
	}
	return TRUE;
}

BOOL WriteIniQQList(CString SecName, CString KeyName, vector<QQInfo>& QQList,
					CString ConfFile, BOOL ClearBeforeWrite)
{
	if (ClearBeforeWrite)
	{
		if (!WritePrivateProfileString(SecName,NULL,NULL,ConfFile))
		{
			return FALSE;
		}
	}
	for (int i = 0; i < QQList.size(); i++)
	{
		CString KeyString = "";
		KeyString.Format("%s[%d]",KeyName,i+1);
		if (!WritePrivateProfileString(SecName,KeyString,QQList[i].GetConfStr(),ConfFile))
		{
			return FALSE;
		}
	}
	return TRUE;
}

int CheckIniQQListLen(CString SecName, CString KeyName, CString ConfFile, int QQListLen)
{
	for (int i = 0; i < QQListLen; i++)
	{
		char tmpStr[1024];
		CString KeyString = "";
		KeyString.Format("%s[%d]",KeyName,i+1);
		GetPrivateProfileString(SecName,KeyString,"错误",tmpStr,1024,ConfFile);
		CString StrData(tmpStr);
		StrData.TrimLeft();
		StrData.TrimRight();
		if (StrData == "错误")
		{
			return i;
		}
	}
	return QQListLen;
}

void ConfigFileError()
{
	AfxMessageBox("配置文件读取失败!请检查文件是否在用.\r如果配置文件不能恢复,请删除!");
}

void RunQQ(CString QQPath, CString QQLogonStr)
{
	ShellExecute(NULL, NULL, QQPath, QQLogonStr, NULL, SW_SHOWNORMAL);
}

BOOL WriteIniHelpInfo(CString SecName, CString KeyName, CString ConfFile)
{
	CStringArray HelpInfoList;
	HelpInfoList.Add("登陆器帮助");
	HelpInfoList.Add("可以从命令行运行,各参数:[runos](随机启动),[logon](启动后自动登陆QQ),");
	HelpInfoList.Add("[exit](登陆QQ后自动退出),[nownd](logon+exit,启动后登陆QQ并退出)");
	HelpInfoList.Add("[config:](指定配置文件)");
	HelpInfoList.Add("命令行例子: qqlogon.exe /runos /nownd /logon /exit /config:\"D:\\登陆器\\conf.ini\"");
	HelpInfoList.Add("注意: 命令行参数将覆盖配置文件的参数!");
	for (int i = 0; i < HelpInfoList.GetSize(); i++)
	{
		CString KeyString = "";
		KeyString.Format("%s[%d]",KeyName,i+1);
		if (!WritePrivateProfileString(SecName,KeyString,HelpInfoList[i],ConfFile))
		{
			return FALSE;
		}
	}
	return TRUE;
}

BOOL FindQQ(CString& QQPath)
{
	if (FindInPS(QQPath))
	{
		//AfxMessageBox("根据进程找到QQ位置!");
		return TRUE;
	}
	else if (FindQQInstallPath(QQPath))
	{
		//AfxMessageBox("根据注册表找到QQ位置!");
		return TRUE;
	}
	else if (FindInFixPath(QQPath))
	{
		//AfxMessageBox("根据常见固定位置找到QQ位置!");
		return TRUE;
	}
	return FALSE;
}

//[HKEY_LOCAL_MACHINE\SOFTWARE\TENCENT\PLATFORM_TYPE_LIST\1]
//"TypePath"="D:\\Program Files\\Tencent\\QQ\\QQ.exe"
//HKEY_CLASSES_ROOT\TypeLib\{C40D0EA2-EDF9-4DB1-BA6F-68FA08B52625}\1.0\0\win32
//HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
//HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
//HKEY_LOCAL_MACHINE\SOFTWARE\TENCENT
//HKEY_LOCAL_MACHINE\SOFTWARE\TENCENT\QQ
//HKEY_CLASSES_ROOT\\TypeLib\\{C40D0EA2-EDF9-4DB1-BA6F-68FA08B52625}\\1.0\\0\\win32
//HKEY_LOCAL_MACHINE\\SOFTWARE\\TENCENT\\PLATFORM_TYPE_LIST\\1
BOOL FindQQInstallPath(CString& QQPath)
{
	vector<HKEY> HKEYList;
	vector<CString> RegKeyList;
	vector<CString> KeyValueList;

	HKEYList.push_back(HKEY_CLASSES_ROOT);
	RegKeyList.push_back("TypeLib\\{C40D0EA2-EDF9-4DB1-BA6F-68FA08B52625}\\1.0\\0\\win32");
	KeyValueList.push_back("");

	HKEYList.push_back(HKEY_LOCAL_MACHINE);
	RegKeyList.push_back("SOFTWARE\\TENCENT\\PLATFORM_TYPE_LIST\\1");
	KeyValueList.push_back("TypePath");
	
	CString QQInstallPath = "";
	for (int i = 0; i < HKEYList.size(); i++)
	{
		// 如果找到QQ安装位置记录
		if (FindInReg(HKEYList[i], RegKeyList[i], KeyValueList[i], QQInstallPath))
		{
			// 检查QQ位置是否存在
			if (IsFileExist(QQInstallPath))
			{
				QQPath = QQInstallPath;
				return TRUE;
			}
		}
	}
	return FALSE;
}

BOOL FindInPS(CString& InstallPath)
{
	PROCESSENTRY32   pe; 
	pe.dwSize = sizeof(PROCESSENTRY32); 
	HANDLE hPeSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 
	for(BOOL bOk=Process32First(hPeSnapshot,&pe);bOk;bOk=Process32Next(hPeSnapshot,&pe)) 
	{
		if (!_stricmp("QQ.exe",pe.szExeFile))
		{
			HANDLE hMeSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,pe.th32ProcessID); 
			MODULEENTRY32 me={sizeof(me)}; 
			for(BOOL fOk=Module32First(hMeSnapshot,&me);fOk;fOk=Module32Next(hMeSnapshot,&me)) 
			{
				char* szQQExeFile= strrchr(me.szExePath,'\\');
				if (NULL != szQQExeFile && !_stricmp("\\QQ.exe",szQQExeFile))
				{
					InstallPath = CString(me.szExePath);
					CloseHandle(hMeSnapshot);
					CloseHandle(hPeSnapshot);
					return TRUE;
				} 
			}
			CloseHandle(hMeSnapshot);
			break;
		}
	}
	CloseHandle(hPeSnapshot);
	return FALSE;
}

BOOL FindInReg(HKEY hRoot, CString QQInstallPathKey, CString KeyValueName, CString& InstallPath)
{
	CRegKey QQInstallKey;
	if (ERROR_SUCCESS != QQInstallKey.Open(hRoot,QQInstallPathKey))	
	{
		QQInstallKey.Close();
		return FALSE;
	}
	char KeyValue[1024];
	DWORD KeyValueLen = 1024;
	if (ERROR_SUCCESS != QQInstallKey.QueryValue(KeyValue, KeyValueName, &KeyValueLen))
	{
		QQInstallKey.Close();
		return FALSE;
	}
	QQInstallKey.Close();
	InstallPath = CString(KeyValue);
	return TRUE;
}

BOOL FindInFixPath(CString& InstallPath)
{
	vector<CString> VolumeList;
	vector<CString> FixPathList;
	VolumeList.push_back("C:\\");
	VolumeList.push_back("D:\\");
	VolumeList.push_back("E:\\");
	VolumeList.push_back("F:\\");
	VolumeList.push_back("G:\\");
	VolumeList.push_back("H:\\");
	FixPathList.push_back("Program Files\\Tencent\\QQ\\QQ.exe");
	FixPathList.push_back("Program Files\\QQ\\QQ.exe");
	FixPathList.push_back("Program Files\\QQ2007\\QQ.exe");
	FixPathList.push_back("Tencent\\QQ\\QQ.exe");
	FixPathList.push_back("QQ2007\\QQ.exe");
	FixPathList.push_back("QQ\\QQ.exe");
	FixPathList.push_back("新建文件夹\\Tencent\\QQ\\QQ.exe");
	FixPathList.push_back("新建文件夹\\QQ\\QQ.exe");
	FixPathList.push_back("新建文件夹\\QQ2007\\QQ.exe");
	FixPathList.push_back("新建文件夹\\QQ.exe");
	for (int i = 0; i < VolumeList.size(); i++)
	{
		for (int j = 0; j < FixPathList.size(); j++)
		{
			CString QQPath = VolumeList[i]+FixPathList[j];
			if (IsFileExist(QQPath))
			{
				InstallPath = QQPath;
				return TRUE;
			}
		}
	}
	return FALSE;
}

BOOL SetQQAutoRun(CString ConfigFileName, BOOL IsDelAutorun)
{
	CString ConfigFileCMD = " /config:\""+ConfigFileName+"\"";
	CRegKey QQLogonKey;
	HKEY hRoot = HKEY_LOCAL_MACHINE;
	CString RunOnce = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
	CString ValueName = "QQ自动登陆器";
	if (!ConfigFileName.IsEmpty())
	{
		ConfigFileName.Replace(':', '_');
		ConfigFileName.Replace(' ', '_');
		ConfigFileName.Replace('.', '_');
		ConfigFileName.Replace('\\', '_');
		ValueName+="_配置_";
		ValueName+=ConfigFileName;
	}
	
	// 取消自启动设置
	if (IsDelAutorun)
	{
		if (ERROR_SUCCESS != QQLogonKey.Open(hRoot,RunOnce))	
		{
			QQLogonKey.Close();
			return FALSE;
		}
		if (ERROR_SUCCESS != QQLogonKey.DeleteValue(ValueName))
		{
			QQLogonKey.Close();
			return FALSE;
		}
		QQLogonKey.Close();
		return TRUE;
	}

	CString FullName;
	CString FileName;
	CString NameNoExt;
	GetCurrentFile(FullName, FileName, NameNoExt);

	//CString Value = "\"D:\\My Documents\\Work\\QQ自动登陆器.exe\" /config:\"d:\\s.ini\"";
	CString Value = "\""+FullName+"\""+ConfigFileCMD;
	if (ERROR_SUCCESS != QQLogonKey.SetValue(hRoot, RunOnce, Value, ValueName))
	{
		QQLogonKey.Close();
		return FALSE;
	}
	QQLogonKey.Close();
	return TRUE;
}

BOOL CALLBACK QQEnumWindowsProc(HWND hwnd,LPARAM lparam)
{
	if (IsWindowVisible(hwnd))
	{
		return TRUE;
	}
	char szTitle[256];
	::GetWindowText(hwnd,szTitle,256-1);
	CString strWinTitle(szTitle);
	strWinTitle.MakeLower();
	CString strMark = "d414641a-643f-4547-8459-d36f7ee9bb0d";
	int iQQidx = strWinTitle.Find("qq.exe");
	if(iQQidx!=-1)
	{
		CString QQNbr = strWinTitle.Right(strWinTitle.GetLength()-(iQQidx+6+strMark.GetLength()));
		vector<CString>* LogonedQQList = ((vector<CString>*)lparam);
		LogonedQQList->push_back(QQNbr);
	}
	return TRUE;
}

BOOL FindLogonedQQ(vector<CString>& LogonedQQList)
{
	vector<CString>& QQList = LogonedQQList;
	::EnumWindows(QQEnumWindowsProc,(LPARAM)&QQList) ;
	if (0 < LogonedQQList.size())
	{
		return TRUE;
	}
	return FALSE;
}

BOOL IsInLogonedList(CString QQNbr, vector<CString>& LogonedQQList)
{
	for (int i = 0; i < LogonedQQList.size(); i++)
	{
		if (!QQNbr.CompareNoCase(LogonedQQList[i]))
		{
			//CString test = "QQ号: " + QQNbr + " 已经登陆!";
			return TRUE;
		}
	}
	return FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -