////////////////////////////////////////////////////////////////////////////// // MFC Tip #8 -- Vision X Software -- http://www.visionx.com/mfcpro // Finding a File Association in the Registry // // This source code is provided for instructional purposes, and is released // to the public domain. No claim of ownership is made. Use it at your own // risk. No warranties are expressed or implied. The resulting program may // not be fully functional. No animals were used to test this code. ////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // GetRegistryExe // Returns the full executable file & path name that will open a document // with a given extension. void GetRegistryExe (LPCSTR szInput, LPSTR szOut, UINT uiOutSize) { szOut[0] = 0; // Copy input extension to our extension. // Make sure it begins with '.' char szExt[16]; szExt[0] = 0; if (szInput[0] != '.') lstrcpy(szExt, "."); lstrcat(szExt, szInput); // Get title of program assoc with this extension. char szName[256]; GetRegistryEntry(szExt, "", szName, sizeof(szName)); if (szName[0] == 0) return; // Get corresponding exe filename. lstrcat(szName, "\\shell\\open\\command"); GetRegistryEntry(szName, "", szOut, uiOutSize); if (szOut[0] == 0) return; // Remove everything after the first "%" char // in case there's a %1, %2, or other param. char *pChar = strchr(szOut, '%'); if (pChar) { for (--pChar ; pChar > szOut && *pChar != ' ' ; --pChar) ; *pChar = 0; } } ///////////////////////////////////////////////////////////////////////////// // GetRegistryEntry // Fetches an HKEY_CLASSES_ROOT registry entry for a given path and value. void GetRegistryEntry (LPCSTR szInPath, LPCSTR szInValue, LPSTR szOut, UINT uiOutSize) { szOut[0] = 0; HKEY hKey; if (RegOpenKey(HKEY_CLASSES_ROOT, szInPath, &hKey) == ERROR_SUCCESS) { LONG lSize = (LONG) uiOutSize; RegQueryValue(hKey, szInValue, szOut, &lSize); RegCloseKey(hKey); } }