Hi! This is my old code but it works and maybe you'll find it useful:

this class works only on Windows. It tries to launch the specified filename in this way:
- takes the filename extention and look the associated program in the registry
- launches this app with our file.

This code is not final and is dirty cause it was written in the very fast way and needs to be modified. So take it as example!

=== winfileexecutor.h ===

Qt Code:
  1. #ifndef WINFILEEXECUTOR_H_
  2. #define WINFILEEXECUTOR_H_
  3.  
  4. #include <QString>
  5.  
  6. /*!
  7. \class WinFileExecutor
  8.  
  9. \brief The WinFileExecutor class provides support to launch book in associated program.
  10. */
  11.  
  12. class WinFileExecutor
  13. {
  14. public:
  15. WinFileExecutor(const QString &fileName);
  16. virtual ~WinFileExecutor() {}
  17.  
  18. QString getKeyPath(const QString _ext) const;
  19. QString getOpenExe(const QString _keyPath) const;
  20.  
  21. private:
  22. QString _fileName;
  23.  
  24. };
  25.  
  26. #endif
To copy to clipboard, switch view to plain text mode 

=== winfileexecutor.cpp ===

Qt Code:
  1. #include "winfileexecutor.h"
  2.  
  3. #include <qt_windows.h>
  4. #include <QMessageBox>
  5. #include <QFileInfo>
  6. #include <QProcess>
  7.  
  8. /*!
  9. How to find program associated with this file type?
  10. It's easy!
  11.  
  12. ONLY FOR WINDOWS OS!
  13. We must access regestry.
  14. The in hkcr find the decription of the extention of the file.
  15. Then we must find the description key and read the open command!
  16.  
  17. */
  18.  
  19. static QString getWindowsRegString( HKEY key, const QString &subKey )
  20. {
  21. QT_WA( {
  22. char buf[1024];
  23. DWORD bsz = sizeof(buf);
  24. int r = RegQueryValueExW( key, (TCHAR*)subKey.utf16(), 0, 0, (LPBYTE)buf, &bsz );
  25. if ( r == ERROR_SUCCESS ) {
  26. s = QString::fromUtf16( (unsigned short *)buf );
  27. } else if ( r == ERROR_MORE_DATA ) {
  28. char *ptr = new char[bsz+1];
  29. r = RegQueryValueEx( key, (TCHAR*)subKey.utf16(), 0, 0, (LPBYTE)ptr, &bsz );
  30. if ( r == ERROR_SUCCESS )
  31. s = ptr;
  32. delete [] ptr;
  33. }
  34. } , {
  35. char buf[512];
  36. DWORD bsz = sizeof(buf);
  37. int r = RegQueryValueExA( key, subKey.toLocal8Bit(), 0, 0, (LPBYTE)buf, &bsz );
  38. if ( r == ERROR_SUCCESS ) {
  39. s = buf;
  40. } else if ( r == ERROR_MORE_DATA ) {
  41. char *ptr = new char[bsz+1];
  42. r = RegQueryValueExA( key, subKey.toLocal8Bit(), 0, 0, (LPBYTE)ptr, &bsz );
  43. if ( r == ERROR_SUCCESS )
  44. s = ptr;
  45. delete [] ptr;
  46. }
  47. } );
  48. return s;
  49. }
  50.  
  51. WinFileExecutor::WinFileExecutor(const QString &fileName)
  52. : _fileName(fileName)
  53. {
  54. if (_fileName.isEmpty())
  55. return;
  56.  
  57. QFileInfo fi(_fileName);
  58. //QString ext = fi.completeSuffix();
  59. QString ext = fi.suffix().prepend(".");
  60.  
  61. QMessageBox::warning(0, ext, _fileName );
  62.  
  63. QString _prog = getOpenExe( getKeyPath(ext) );
  64.  
  65. if (_prog.isEmpty())
  66. return;
  67.  
  68. QProcess::startDetached( _prog, QStringList() << fileName );
  69. }
  70.  
  71. QString WinFileExecutor::getKeyPath(const QString _ext) const
  72. {
  73. HKEY k;
  74. int r;
  75.  
  76. QT_WA( {
  77. r = RegOpenKeyExW( HKEY_CLASSES_ROOT, reinterpret_cast<const wchar_t *>(_ext.utf16()),
  78. 0, KEY_READ, &k );
  79. } , {
  80. r = RegOpenKeyExA( HKEY_CLASSES_ROOT, _ext.toLocal8Bit(),
  81. 0, KEY_READ, &k );
  82. } );
  83.  
  84. if ( r == ERROR_SUCCESS )
  85. {
  86. s = getWindowsRegString( k, QString::null );
  87. RegCloseKey( k );
  88. }
  89. else
  90. {
  91. RegCloseKey( k );
  92. }
  93.  
  94. QMessageBox::warning(0, s, s );
  95.  
  96. return s;
  97. }
  98.  
  99. QString WinFileExecutor::getOpenExe(const QString _keyPath) const
  100. {
  101. HKEY k;
  102. int r;
  103.  
  104. QString _fullPath = _keyPath + "\\shell\\Open\\Command";
  105.  
  106. QT_WA( {
  107. r = RegOpenKeyExW( HKEY_CLASSES_ROOT, reinterpret_cast<const wchar_t *>(_fullPath.utf16()),
  108. 0, KEY_READ, &k );
  109. } , {
  110. r = RegOpenKeyExA( HKEY_CLASSES_ROOT, _fullPath.toLocal8Bit(),
  111. 0, KEY_READ, &k );
  112. } );
  113.  
  114. if ( r == ERROR_SUCCESS )
  115. {
  116. s = getWindowsRegString( k, QString::null );
  117. RegCloseKey( k );
  118. }
  119. else
  120. {
  121. RegCloseKey( k );
  122. }
  123.  
  124. QStringList lst = s.split(" \"");
  125. QString _progName = lst[ 0 ].remove("\"");
  126.  
  127.  
  128. QMessageBox::warning(0, s, _progName );
  129.  
  130. return _progName;
  131. }
To copy to clipboard, switch view to plain text mode