July 30, 2009

How To: Get the Installation Directory of Windows Media Player

Code Snippet

bool GetWMPInstallDirectory(wstring& wstrDirectory)
{
  bool bOK = false;
  HKEY hkResult = NULL;

  LONG lRet = ::RegOpenKeyExW(HKEY_LOCAL_MACHINE,
    L"SOFTWARE\\Microsoft\\MediaPlayer", NULL, NULL, &hkResult);

  if(lRet == ERROR_SUCCESS)
  {
    wchar_t wszRet[_MAX_PATH] = {0};
    DWORD type = REG_SZ;
    DWORD cbData = sizeof(wszRet);

    lRet = ::RegQueryValueExW(hkResult, L"Installation Directory",
      NULL, &type, (LPBYTE)wszRet, &cbData);

    if(lRet == ERROR_SUCCESS)
    {
      // We've got the installation directory of WMP
      wstrDirectory = wszRet;
      bOK = true;
    }

    ::RegCloseKey(hkResult);
    hkResult = NULL;
  }

  return bOK;
}