#include "winfileexecutor.h"
#include <qt_windows.h>
#include <QMessageBox>
#include <QFileInfo>
#include <QProcess>
/*!
How to find program associated with this file type?
It's easy!
ONLY FOR WINDOWS OS!
We must access regestry.
The in hkcr find the decription of the extention of the file.
Then we must find the description key and read the open command!
*/
{
QT_WA( {
char buf[1024];
DWORD bsz = sizeof(buf);
int r = RegQueryValueExW( key, (TCHAR*)subKey.utf16(), 0, 0, (LPBYTE)buf, &bsz );
if ( r == ERROR_SUCCESS ) {
s
= QString::fromUtf16( (unsigned short *)buf
);
} else if ( r == ERROR_MORE_DATA ) {
char *ptr = new char[bsz+1];
r = RegQueryValueEx( key, (TCHAR*)subKey.utf16(), 0, 0, (LPBYTE)ptr, &bsz );
if ( r == ERROR_SUCCESS )
s = ptr;
delete [] ptr;
}
} , {
char buf[512];
DWORD bsz = sizeof(buf);
int r = RegQueryValueExA( key, subKey.toLocal8Bit(), 0, 0, (LPBYTE)buf, &bsz );
if ( r == ERROR_SUCCESS ) {
s = buf;
} else if ( r == ERROR_MORE_DATA ) {
char *ptr = new char[bsz+1];
r = RegQueryValueExA( key, subKey.toLocal8Bit(), 0, 0, (LPBYTE)ptr, &bsz );
if ( r == ERROR_SUCCESS )
s = ptr;
delete [] ptr;
}
} );
return s;
}
WinFileExecutor
::WinFileExecutor(const QString &fileName
): _fileName(fileName)
{
if (_fileName.isEmpty())
return;
//QString ext = fi.completeSuffix();
QString ext
= fi.
suffix().
prepend(".");
QString _prog
= getOpenExe
( getKeyPath
(ext
) );
if (_prog.isEmpty())
return;
}
{
HKEY k;
int r;
QT_WA( {
r = RegOpenKeyExW( HKEY_CLASSES_ROOT, reinterpret_cast<const wchar_t *>(_ext.utf16()),
0, KEY_READ, &k );
} , {
r = RegOpenKeyExA( HKEY_CLASSES_ROOT, _ext.toLocal8Bit(),
0, KEY_READ, &k );
} );
if ( r == ERROR_SUCCESS )
{
s
= getWindowsRegString
( k,
QString::null );
RegCloseKey( k );
}
else
{
RegCloseKey( k );
}
return s;
}
{
HKEY k;
int r;
QString _fullPath
= _keyPath
+ "\\shell\\Open\\Command";
QT_WA( {
r = RegOpenKeyExW( HKEY_CLASSES_ROOT, reinterpret_cast<const wchar_t *>(_fullPath.utf16()),
0, KEY_READ, &k );
} , {
r = RegOpenKeyExA( HKEY_CLASSES_ROOT, _fullPath.toLocal8Bit(),
0, KEY_READ, &k );
} );
if ( r == ERROR_SUCCESS )
{
s
= getWindowsRegString
( k,
QString::null );
RegCloseKey( k );
}
else
{
RegCloseKey( k );
}
QString _progName
= lst
[ 0 ].
remove("\"");
return _progName;
}
#include "winfileexecutor.h"
#include <qt_windows.h>
#include <QMessageBox>
#include <QFileInfo>
#include <QProcess>
/*!
How to find program associated with this file type?
It's easy!
ONLY FOR WINDOWS OS!
We must access regestry.
The in hkcr find the decription of the extention of the file.
Then we must find the description key and read the open command!
*/
static QString getWindowsRegString( HKEY key, const QString &subKey )
{
QString s;
QT_WA( {
char buf[1024];
DWORD bsz = sizeof(buf);
int r = RegQueryValueExW( key, (TCHAR*)subKey.utf16(), 0, 0, (LPBYTE)buf, &bsz );
if ( r == ERROR_SUCCESS ) {
s = QString::fromUtf16( (unsigned short *)buf );
} else if ( r == ERROR_MORE_DATA ) {
char *ptr = new char[bsz+1];
r = RegQueryValueEx( key, (TCHAR*)subKey.utf16(), 0, 0, (LPBYTE)ptr, &bsz );
if ( r == ERROR_SUCCESS )
s = ptr;
delete [] ptr;
}
} , {
char buf[512];
DWORD bsz = sizeof(buf);
int r = RegQueryValueExA( key, subKey.toLocal8Bit(), 0, 0, (LPBYTE)buf, &bsz );
if ( r == ERROR_SUCCESS ) {
s = buf;
} else if ( r == ERROR_MORE_DATA ) {
char *ptr = new char[bsz+1];
r = RegQueryValueExA( key, subKey.toLocal8Bit(), 0, 0, (LPBYTE)ptr, &bsz );
if ( r == ERROR_SUCCESS )
s = ptr;
delete [] ptr;
}
} );
return s;
}
WinFileExecutor::WinFileExecutor(const QString &fileName)
: _fileName(fileName)
{
if (_fileName.isEmpty())
return;
QFileInfo fi(_fileName);
//QString ext = fi.completeSuffix();
QString ext = fi.suffix().prepend(".");
QMessageBox::warning(0, ext, _fileName );
QString _prog = getOpenExe( getKeyPath(ext) );
if (_prog.isEmpty())
return;
QProcess::startDetached( _prog, QStringList() << fileName );
}
QString WinFileExecutor::getKeyPath(const QString _ext) const
{
HKEY k;
int r;
QString s;
QT_WA( {
r = RegOpenKeyExW( HKEY_CLASSES_ROOT, reinterpret_cast<const wchar_t *>(_ext.utf16()),
0, KEY_READ, &k );
} , {
r = RegOpenKeyExA( HKEY_CLASSES_ROOT, _ext.toLocal8Bit(),
0, KEY_READ, &k );
} );
if ( r == ERROR_SUCCESS )
{
s = getWindowsRegString( k, QString::null );
RegCloseKey( k );
}
else
{
RegCloseKey( k );
}
QMessageBox::warning(0, s, s );
return s;
}
QString WinFileExecutor::getOpenExe(const QString _keyPath) const
{
HKEY k;
int r;
QString s;
QString _fullPath = _keyPath + "\\shell\\Open\\Command";
QT_WA( {
r = RegOpenKeyExW( HKEY_CLASSES_ROOT, reinterpret_cast<const wchar_t *>(_fullPath.utf16()),
0, KEY_READ, &k );
} , {
r = RegOpenKeyExA( HKEY_CLASSES_ROOT, _fullPath.toLocal8Bit(),
0, KEY_READ, &k );
} );
if ( r == ERROR_SUCCESS )
{
s = getWindowsRegString( k, QString::null );
RegCloseKey( k );
}
else
{
RegCloseKey( k );
}
QStringList lst = s.split(" \"");
QString _progName = lst[ 0 ].remove("\"");
QMessageBox::warning(0, s, _progName );
return _progName;
}
To copy to clipboard, switch view to plain text mode
Bookmarks