//设置程序关联.sas文件
void CSmileDesignApp::SetFileRelation()
{
TCHAR appPath[_MAX_PATH];
TCHAR appName[_MAX_PATH];
LPCTSTR NAME_APP = _T("SmileDesign");
LPCTSTR NAME_PROJECTFILE_EXT = _T(".sas");
if (GetModuleFileName(NULL, appPath, _MAX_PATH) != 0)
{
_stprintf_s(appName, _T("\"%s\" %%1"), appPath);
//检查注册表是否已经添加了,没有则添加
if (!CheckFileRelation(NAME_PROJECTFILE_EXT, appName, NAME_APP))
{
TCHAR icoPath[_MAX_PATH];
_stprintf_s(icoPath, _T("%s,0"), appPath);
RegisterFileRelation(NAME_PROJECTFILE_EXT, appName, NAME_APP, icoPath);
}
}
}
void CSmileDesignApp::RegisterFileRelation(LPCTSTR strExt, LPCTSTR strAppName, LPCTSTR strAppKey, LPCTSTR strDefaultIcon)
{
TCHAR strTemp[_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);
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);
RegSetValue(hKey, _T(""), REG_SZ, strAppName, _tcslen(strAppName) + 1);
RegCloseKey(hKey);
//通知文件关联已经更新
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
}
BOOL CSmileDesignApp::CheckFileRelation(LPCTSTR strExt, LPCTSTR strAppName, LPCTSTR strAppKey)
{
int nRet = FALSE;
HKEY hExtKey;
TCHAR strTemp[_MAX_PATH];
DWORD dwSize = sizeof(strTemp);
if (RegOpenKey(HKEY_CLASSES_ROOT, strExt, &hExtKey) == ERROR_SUCCESS)
{
RegQueryValueEx(hExtKey, NULL, NULL, NULL, (LPBYTE)strTemp, &dwSize);
RegCloseKey(hExtKey);
if (_tcscmp(strTemp, strAppKey) == 0)
{
_stprintf_s(strTemp, _T("%s\\Shell\\Open\\Command"), strAppKey);
if (RegOpenKey(HKEY_CLASSES_ROOT, strTemp, &hExtKey) == ERROR_SUCCESS)
{
dwSize = sizeof(strTemp);
RegQueryValueEx(hExtKey, NULL, NULL, NULL, (LPBYTE)strTemp, &dwSize);
if (_tcscmp(strTemp, strAppName) == 0)
{
nRet = TRUE;
}
RegCloseKey(hExtKey);
}
}
}
return nRet;
}