1. 文件关联和图标关联
例如关联.apk文件,则电脑上的所有后缀名为.apk的文件图标均变换成与关联程序一样的图标,并且双击.apk文件后,所关联的程序马上启动。其实没什么难的地方,只要细心地把指定的注册表下的值添加完成即可。
为了看着清楚,我将实现过程分成了两个函数,本文以.apk文件和程序MuMaYiInstaller.exe为例(关联关键词为mumayi):
函数1:
// 设置关联strPath所需的注册表环境,strPath为带路径的程序名
void CMuMaYiAgreement::SetAssociateEnvironment(CString
strPath)
{
// 设置路径HKEY_CLASSES_ROOT\\mumayi下的键值
HKEY hkey;
CString strResult("");
LPBYTE OwnerGet = new
BYTE[80];//定义用户姓名
memset(OwnerGet, 0, 80);
DWORD dType1 =
REG_SZ;//定义数据类型
DWORD dLength = 80;//定义数据长度
CString strValue("");
if ( RegOpenKey( HKEY_CLASSES_ROOT,
"mumayi\\Shell\\Open\\Command\\", &hkey ) ==
ERROR_SUCCESS )
{
RegQueryValueEx(hkey, NULL,
NULL, &dType1, OwnerGet,
&dLength);
strResult =
(LPTSTR)OwnerGet;
// 此处为了省事儿,直接写成了MuMaYiInstaller.exe,其实此处的应用名应该从strPath中解析出来
if ( -1 ==
strResult.Find("MuMaYiInstaller.exe\"
\"%%1\""))
{
strValue.Format("\"%s\"
\"%%1\"", strPath);
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
RegOpenKey(
HKEY_CLASSES_ROOT, "mumayi\\Shell\\Open",
&hkey);
strValue.Empty();
strValue.Format("用木蚂蚁安装器安装");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
RegOpenKey(
HKEY_CLASSES_ROOT, "mumayi", &hkey);
strValue.Empty();
strValue.Format("安装器程序");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
else
{
if ( RegCreateKey(
HKEY_CLASSES_ROOT, "mumayi\\Shell\\Open\\Command",
&hkey ) != ERROR_SUCCESS )
{
return;
}
else
{
strValue.Format("\"%s\"
\"%%1\"", strPath);
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
RegOpenKey(
HKEY_CLASSES_ROOT, "mumayi\\Shell\\Open",
&hkey);
strValue.Empty();
strValue.Format("用木蚂蚁安装器安装");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
RegOpenKey(
HKEY_CLASSES_ROOT, "mumayi", &hkey);
strValue.Empty();
strValue.Format("安装器程序");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
memset(OwnerGet, 0, 80);
if (strResult.IsEmpty())
strResult.Empty();
strValue.Empty();
if ( RegOpenKey( HKEY_CLASSES_ROOT,
"mumayi\\DefaultIcon\\", &hkey ) == ERROR_SUCCESS
)
{
RegQueryValueEx(hkey, NULL,
NULL, &dType1, OwnerGet,
&dLength);
strResult =
(LPTSTR)OwnerGet;
if ( -1 ==
strResult.Find("MuMaYiInstaller.exe,0"))
{
//
该关键字下没有我的软件相关名,修改之
strValue.Format("%s,0",
strPath);
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
else
{
if ( RegCreateKey(
HKEY_CLASSES_ROOT, "mumayi\\DefaultIcon", &hkey )
!= ERROR_SUCCESS )
{
return;
}
else
{
strValue.Format("%s,0",
strPath);
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
// 设置路径"HKEY_LOCAL_MACHINE\\SORTWARE\\Classes\\mumayi\\"下的值
memset(OwnerGet, 0, 80);
if (strResult.IsEmpty())
strResult.Empty();
strValue.Empty();
if ( RegOpenKey( HKEY_LOCAL_MACHINE,
"SORTWARE\\Classes\\mumayi\\Shell\\Open\\Command\\",
&hkey ) == ERROR_SUCCESS )
{
RegQueryValueEx(hkey, NULL,
NULL, &dType1, OwnerGet,
&dLength);
strResult =
(LPTSTR)OwnerGet;
if ( -1 ==
strResult.Find("MuMaYiInstaller.exe\" \"%%1\""))
{
strValue.Format("\"%s\"
\"%%1\"", strPath);
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
RegOpenKey(
HKEY_LOCAL_MACHINE, "SORTWARE\\Classes\\mumayi\\Shell\\Open\\",
&hkey);
strValue.Empty();
strValue.Format("用木蚂蚁安装器安装");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
RegOpenKey(
HKEY_LOCAL_MACHINE, "SORTWARE\\Classes\\mumayi\\",
&hkey);
strValue.Empty();
strValue.Format("安装器程序");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
else
{
if ( RegCreateKey(
HKEY_LOCAL_MACHINE,
"SOFTWARE\\Classes\\mumayi\\Shell\\Open\\Command",
&hkey ) != ERROR_SUCCESS )
{
return;
}
else
{
strValue.Format("\"%s\"
\"%%1\"", strPath);
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
RegOpenKey(
HKEY_LOCAL_MACHINE, "SOFTWARE\\Classes\\mumayi\\Shell\\Open\\",
&hkey);
strValue.Empty();
strValue.Format("用木蚂蚁安装器安装");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
RegOpenKey(
HKEY_LOCAL_MACHINE, "SOFTWARE\\Classes\\mumayi\\",
&hkey);
strValue.Empty();
strValue.Format("安装器程序");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
// 设置路径HKEY_CLASSES_ROOT\\mumayi下的键值
memset(OwnerGet, 0, 80);
if (strResult.IsEmpty())
strResult.Empty();
strValue.Empty();
if ( RegOpenKey( HKEY_CLASSES_ROOT,
"SOFTWARE\\Classes\\mumayi\\DefaultIcon\\", &hkey )
== ERROR_SUCCESS )
{
RegQueryValueEx(hkey, NULL,
NULL, &dType1, OwnerGet,
&dLength);
strResult =
(LPTSTR)OwnerGet;
if ( -1 ==
strResult.Find("MuMaYiInstaller.exe,0"))
{
//
该关键字下没有我的软件相关名,修改之
strValue.Format("%s,0",
strPath);
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
else
{
if ( RegCreateKey(
HKEY_CLASSES_ROOT, "SOFTWARE\\Classes\\mumayi\\DefaultIcon",
&hkey ) != ERROR_SUCCESS )
{
return;
}
else
{
strValue.Format("%s,0",
strPath);
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
// 通知系统,文件关联改变了
SHChangeNotify(SHCNE_ASSOCCHANGED,
SHCNF_IDLIST,NULL,NULL);
SHChangeNotify(SHCNE_UPDATEIMAGE,SHCNF_DWORD,NULL,NULL);
}
函数2:
//函数名称:AssociateApkFile
//函数功能:关联apk文件和图标
//输入参数:strType
文件类型 .apk
.mpk等
//
strPath 程序路径名 与文件关联的可执行程序的带路径的文件名
//返 回 值:NULL
//函数说明:
void CMuMaYiAgreement::AssociateFile(CString strType, CString
strPath)
{
//HKEY_LOCAL_MACHINE/Software/Classes/等于HKEY_CLASSES_ROOT
HKEY hkey;
CString
strKeyName("mumayi");
CString strResult("");
LPBYTE OwnerGet = new
BYTE[80];//定义用户姓名
memset(OwnerGet, 0, 80);
DWORD dType1 =
REG_SZ;//定义数据类型
DWORD dLength = 80;//定义数据长度
CString strValue("");
CString strRegPath("");
strRegPath = strType + "\\"; //
.apk
// 设置路径"HKEY_CLASS_ROOT\\.apk"的键值,关联.apk文件用
if ( RegOpenKey( HKEY_CLASSES_ROOT, strRegPath,
&hkey ) == ERROR_SUCCESS )
{
RegQueryValueEx(hkey, NULL,
NULL, &dType1, OwnerGet,
&dLength);
strResult =
(LPTSTR)OwnerGet;
if ( -1 ==
strResult.Find(CString("mumayi")))
{
strValue.Format("mumayi");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
else
{
if ( RegCreateKey(
HKEY_CLASSES_ROOT, strType, &hkey ) !=
ERROR_SUCCESS )
{
return;
}
else
{
strValue.Format("mumayi");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
// 设置"SOFTWARE\\Classes\\.apk"的键值
memset(OwnerGet, 0, 80);
if (strResult.IsEmpty())
strResult.Empty();
strValue.Empty();
strRegPath = CString("SOFTWARE\\Classes\\") +
strType + "\\"; //
SOFTWARE\\Classes\\.apk\\
if ( RegOpenKey( HKEY_LOCAL_MACHINE,
strRegPath, &hkey ) == ERROR_SUCCESS )
{
RegQueryValueEx(hkey, NULL,
NULL, &dType1, OwnerGet,
&dLength);
strResult =
(LPTSTR)OwnerGet;
if ( -1 ==
strResult.Find("mumayi"))
{
//
该关键字下没有我的软件相关名,修改之
strValue.Format("mumayi");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
else
{
if ( RegCreateKey(
HKEY_LOCAL_MACHINE, strRegPath, &hkey ) !=
ERROR_SUCCESS )
{
return;
}
else
{
strValue.Format("mumayi");
RegSetValue(hkey,
_T(""), REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
// 设置路径"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.apk\\UserChoice"的值
memset(OwnerGet, 0, 80);
if (strResult.IsEmpty())
strResult.Empty();
strValue.Empty();
strRegPath =
CString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\")
+ strType + "\\UserChoice";
if ( RegOpenKey( HKEY_CURRENT_USER,
strRegPath, &hkey ) == ERROR_SUCCESS )
{
RegQueryValueEx(hkey, NULL,
NULL, &dType1, OwnerGet,
&dLength);
strResult =
(LPTSTR)OwnerGet;
if ( -1 ==
strResult.Find("mumayi"))
{
//
该关键字下没有我的软件相关名,修改之
strValue.Format("mumayi");
RegSetValue(hkey,
"Progid", REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"Progid", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
else
{
if ( RegCreateKey(
HKEY_CURRENT_USER, strRegPath, &hkey ) !=
ERROR_SUCCESS )
{
return;
}
else
{
strValue.Format("mumayi");
RegSetValue(hkey,
"Progid", REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"Progid", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
memset(OwnerGet, 0, 80);
if (strResult.IsEmpty())
strResult.Empty();
strValue.Empty();
strRegPath =
CString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\")
+ strType;
if ( RegOpenKey( HKEY_CURRENT_USER, strRegPath,
&hkey ) == ERROR_SUCCESS )
{
RegQueryValueEx(hkey, NULL,
NULL, &dType1, OwnerGet,
&dLength);
strResult =
(LPTSTR)OwnerGet;
if ( -1 ==
strResult.Find("mumayi"))
{
//
该关键字下没有我的软件相关名,修改之
strValue.Format("mumayi");
RegSetValue(hkey,
"Progid", REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"Progid", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
else
{
if ( RegCreateKey(
HKEY_CURRENT_USER, strRegPath, &hkey ) !=
ERROR_SUCCESS )
{
return;
}
else
{
strValue.Format("mumayi");
RegSetValue(hkey,
"Progid", REG_SZ, strValue, strValue.GetLength()+1);
RegSetValueEx(hkey,
"Progid", 0, REG_SZ, (const
BYTE*)strValue.GetBuffer(strValue.GetLength()),
strValue.GetLength()+1);
RegCloseKey(hkey);
}
}
// 通知系统,文件关联改变了
SHChangeNotify(SHCNE_ASSOCCHANGED,
SHCNF_IDLIST,NULL,NULL);
SHChangeNotify(SHCNE_UPDATEIMAGE,SHCNF_DWORD,NULL,NULL);
}
2. 双击关联文件,关联程序启动后获取文件路径
在C**App::InitInstance()中,通过获取命令行来获得文件路径(不是C**Dlg::InitInstance()中):
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
CString strFilePathName =
cmdInfo.m_strFileName; // strFilePathName
即为带路径的文件名
【完】
加载中,请稍候......