title: Get file type from windows registry in c++
body: <p>I am trying to display file type of given filename (based on extension) using <code>AssocQueryKey()</code> API function.
  The problem is thar return wrong <code>HKEY</code> value sometimes. For example the following function works correctly on win 7 ultimate x64, but fails for some extensions like ".mp3" on my win xp x86 machine (other extensions works though).
  Even when "succeeded" and returns S_OK, <code>GetLastError()</code> is 1008 ALWAYS after <code>AssocQueryKey()</code> call:</p>
  
  <pre><code>// Return STL string representation of file type from windows registry
  stlstring GetFileTypeFromRegistry(const stlstring&amp; m_filename)
  {
   CRegKey reg;
   HKEY key = {0};
   stlstring s;
   //Get file extension
   LPCTSTR fExt = PathFindExtension(m_filename.c_str());
   if(AssocQueryKey(NULL, ASSOCKEY_CLASS, fExt, TEXT(""),  &amp;key) != S_OK)
    DisplayError(_T("AssocQueryKey != S_OK"), GetLastError());
   else
    DisplayError(_T("AssocQueryKey == S_OK"), GetLastError());
  
   if(reg.Open ( key, NULL, KEY_QUERY_VALUE) != ERROR_SUCCESS){
    reg.Close();
    DisplayError((LPTSTR)fExt);
    return s;
   }
   //DWORD out = 0;
   /*WCHAR *h = new WCHAR[1024];
   ZeroMemory(h, sizeof(h));
   AssocQueryStringByKey(0, ASSOCSTR_EXECUTABLE, HKEY_CLASSES_ROOT, NULL, h, &amp;out);
   //MessageBox(0,_T("gbtbb"),h,MB_OK);
   delete[] h;*/
   ULONG m_sz = 256;
   //if( reg.QueryStringValue(NULL, NULL, &amp;m_sz) == ERROR_SUCCESS){
  
    TCHAR *m_regstring = new TCHAR[m_sz + 1];
     if(reg.QueryStringValue(NULL, m_regstring, &amp;m_sz) == ERROR_SUCCESS){
      //DisplayError(_T(""));
      s += m_regstring;
     /*delete[] m_regstring; m_regstring = NULL;
     reg.Close();
      return s;*/
     } else {
      DisplayError(_T("CRegKey::QueryStringValue()"), GetLastError());
     }
     s += m_regstring;
     delete[] m_regstring; m_regstring = NULL;
     reg.Close();
     return s;
   /*}
   reg.Close();
   return s;*/
  }
  </code></pre>
  
  <p>Any ideas on this ?? This function is from a DLL which is loaded by windows explorer, implementing <code>IQueryInfo::GetInfoTip()</code> if that matters.</p>