VC++ 实现文件与应用程序关联
2016-04-11 18:11
603 查看
.h文件
.cpp文件
转载:http://www.cnblogs.com/RascallySnake/archive/2013/03/01/2939102.html
http://www.jb51.net/article/68189.htm
http://blog.csdn.net/sh031323/article/details/4683120
http://blog.sina.com.cn/s/blog_5d51f4e9010160mm.html
http://www.debugease.com/vc/2673556.html
#ifndef _FILERELATION_H_ #define _FILERELATION_H_ class CFileRelation { public: /* * 检测扩展名是存在于注册表 * strExt: 要检测的扩展名(例如: ".txt") *return:TRUE 表示已创建;FALSE 表示未创建 */ static BOOL CheckEncryptCloudKey(LPCTSTR strExt); /*在注册表中创建指定的扩展名 * strExt: 要检测的扩展名(例如: ".txt") * strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile") */ static void CreateFileExtKey(LPCTSTR strExt, LPCTSTR strAppKey); /* * 检测文件关联情况 * strExt: 要检测的扩展名(例如: ".txt") * strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile") * 返回TRUE: 表示已关联,FALSE: 表示未关联 */ static BOOL CheckFileRelation(LPCTSTR strExt, LPCTSTR strAppKey); /* * 如果CheckFileRelation检测已关联,我们可以重新指定关联程序 * strExt: 要检测的扩展名(例如: ".txt") * strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile") */ static void ReSetFileRelation(LPCTSTR strExt, LPCTSTR strAppKey); /* * 注册文件关联 * strExe: 要检测的扩展名(例如: ".txt") * strAppName: 要关联的应用程序名(例如: "C:/MyApp/MyApp.exe") * strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile") * strDefaultIcon: 扩展名为strAppName的图标文件(例如: *"C:/MyApp/MyApp.exe,0") * strDescribe: 文件类型描述 */ static void RegisterFileRelation(LPCTSTR strExt, LPCTSTR strAppName, LPCTSTR strAppKey, LPCTSTR strDefaultIcon, LPCTSTR strDescribe); }; #endif
.cpp文件
#include <winreg.h> #include "FileRelation.h" BOOL CFileRelation::CheckEncryptCloudKey(LPCTSTR strExt) { int nRet=FALSE; HKEY hExtKey; if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS) { RegCloseKey(hExtKey); nRet = TRUE; } return nRet; } void CFileRelation::CreateFileExtKey(LPCTSTR strExt, LPCTSTR strAppKey) { HKEY hExtKey; TCHAR szPath[_MAX_PATH]; memset(szPath,0,_MAX_PATH); DWORD dwSize=sizeof(szPath); if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS) { RegSetValue(hExtKey,_T(""),REG_SZ,strAppKey,_tcslen(strAppKey)+1); RegCloseKey(hExtKey); } } BOOL CFileRelation::CheckFileRelation(LPCTSTR strExt, LPCTSTR strAppKey) { int nRet=FALSE; HKEY hExtKey; TCHAR szPath[_MAX_PATH]; memset(szPath,0,_MAX_PATH); DWORD dwSize=sizeof(szPath); if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS) { RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_tcsicmp(szPath,strAppKey)==0) { nRet=TRUE; } RegCloseKey(hExtKey); return nRet; } return nRet; } void CFileRelation::ReSetFileRelation(LPCTSTR strExt, LPCTSTR strAppKey) { HKEY hExtKey; TCHAR szPath[_MAX_PATH]; memset(szPath,0,_MAX_PATH); DWORD dwSize=sizeof(szPath); if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS) { RegSetValue(hExtKey,_T(""),REG_SZ,strAppKey,_tcslen(strAppKey)+1); RegCloseKey(hExtKey); } } void CFileRelation::RegisterFileRelation(LPCTSTR strExt, LPCTSTR strAppName, LPCTSTR strAppKey, LPCTSTR strDefaultIcon, LPCTSTR strDescribe) { TCHAR strTemp[_MAX_PATH]; memset(strTemp,0,_MAX_PATH); HKEY hKey; RegCreateKey(HKEY_CLASSES_ROOT,strExt,&hKey); RegSetValue(hKey,_T(""),REG_SZ,strAppKey,_tcslen(strAppKey)+1); RegCloseKey(hKey); RegCreateKey(HKEY_CLASSES_ROOT,strAppKey,&hKey); RegSetValue(hKey,_T(""),REG_SZ,strDescribe,_tcslen(strDescribe)+1); RegCloseKey(hKey); _stprintf_s(strTemp,_T("%s\\DefaultIcon"),strAppKey); RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey); RegSetValue(hKey,_T(""),REG_SZ,strDefaultIcon,_tcslen(strDefaultIcon)+1); RegCloseKey(hKey); _stprintf_s(strTemp,_T("%s\\Shell"),strAppKey); RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey); RegSetValue(hKey,_T(""),REG_SZ,_T("Open"),_tcslen(_T("Open"))+1); RegCloseKey(hKey); _stprintf_s(strTemp,_T("%s\\Shell\\Open\\Command"),strAppKey); RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey); _stprintf_s(strTemp,_T("%s %%1"),strAppName); RegSetValue(hKey,_T(""),REG_SZ,strTemp,_tcslen(strTemp)+1); RegCloseKey(hKey); }
转载:http://www.cnblogs.com/RascallySnake/archive/2013/03/01/2939102.html
http://www.jb51.net/article/68189.htm
http://blog.csdn.net/sh031323/article/details/4683120
http://blog.sina.com.cn/s/blog_5d51f4e9010160mm.html
http://www.debugease.com/vc/2673556.html
相关文章推荐
- C++ int与string的转化
- C++头文件管理
- c++快速简易入门教程_011内联函数
- c++快速简易入门教程_010回调函数
- 【C/C++】计时函数比较
- c++快速简易入门教程_009引用
- c++快速简易入门教程_008函数的覆盖和隐藏
- c语言中的rewind函数,Win CE 不支持,可用fseek函数替换
- c++快速简易入门教程_007虚函数与多态性、纯虚函数
- 第3周 C语言及程序设计初步例程-46 小小型应用系统开发指导(一)
- c++创建txt文件,并写入内容
- C++作业3—本月有几天/定期存款利息计算器/个人所得税计算器
- c++标准模板库_联合容器_Bitsets详解
- c++标准模板库_容器适配器 _Queues(队列)详解
- c++标准模板库_容器适配器 _Stacks(堆栈)详解
- c++标准模板库_顺序结构_List详解
- c++标准模板库_顺序结构_Vectors详解
- c++迭代器Iterator详解
- c++指针详解
- c++实验3-个人所得税计算器