// DestroyIcon() the return value HICON LoadRegClassSmallIcon(const char *pszClassName) { HICON hIcon=NULL; HKEY hClassKey,hIconKey; TCHAR *pszIconLoc,*p; /* using the merged view classes key for reading */ /* class */ if (!RegOpenKeyExA(HKEY_CLASSES_ROOT,pszClassName,0,KEY_QUERY_VALUE,&hClassKey)) { /* default icon */ if (!RegOpenKeyEx(hClassKey,_T("DefaultIcon"),0,KEY_QUERY_VALUE,&hIconKey)) { /* extract icon */ pszIconLoc=GetRegStrValue(hIconKey,NULL); if (pszIconLoc!=NULL) { p=_tcsrchr(pszIconLoc,_T(',')); if (p!=NULL) { *(p++)=0; ExtractIconEx(pszIconLoc,_ttoi(p),NULL,&hIcon,1); } mir_free(pszIconLoc); } RegCloseKey(hIconKey); } RegCloseKey(hClassKey); } return hIcon; }
static BOOL Opera6_GetIniFilePath(TCHAR *szIniFile) { HKEY hExeKey; TCHAR szPath[MAX_PATH],*p; BOOL fSuccess=FALSE; DWORD len; /* Info: http://opera-info.de/forum/thread.php?threadid=2905 */ /* app path */ if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Netscape.exe",0,KEY_QUERY_VALUE,&hExeKey)) { /* exe name */ p=GetRegStrValue(hExeKey,NULL); if (p!=NULL && _tcsstr(p,_T("Opera.exe"))!=NULL) { /* path */ mir_free(p); p=GetRegStrValue(hExeKey,_T("Path")); len=mir_tstrlen(p); if (p[len-1]==_T('\\')) p[len-1]=0; fSuccess=(p!=NULL && ExpandEnvironmentStrings(p,szPath,MAX_PATH)); } mir_free(p); /* does NULL check */ RegCloseKey(hExeKey); } if (fSuccess) { TCHAR szFileBuf[MAX_PATH+34]; /* operadef6.ini */ mir_tstrcat(mir_tstrcpy(szFileBuf,szPath),_T("\\operadef6.ini")); /* buffer safe */ /* If enabled Opera will use Windows profiles to store individual user settings */ if (GetPrivateProfileInt(_T("System"),_T("Multi User"),0,szFileBuf)==1) { p=_tcsrchr(szPath,'\\'); mir_tstrcpy(szFileBuf,_T("%APPDATA%\\Opera")); /* buffer safe */ if (p!=NULL) mir_tstrcat(szFileBuf,p); /* buffer safe */ } else mir_tstrcpy(szFileBuf,szPath); /* opera6.ini */ mir_tstrcat(szFileBuf,_T("\\profile\\opera6.ini")); /* buffer safe */ fSuccess=ExpandEnvironmentStrings(szFileBuf,szIniFile,MAX_PATH)!=0; } /* check file existstance */ if (fSuccess) { HANDLE hFile; hFile=CreateFile(szIniFile,0,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL); if (hFile==INVALID_HANDLE_VALUE) fSuccess=FALSE; else CloseHandle(hFile); } return fSuccess; }
// hKey must have been opened with KEY_QUERY_VALUE access right static BOOL IsRegStrValue(HKEY hKey,const TCHAR *pszValName,const TCHAR *pszCmpVal) { BOOL fSame=FALSE; TCHAR *pszVal=GetRegStrValue(hKey,pszValName); if (pszVal!=NULL) { fSame=!mir_tstrcmp(pszVal,pszCmpVal); mir_free(pszVal); } return fSame; }
// hKey must have been opened with KEY_QUERY_VALUE access right static BOOL IsRegStrValueA(HKEY hKey,const TCHAR *pszValName,const char *pszCmpVal) { BOOL fSame=FALSE; char *pszValA; TCHAR *pszVal=GetRegStrValue(hKey,pszValName); if (pszVal!=NULL) { pszValA=t2a(pszVal); if (pszValA!=NULL) fSame=!mir_strcmp(pszValA,pszCmpVal); mir_free(pszValA); /* does NULL check */ mir_free(pszVal); } return fSame; }
BOOL RemoveRegClass(const char *pszClassName) { LONG res; HKEY hRootKey,hClassKey,hShellKey,hVerbKey; TCHAR *ptszClassName,*ptszPrevRunCmd; /* try to open interactive user's classes key */ if (RegOpenKeyEx(HKEY_CURRENT_USER,_T("Software\\Classes"),0,DELETE,&hRootKey)) hRootKey=HKEY_CLASSES_ROOT; /* class name */ ptszClassName=a2t(pszClassName); if (ptszClassName!=NULL) res=DeleteRegSubTree(hRootKey,ptszClassName); else res=ERROR_OUTOFMEMORY; mir_free(ptszClassName); /* does NULL check */ /* backup only saved/restored for fUrlProto */ if (!res) { if ((res=RestoreRegTree(hRootKey,pszClassName,"bak_"))==ERROR_SUCCESS) /* class */ if (!RegOpenKeyExA(hRootKey,pszClassName,0,KEY_QUERY_VALUE,&hClassKey)) { /* shell */ if (!RegOpenKeyEx(hClassKey,_T("shell"),0,KEY_QUERY_VALUE,&hShellKey)) { /* verb */ if (!RegOpenKeyEx(hShellKey,_T("open"),0,KEY_QUERY_VALUE,&hVerbKey)) { /* command */ ptszPrevRunCmd=GetRegStrValue(hVerbKey,_T("command")); if (ptszPrevRunCmd!=NULL && !IsValidRunCommand(ptszPrevRunCmd)) res=DeleteRegSubTree(hRootKey,ptszClassName); /* backup outdated, remove all */ mir_free(ptszPrevRunCmd); /* does NULL check */ RegCloseKey(hVerbKey); } RegCloseKey(hShellKey); } RegCloseKey(hClassKey); } } else DeleteRegTreeBackup(pszClassName,"bak_"); if (hRootKey!=HKEY_CLASSES_ROOT) RegCloseKey(hRootKey); if (res==ERROR_SUCCESS || res==ERROR_FILE_NOT_FOUND || res==ERROR_NO_MORE_ITEMS) return TRUE; SetLastError(res); return FALSE; }
void RemoveRegFileExt(const char *pszFileExt,const char *pszClassName) { HKEY hRootKey,hExtKey,hSubKey; DWORD nOpenWithCount; TCHAR *pszPrevClassName=NULL; BOOL fRestored=FALSE; /* try to open interactive user's classes key */ if (RegOpenKeyEx(HKEY_CURRENT_USER,_T("Software\\Classes"),0,DELETE,&hRootKey)) hRootKey=HKEY_CLASSES_ROOT; /* file ext */ if (!RegOpenKeyExA(hRootKey,pszFileExt,0,KEY_QUERY_VALUE|KEY_SET_VALUE|DELETE,&hExtKey)) { /* class name (the important part) */ if (!RestoreRegTree(hRootKey,pszFileExt,"bak_")) { pszPrevClassName=GetRegStrValue(hExtKey,NULL); if (pszPrevClassName!=NULL) { /* previous class name still exists? */ /* using the merged view classes key for reading */ if (!RegOpenKeyEx(HKEY_CLASSES_ROOT,pszPrevClassName,0,KEY_QUERY_VALUE,&hSubKey)) { fRestored=TRUE; RegCloseKey(hSubKey); } else RegDeleteValue(hExtKey,NULL); mir_free(pszPrevClassName); } } if (pszPrevClassName==NULL) RegDeleteValue(hExtKey,NULL); /* open with progids (remove if empty) */ nOpenWithCount=0; if (!RegOpenKeyEx(hExtKey,_T("OpenWithProgids"),0,KEY_SET_VALUE|KEY_QUERY_VALUE,&hSubKey)) { /* remove current class (if set by another app) */ RegDeleteValueA(hSubKey,pszClassName); RegQueryInfoKey(hSubKey,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&nOpenWithCount,NULL,NULL,NULL); RegCloseKey(hSubKey); } if (!nOpenWithCount) RegDeleteKey(hExtKey,_T("OpenWithProgids")); /* delete if no values */ RegCloseKey(hExtKey); } else DeleteRegTreeBackup(pszFileExt,"bak_"); if (!fRestored) RegDeleteKeyA(hRootKey,pszFileExt); /* try to remove it all */ if (hRootKey!=HKEY_CLASSES_ROOT) RegCloseKey(hRootKey); }
// pszMimeType is allowed to be NULL BOOL AddRegFileExt(const char *pszFileExt,const char *pszClassName,const char *pszMimeType,BOOL fIsText) { BOOL fSuccess=FALSE; HKEY hRootKey,hExtKey,hOpenWithKey; /* some error checking for disallowed values (to avoid errors in registry) */ if (strchr(pszFileExt,'\\')!=NULL || strchr(pszFileExt,' ')!=NULL) return FALSE; /* try to open interactive user's classes key */ if (RegOpenKeyEx(HKEY_CURRENT_USER,_T("Software\\Classes"),0,KEY_CREATE_SUB_KEY,&hRootKey)) hRootKey=HKEY_CLASSES_ROOT; /* file ext */ if (!RegCreateKeyExA(hRootKey,pszFileExt,0,NULL,0,KEY_SET_VALUE|KEY_QUERY_VALUE|KEY_CREATE_SUB_KEY,NULL,&hExtKey,NULL)) { /* backup previous app */ BackupRegTree(hRootKey,pszFileExt,"bak_"); /* remove any no-open flag */ RegDeleteValue(hExtKey,_T("NoOpen")); /* open with progids */ TCHAR *pszPrevClass=GetRegStrValue(hExtKey,NULL); if (pszPrevClass!=NULL && !IsRegStrValueA(hExtKey,NULL,pszClassName)) if (!RegCreateKeyEx(hExtKey,_T("OpenWithProgids"),0,NULL,0,KEY_SET_VALUE,NULL,&hOpenWithKey,NULL)) { /* previous class (backup) */ RegSetValueEx(hOpenWithKey,pszPrevClass,0,REG_NONE,NULL,0); RegCloseKey(hOpenWithKey); } mir_free(pszPrevClass); /* does NULL check */ /* class name */ fSuccess=!RegSetValueExA(hExtKey, NULL, 0, REG_SZ, (BYTE*)pszClassName, (int)mir_strlen(pszClassName)+1); /* mime type e.g. "application/x-icq" */ if (pszMimeType!=NULL) RegSetValueExA(hExtKey, "Content Type", 0, REG_SZ, (BYTE*)pszMimeType, (int)mir_strlen(pszMimeType)+1); /* perceived type e.g. text (WinXP+) */ if (fIsText) RegSetValueEx(hExtKey,_T("PerceivedType"),0,REG_SZ,(BYTE*)_T("text"),5*sizeof(TCHAR)); RegCloseKey(hExtKey); } if (hRootKey!=HKEY_CLASSES_ROOT) RegCloseKey(hRootKey); return fSuccess; }