http://www.cnblogs.com/limaoshengcpp/archive/2012/03/21/2409474.html
一般来说可以通过写注册表的方式实现,在函数入口处实现功能。
例如在VC/MFC中,可以在应用程序的C**App.cpp文件中InitInstance()函数中实现该功能。
using namespace std;
//关联文件的后缀名,如”txt”、”doc”等
string m_csExtension;
string m_csShellOpenCommand;
string m_csDocumentShellOpenCommand;
//注册表中文件夹类名
string m_csDocumentClassName;
//关联文件的默认图标
string m_csDocumentDefaultIcon;
///赋值函数//
void SetExtension( LPCTSTR szExtension )
{
m_csExtension = szExtension;
}
void SetShellOpenCommand( LPCTSTR szShellOpenCommand )
{
m_csShellOpenCommand = szShellOpenCommand;
}
void SetDocumentShellOpenCommand( LPCTSTR szDocumentShellOpenCommand )
{
m_csDocumentShellOpenCommand = szDocumentShellOpenCommand;
}
void SetDocumentClassName( LPCTSTR szDocumentClassName )
{
m_csDocumentClassName = szDocumentClassName;
}
void SetDocumentDefaultIcon( LPCTSTR szDocumentDefaultIcon )
{
m_csDocumentDefaultIcon = szDocumentDefaultIcon;
}
///赋值函数//
//关键函数:实现写注册表的函数
BOOL SetRegistryValue(
HKEY hOpenKey,
LPCTSTR szKey,
LPCTSTR szValue,
LPCTSTR szData
){
// validate input
if( !hOpenKey || !szKey || !szKey[0] ||
!szValue || !szData ){
::SetLastError(E_INVALIDARG);
return FALSE;
}
BOOL bRetVal = FALSE;
DWORD dwDisposition;
DWORD dwReserved = 0;
HKEY hTempKey = (HKEY)0;
// length specifier is in bytes, and some TCHAR
// are more than 1 byte each
DWORD dwBufferLength = lstrlen(szData) * sizeof(TCHAR);
// Open key of interest
// Assume all access is okay and that all keys will be stored to file
// Utilize the default security attributes
if( ERROR_SUCCESS == ::RegCreateKeyEx(hOpenKey, szKey, dwReserved,
(LPTSTR)0, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, 0,
&hTempKey, &dwDisposition) ){
// dwBufferLength must include size of terminating nul
// character when using REG_SZ with RegSetValueEx function
dwBufferLength += sizeof(TCHAR);
if( ERROR_SUCCESS == ::RegSetValueEx(hTempKey, (LPTSTR)szValue,
dwReserved, REG_SZ, (LPBYTE)szData, dwBufferLength) ){
bRetVal = TRUE;
}
}
// close opened key
if( hTempKey ){
::RegCloseKey(hTempKey);
}
return bRetVal;
}
BOOL RegSetExtension(void)
{
if( m_csExtension.empty() )
{
return FALSE;
}
std::string csKey = “.” + m_csExtension;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), “”, m_csDocumentClassName.c_str());
if( !m_csShellOpenCommand.empty() )
{
csKey += “\shell\open\command”;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), “”, m_csShellOpenCommand.c_str());
}
return TRUE;
}
BOOL RegSetDocumentType(void)
{
if( m_csDocumentClassName.empty())
{
return FALSE;
}
std::string csKey = m_csDocumentClassName;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), “”, m_csDocumentDescription.c_str());
// DefaultIcon
if( !m_csDocumentDefaultIcon.empty() )
{
csKey = m_csDocumentClassName;
csKey += “\DefaultIcon”;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), “”, m_csDocumentDefaultIcon.c_str());
}
// shell\open\command
if( !m_csShellOpenCommand.empty() )
{
csKey = m_csDocumentClassName;
csKey += “\shell\open\command”;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), “”, m_csShellOpenCommand.c_str());
}
return TRUE;
}
BOOL RegSetAllInfo(void)
{
RegSetExtension();
RegSetDocumentType();
return TRUE;
}
//注册程序与文件后缀名的关联
void RegisterFileAndProgram()
{
一个应用程序与多个文件后缀关联
LPCTSTR strExtension[] =
{
“bmp”,
“jpg”,
“jpe”,
“jpeg”
};
CGCFileTypeAccess TheFTA;
TCHAR szProgPath[MAX_PATH * 2];
//获取程序路径
::GetModuleFileName(NULL, szProgPath, sizeof(szProgPath)/sizeof(TCHAR));
CString csTempText;
for(int i = 0; i < strExternsionLength; ++i)
{
//设置程序需要关联的后缀名,如”txt” “doc” 等
SetExtension(strExtension[i]);
csTempText.Format(“\”%s\” %s”,szProgPath,”\”%1\”“);
SetShellOpenCommand(csTempText);
SetDocumentShellOpenCommand(csTempText);
//设置注册表中文件类的别名,例如可以是程序名称:**.exe
SetDocumentClassName(“**”);
// use first icon in program
csTempText = szProgPath;
csTempText += “,0”;
SetDocumentDefaultIcon(csTempText);
RegSetAllInfo();
}
}
//入口函数:初始化所需的操作
BOOL C**App::InitInstance()
{
//
//Code
//注册程序与文件后缀名的关联
RegisterFileAndProgram();
// 分析标准外壳命令、DDE、打开文件操作的命令行
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
//获取命令行传过来的参数:双击打开的文件的文件路径名称
CString strFilePathName = cmdInfo.m_strFileName;
// 调度在命令行中指定的命令。如果
// 用 /RegServer、/Register、/Unregserver 或 /Unregister 启动应用程序,则返回 FALSE。
if (!ProcessShellCommand(cmdInfo))
return FALSE;
//
//通过获取的strFilePathName文件名称实现相关操作//
//Add code here//
return TRUE;
}
复制代码