2

我尝试提取文件的图标并将其返回给外壳扩展的 GetIconLocation。通常,我更改扩展名为 xx 的文件(te.docx.xx)的图标,并返回不带 xx 的文件图标。(为此,我在 temp 目录中创建了带有原始扩展名的临时文件,例如 te.docx)我的操作系统是 windows 7 x64。我的代码是:

    STDMETHODIMP CTxtIconShlExt::GetIconLocation (
    UINT uFlags,  LPTSTR szIconFile, UINT cchMax,
    int* piIndex, UINT* pwFlags )
{
DWORD     dwFileSizeLo, dwFileSizeHi;
DWORDLONG qwSize;
HANDLE    hFile;
OutputDebugStringW(L"Hello world, from GetIconLocation !");
    std::string strFilePath;
    std::string tempFolder="c:\\.tmp";
    std::string tempFile="tmpfile";
    std::string fileWithOutDN;
    SHFILEINFO retShFileInfo;
    for(int i = 0; m_szFilename[i] != 0; i++)
    {
        strFilePath += m_szFilename[i];
        }

    fileWithOutDN= strFilePath.substr(0,strFilePath.size()-3 );
    std::string extension = fileWithOutDN.substr(fileWithOutDN.find_last_of("."));
    CreateDirectory(tempFolder.c_str(),NULL);

    tempFile=tempFolder+"\\"+tempFile+extension;

    GetFileAttributes(tempFile.c_str()); // from winbase.h
    if(INVALID_FILE_ATTRIBUTES == GetFileAttributes(tempFile.c_str()) && GetLastError()==ERROR_FILE_NOT_FOUND)
    {
        //File not found
        HANDLE h = CreateFile(tempFile.c_str(),    // name of the file
                          GENERIC_WRITE, // open for writing
                          0,             // sharing mode, none in this case
                          0,             // use default security descriptor
                          CREATE_ALWAYS, // overwrite if exists
                          FILE_ATTRIBUTE_NORMAL,
                          0);
        if (h)
        {
             CloseHandle(h);
        }else
        {
            return S_FALSE; //faild to create file
        }
    }

    ZeroMemory(&retShFileInfo, sizeof(SHFILEINFO));
    CoInitialize(NULL);
    SHGetFileInfo(tempFile.c_str(),256,&retShFileInfo,sizeof(SHFILEINFO),SHGFI_ICON | SHGFI_LARGEICON);
    lstrcpyn ( szIconFile, retShFileInfo.szDisplayName, cchMax );
    *piIndex = retShFileInfo.iIcon;
    *pwFlags = 0;
    return S_OK;

我的问题是 retShFileInfo.szDisplayName 返回一个空数组(所有项目都为零),它应该返回图标位置的完整路径。我尝试使用标志的组合,但没有任何帮助

4

0 回答 0