在Qt应用程序中使用WinAPI提取图标

在Qt应用程序中使用WinAPI提取图标,qt,winapi,icons,exe,Qt,Winapi,Icons,Exe,我正在尝试使用WinAPI从exe文件中提取图标,但它不起作用 代码如下: QIcon OSTools::AppsInterface::extractAppIcon(const QString &fileName) const { wchar_t *convertedName = new wchar_t[fileName.length() + 1]; fileName.toWCharArray(convertedName); convertedName[fileN

我正在尝试使用WinAPI从exe文件中提取图标,但它不起作用

代码如下:

QIcon OSTools::AppsInterface::extractAppIcon(const QString &fileName) const {
    wchar_t *convertedName = new wchar_t[fileName.length() + 1];
    fileName.toWCharArray(convertedName);
    convertedName[fileName.length()] = '\0';
    HICON Icon = ExtractIcon(NULL, convertedName, 0);

    QPixmap pixmap = QPixmap::fromWinHICON(Icon);
    return QIcon(pixmap);
}
代码输出:

QPixmap::fromWinHICON(), failed to GetIconInfo()
()

我认为问题在于我发送NULL而不是“调用函数的应用程序实例的句柄”。但是,一般来说,我使用Qt,在我的应用程序中它只是一个WinAPI函数

怎么了?使用WinAPI提取图标的正确方法是什么?如果您有其他功能建议,请给我一个例子。这是我第一次使用WinAPI


更新:是的,有更好的方法。您可以使用类来做这些事情。

对我有效,即使使用NULL。但获得HINSTATION实际上非常简单。我想你在别的地方有问题。你的目标exe真的有嵌入的图标吗

#ifdef Q_WS_WIN
#include <qt_windows.h>
#endif

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);
#ifdef Q_WS_WIN
  QString fileName("D:\\_dev\\eclipse\\eclipse.exe");
  wchar_t *convertedName = new wchar_t[fileName.length() + 1];
  fileName.toWCharArray(convertedName);
  convertedName[fileName.length()] = '\0';
  HINSTANCE hInstance = ::GetModuleHandle(NULL);
  HICON Icon = ::ExtractIcon(hInstance, convertedName, 0);
  ui->label->setPixmap(QPixmap::fromWinHICON(Icon));
#endif
}
#ifdef Q#u WS#u WIN
#包括
#恩迪夫
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
#如果你赢了
QString文件名(“D:\\\\ u dev\\eclipse\\eclipse.exe”);
wchar_t*convertedName=new wchar_t[fileName.length()+1];
fileName.toWCharArray(convertedName);
convertedName[fileName.length()]='\0';
HINSTANCE HINSTANCE=::GetModuleHandle(NULL);
HICON图标=::ExtractIcon(hInstance,convertedName,0);
ui->label->setPixmap(QPixmap::fromWinHICON(图标));
#恩迪夫
}

解决方案非常简单。我只是发送了“.lnk”文件的路径,而不是文件的路径。那是我的疏忽。

我用过,效果很好。试试这个:

QPushButton b;
b.show();

QIcon icon;
QFileIconProvider fileiconpr;
icon = fileIconProvider.icon(QFileInfo("/*file name*/"));

b.setIcon(icon);

// And you can also save it where you want : 
QPixmap pixmap = icon.pixmap( QSize(/*desired size*/)  );
pixmap.save("/Desktop/notepad-icon.png");

。祝你有愉快的一天。

我想这
HICON
不是空的吧?尝试直接调用
GetIconInfo
查看错误结果。@Neil GetLastError()在调用GetIconInfo之前返回错误1402“无效的光标句柄”,在-0之后。啊,所以你的
图标一直是
NULL
。。。正如我记得的,它指向了随机记忆点。