您的位置:首页 > 编程语言 > C语言/C++

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

2016-04-11 18:11 603 查看
.h文件

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: