FileAssociation.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:6k
源码类别:
P2P编程
开发平台:
Visual C++
- // FileAssociation.cpp: implementation of the CFileAssociation class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testbt.h"
- #include "FileAssociation.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- void ShowRetErrorMsg(long lResult)
- {
- #ifdef _DEBUG
- LPVOID lpMsgBuf;
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- lResult,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL
- );
- // Display the string.
- ::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
- #endif
- assert(false);
- }
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CFileAssociation::CFileAssociation(CString strFileTypeId, CString strFilterExt, CString strShellMenuName)
- {
- m_strFileTypeId = strFileTypeId;
- m_strFilterExt = strFilterExt;
- m_strShellMenuName = strShellMenuName;
- }
- CFileAssociation::~CFileAssociation()
- {
- }
- bool CFileAssociation::MakeAssocaition(bool bRegister) const
- {
- // CString strFileTypeId = "TestBT.Document";
- // CString strFilterExt = ".torrent";
- assert(!m_strFilterExt.IsEmpty() && m_strFilterExt[0] == '.');
- assert(!m_strFileTypeId.IsEmpty() && m_strFileTypeId.Find(' ') == -1); // no spaces allowed
- CString strTemp;
- LONG lSize = _MAX_PATH * 2;
- LONG lResult = ::RegQueryValue(HKEY_CLASSES_ROOT, m_strFilterExt,
- strTemp.GetBuffer(lSize), &lSize);
- if (lResult != ERROR_SUCCESS || strTemp.IsEmpty() ||
- strTemp != m_strFileTypeId)
- {
- if (bRegister)
- {
- // no association for that suffix
- if (::RegSetValue(HKEY_CLASSES_ROOT, m_strFilterExt, REG_SZ,
- m_strFileTypeId, m_strFileTypeId.GetLength() * sizeof(TCHAR)) != ERROR_SUCCESS)
- {
- return false;
- }
- return true;
- }
- }
- if (lResult == ERROR_SUCCESS && strTemp == m_strFileTypeId && !bRegister)
- {
- HKEY key = 0;
- LONG lResult = RegOpenKey(HKEY_CLASSES_ROOT, m_strFilterExt, &key);
- if (lResult == ERROR_SUCCESS)
- {
- int iRet = ::RegDeleteValue(key, NULL);
- if (iRet != ERROR_SUCCESS )
- {
- assert(false);
- ShowRetErrorMsg(iRet);
- }
- }
- ::RegCloseKey(key);
- }
- SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
- return true;
- }
- bool CFileAssociation::MakeGenerateTorrentMenu(bool bRegister) const
- {
- // CString strMenuPath = "*\shell\制作torrent(神五BT)\command";
- // CString strDirectoryPath = "Directory\shell\制作torrent(神五BT)\command";
- assert(!m_strShellMenuName.IsEmpty());
- CString strMenuPath = "*\shell\" + m_strShellMenuName + "\command";
- CString strDirectoryPath = "Directory\shell\" + m_strShellMenuName + "\command";
- CString strToDelMenuPath = "*\shell\" + m_strShellMenuName;
- CString strToDelDirectoryPath = "Directory\shell\" + m_strShellMenuName;
- CString strPathName ;
- GetShortFileName(AfxGetInstanceHandle(), strPathName);
- CString strCommand = """ + strPathName + """ + " /t "+ " "%1" " ;
- CString strTemp;
- LONG lSize = _MAX_PATH * 2;
- LONG lResult = ::RegQueryValue(HKEY_CLASSES_ROOT, strMenuPath,
- strTemp.GetBuffer(lSize), &lSize);
- if (lResult != ERROR_SUCCESS || strTemp != strCommand)
- {
- if (bRegister)
- {
- if (::RegSetValue(HKEY_CLASSES_ROOT, strMenuPath, REG_SZ,
- strCommand, strCommand.GetLength() * sizeof(TCHAR)) != ERROR_SUCCESS)
- {
- return false;
- }
- }
- }
- else
- {
- if (!bRegister)
- {
- // lResult = ::RegQueryValue(HKEY_CLASSES_ROOT, strMenuPath, strTemp.GetBuffer(lSize), &lSize);
- //*
- lResult = ::RegDeleteKey(HKEY_CLASSES_ROOT, strMenuPath);
- if (lResult != ERROR_SUCCESS )
- {
- ShowRetErrorMsg(lResult);
- }
- lResult = ::RegDeleteKey(HKEY_CLASSES_ROOT, strToDelMenuPath);
- if (lResult != ERROR_SUCCESS )
- {
- ShowRetErrorMsg(lResult);
- }
- //*/
- }
- }
- lResult = ::RegQueryValue(HKEY_CLASSES_ROOT, strDirectoryPath,
- strTemp.GetBuffer(lSize), &lSize);
- if (lResult != ERROR_SUCCESS || strTemp != strCommand)
- {
- if (bRegister)
- {
- if (::RegSetValue(HKEY_CLASSES_ROOT, strDirectoryPath, REG_SZ,
- strCommand, strCommand.GetLength() * sizeof(TCHAR)) != ERROR_SUCCESS)
- {
- return false;
- }
- }
- }
- else
- {
- if (!bRegister)
- {
- lResult = ::RegDeleteKey(HKEY_CLASSES_ROOT, strDirectoryPath);
- if (lResult != ERROR_SUCCESS )
- {
- ShowRetErrorMsg(lResult);
- }
- lResult = ::RegDeleteKey(HKEY_CLASSES_ROOT, strToDelDirectoryPath);
- if (lResult != ERROR_SUCCESS )
- {
- ShowRetErrorMsg(lResult);
- }
- }
- }
- return true;
- }
- void CFileAssociation::GetShortFileName(HINSTANCE hInst, CString& strShortName) const
- {
- TCHAR szLongPathName[_MAX_PATH];
- ::GetModuleFileName(hInst, szLongPathName, _MAX_PATH);
- if (::GetShortPathName(szLongPathName,
- strShortName.GetBuffer(_MAX_PATH), _MAX_PATH) == 0)
- {
- // rare failure case (especially on not-so-modern file systems)
- strShortName = szLongPathName;
- }
- strShortName.ReleaseBuffer();
- }
- BOOL CFileAssociation::SetRegKeyMy(LPCTSTR lpszKey, LPCTSTR lpszValue, LPCTSTR lpszValueName)
- {
- if (lpszValueName == NULL)
- {
- if (::RegSetValue(HKEY_CLASSES_ROOT, lpszKey, REG_SZ,
- lpszValue, lstrlen(lpszValue) * sizeof(TCHAR)) != ERROR_SUCCESS)
- {
- TRACE1("Warning: registration database update failed for key '%s'.n",
- lpszKey);
- return FALSE;
- }
- return TRUE;
- }
- else
- {
- HKEY hKey;
- if(::RegCreateKey(HKEY_CLASSES_ROOT, lpszKey, &hKey) == ERROR_SUCCESS)
- {
- LONG lResult = ::RegSetValueEx(hKey, lpszValueName, 0, REG_SZ,
- (CONST BYTE*)lpszValue, (lstrlen(lpszValue) + 1) * sizeof(TCHAR));
- if(::RegCloseKey(hKey) == ERROR_SUCCESS && lResult == ERROR_SUCCESS)
- return TRUE;
- }
- TRACE1("Warning: registration database update failed for key '%s'.n", lpszKey);
- return FALSE;
- }
- }