赞助

VC++ 实现文件与应用程序关联

.h文件

 1 #ifndef _FILERELATION_H_
 2 #define _FILERELATION_H_
 3 
 4 class CFileRelation
 5 {
 6 public:
 7 
 8     /*
 9     * 检测扩展名是存在于注册表
10     * strExt: 要检测的扩展名(例如: ".txt")
11     *return:TRUE 表示已创建;FALSE 表示未创建
12     */
13     static BOOL CheckEncryptCloudKey(LPCTSTR strExt);
14 
15     /*在注册表中创建指定的扩展名
16     * strExt: 要检测的扩展名(例如: ".txt")
17     * strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile")
18     */
19 
20     static void CreateFileExtKey(LPCTSTR strExt, LPCTSTR strAppKey);
21 
22     /*
23     * 检测文件关联情况
24     * strExt: 要检测的扩展名(例如: ".txt")
25     * strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile")
26     * 返回TRUE: 表示已关联,FALSE: 表示未关联
27     */
28     static BOOL CheckFileRelation(LPCTSTR strExt, LPCTSTR strAppKey);
29 
30     /*
31     * 如果CheckFileRelation检测已关联,我们可以重新指定关联程序
32     * strExt: 要检测的扩展名(例如: ".txt")
33     * strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile")
34     */
35     static void ReSetFileRelation(LPCTSTR strExt, LPCTSTR strAppKey);
36 
37 
38     /*
39     * 注册文件关联
40     * strExe: 要检测的扩展名(例如: ".txt")
41     * strAppName: 要关联的应用程序名(例如: "C:/MyApp/MyApp.exe")
42     * strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile")
43     * strDefaultIcon: 扩展名为strAppName的图标文件(例如: *"C:/MyApp/MyApp.exe,0")
44     * strDescribe: 文件类型描述
45     */
46     static void RegisterFileRelation(LPCTSTR strExt, LPCTSTR strAppName, LPCTSTR strAppKey, LPCTSTR strDefaultIcon, LPCTSTR strDescribe);
47 };
48 #endif

.cpp文件

 1 #include <winreg.h>
 2 #include "FileRelation.h"
 3 
 4 BOOL CFileRelation::CheckEncryptCloudKey(LPCTSTR strExt)
 5 {
 6     int nRet=FALSE;
 7     HKEY hExtKey;
 8 
 9     if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS)
10     {
11         RegCloseKey(hExtKey);
12         nRet = TRUE;
13     }
14 
15     return nRet;
16 }
17 
18 void CFileRelation::CreateFileExtKey(LPCTSTR strExt, LPCTSTR strAppKey)
19 {
20     HKEY hExtKey;
21     TCHAR szPath[_MAX_PATH]; 
22     memset(szPath,0,_MAX_PATH);
23     DWORD dwSize=sizeof(szPath); 
24     if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS)
25     {
26         RegSetValue(hExtKey,_T(""),REG_SZ,strAppKey,_tcslen(strAppKey)+1);
27         RegCloseKey(hExtKey);
28     }
29 
30 }
31 
32 BOOL CFileRelation::CheckFileRelation(LPCTSTR strExt, LPCTSTR strAppKey)
33 {
34     int nRet=FALSE;
35     HKEY hExtKey;
36     TCHAR szPath[_MAX_PATH]; 
37     memset(szPath,0,_MAX_PATH);
38     DWORD dwSize=sizeof(szPath); 
39     if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS)
40     {
41         RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
42         if(_tcsicmp(szPath,strAppKey)==0)
43         {
44             nRet=TRUE;
45         }
46         RegCloseKey(hExtKey);
47         return nRet;
48     }
49     return nRet;
50 }
51 
52 void CFileRelation::ReSetFileRelation(LPCTSTR strExt, LPCTSTR strAppKey)
53 {
54     HKEY hExtKey;
55     TCHAR szPath[_MAX_PATH]; 
56     memset(szPath,0,_MAX_PATH);
57     DWORD dwSize=sizeof(szPath); 
58     if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS)
59     {
60         RegSetValue(hExtKey,_T(""),REG_SZ,strAppKey,_tcslen(strAppKey)+1);
61         RegCloseKey(hExtKey);
62     }
63 }
64 
65 void CFileRelation::RegisterFileRelation(LPCTSTR strExt, LPCTSTR strAppName, LPCTSTR strAppKey, LPCTSTR strDefaultIcon, LPCTSTR strDescribe)
66 {
67     TCHAR strTemp[_MAX_PATH];
68     memset(strTemp,0,_MAX_PATH);
69     HKEY hKey;
70 
71     RegCreateKey(HKEY_CLASSES_ROOT,strExt,&hKey);
72     RegSetValue(hKey,_T(""),REG_SZ,strAppKey,_tcslen(strAppKey)+1);
73     RegCloseKey(hKey);
74 
75     RegCreateKey(HKEY_CLASSES_ROOT,strAppKey,&hKey);
76     RegSetValue(hKey,_T(""),REG_SZ,strDescribe,_tcslen(strDescribe)+1);
77     RegCloseKey(hKey);
78 
79     _stprintf_s(strTemp,_T("%s\\DefaultIcon"),strAppKey);
80     RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
81     RegSetValue(hKey,_T(""),REG_SZ,strDefaultIcon,_tcslen(strDefaultIcon)+1);
82     RegCloseKey(hKey);
83 
84     _stprintf_s(strTemp,_T("%s\\Shell"),strAppKey);
85     RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
86     RegSetValue(hKey,_T(""),REG_SZ,_T("Open"),_tcslen(_T("Open"))+1);
87     RegCloseKey(hKey);
88 
89     _stprintf_s(strTemp,_T("%s\\Shell\\Open\\Command"),strAppKey);
90     RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
91     _stprintf_s(strTemp,_T("%s %%1"),strAppName);
92     RegSetValue(hKey,_T(""),REG_SZ,strTemp,_tcslen(strTemp)+1);
93     RegCloseKey(hKey);
94 }

转载: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

posted @ 2016-04-11 18:11  车臣  阅读(680)  评论(0编辑  收藏  举报