VC++ implements an example of a method to add file associations

  • 2020-05-27 06:51:02
  • OfStack

The example in this article shows how VC++ implements the method of adding file associations. I will share it with you for your reference as follows:


//  Detect file association 
// strExt:  The extension to detect ( For example, : ".txt")
// strAppKey: ExeName The key value of the extension in the registry ( For example, : "txtfile")
//  return TRUE:  Represents the association, FALSE:  Means uncorrelated 
BOOL CheckFileRelation(const char *strExt, const char *strAppKey)
{
  int nRet=FALSE;
  HKEY hExtKey;
  char szPath[_MAX_PATH];
  DWORD dwSize=sizeof(szPath);
  if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS)
  {
    RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
    if(_stricmp(szPath,strAppKey)==0)
    {
      nRet=TRUE;
    }
    RegCloseKey(hExtKey);
    return nRet;
  }
  return nRet;
}
//---------------------------------------------------------------------------
//  Register file association 
// strExe:  The extension to detect ( For example, : ".txt")
// strAppName:  The name of the application to be associated with ( For example, : "C:\MyApp\MyApp.exe")
// strAppKey: ExeName The key value of the extension in the registry ( For example, : "txtfile")
// strDefaultIcon:  extension strAppName Icon file ( For example, : "C:\MyApp\MyApp.exe,0")
// strDescribe:  File type description 
void RegisterFileRelation(char *strExt, char *strAppName, char *strAppKey, char *strDefaultIcon, char *strDescribe)
{
  char strTemp[_MAX_PATH];
  HKEY hKey;
  RegCreateKey(HKEY_CLASSES_ROOT,strExt,&hKey);
  RegSetValue(hKey,"",REG_SZ,strAppKey,strlen(strAppKey)+1);
  RegCloseKey(hKey);
  RegCreateKey(HKEY_CLASSES_ROOT,strAppKey,&hKey);
  RegSetValue(hKey,"",REG_SZ,strDescribe,strlen(strDescribe)+1);
  RegCloseKey(hKey);
  sprintf(strTemp,"%s\\DefaultIcon",strAppKey);
  RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
  RegSetValue(hKey,"",REG_SZ,strDefaultIcon,strlen(strDefaultIcon)+1);
  RegCloseKey(hKey);
  sprintf(strTemp,"%s\\Shell",strAppKey);
  RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
  RegSetValue(hKey,"",REG_SZ,"Open",strlen("Open")+1);
  RegCloseKey(hKey);
  sprintf(strTemp,"%s\\Shell\\Open\\Command",strAppKey);
  RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
  sprintf(strTemp,"%s \"%%1\"",strAppName);
  RegSetValue(hKey,"",REG_SZ,strTemp,strlen(strTemp)+1);
  RegCloseKey(hKey);
}

It can be used as follows:


void CguanlianDlg::OnBnClickedOk()
{
  // TODO:  Add control notification handler code here 
  // Example code that USES these two functions to correlate 
  char strExt[10] = ".zwcTxt";
  char strAppKey[30] = "Windows.zwcTxt.1.0";
  BOOL relationExists = CheckFileRelation(strExt, strAppKey);
  if(!relationExists)
  {
    char strAppName[MAX_PATH + 1] = "E:\\Desktop\\Temp\\GMTools.exe";
    char strDefaultIcon[MAX_PATH + 1] = "";
    char strDescribe[100] = "WellTest Interpretation Files";
    RegisterFileRelation(strExt, strAppName, strAppKey, strDefaultIcon, strDescribe);
  }
}

I hope this article is helpful to you VC++ programming.


Related articles: