ExtractIconEx怎么返回NULL?

vcplusplus2005 2006-08-16 07:41:59
HICON hIcon;
CString strIconPath;
strIconPath = _T("C:\\program files\\FlashGet\\flashget.exe");
ExtractIconEx(strIconPath,128,&hIcon,NULL,1);

我用提图标软件就可以提出来。可是上面的代码却不行。

hIcon为NULL.
这是为什么呢?
...全文
190 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
claider 2007-02-07
  • 打赏
  • 举报
回复
关注
vcplusplus2005 2006-08-17
  • 打赏
  • 举报
回复
而且我用其他的抓图标程序,128是能抓出来的
vcplusplus2005 2006-08-17
  • 打赏
  • 举报
回复
不是吧,C:\PROGRA~1\FlashGet\flashget.exe,128
这是我在注册表中找到的键值,难道是假的?
0是可以的。
cybermat 2006-08-17
  • 打赏
  • 举报
回复
我用你的代码试过了,没问题啊,128确实也没超出索引范围。128正好是MFC默认的那个图标
cybermat 2006-08-17
  • 打赏
  • 举报
回复
你把128改成0试过吗?
vcplusplus2005 2006-08-17
  • 打赏
  • 举报
回复
当然不是这个问题,这个我是用其他的软件查出来的
vcplusplus2005 2006-08-17
  • 打赏
  • 举报
回复
cybermat 2006-08-16
  • 打赏
  • 举报
回复
ExtractIconEx的第二个参数是图标在EXE或DLL中的索引,你用128可能是超出索引范围了
C# 根据文件类型获取文件图标 using System; using System.Collections.Generic; using System.Collections; using System.Text; using System.Runtime.InteropServices; using Microsoft.Win32; using System.Drawing; namespace FileTypeAndIcon { /// /// Structure that encapsulates basic information of icon embedded in a file. /// public struct EmbeddedIconInfo { public string FileName; public int IconIndex; } public class RegisteredFileType { #region APIs [DllImport("shell32.dll", EntryPoint = "ExtractIconA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern IntPtr ExtractIcon(int hInst, string lpszExeFileName, int nIconIndex); [DllImport("shell32.dll", CharSet = CharSet.Auto)] private static extern uint ExtractIconEx(string szFileName, int nIconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons); [DllImport("user32.dll", EntryPoint = "DestroyIcon", SetLastError = true)] private static unsafe extern int DestroyIcon(IntPtr hIcon); #endregion #region CORE METHODS /// /// Gets registered file types and their associated icon in the system. /// /// Returns a hash table which contains the file extension as keys, the icon file and param as values. public static Hashtable GetFileTypeAndIcon() { try { // Create a registry key object to represent the HKEY_CLASSES_ROOT registry section RegistryKey rkRoot = Registry.ClassesRoot; //Gets all sub keys' names. string[] keyNames = rkRoot.GetSubKeyNames(); Hashtable iconsInfo = new Hashtable(); //Find the file icon. foreach (string keyName in keyNames) { if (String.IsNullOrEmpty(keyName)) continue; int indexOfPoint = keyName.IndexOf("."); //If this key is not a file exttension(eg, .zip), skip it. if (indexOfPoint != 0) continue; RegistryKey rkFileType = rkRoot.OpenSubKey(keyName); if (rkFileType == null) continue; //Gets the default value of this key that contains the information of file type. object defaultValue = rkFileType.GetValue(""); if (defaultValue == null) continue; //Go to the key that specifies the default icon associates with this file type. string defaultIcon = defaultValue.ToString() + "\\DefaultIcon"; RegistryKey rkFileIcon = rkRoot.OpenSubKey(defaultIcon); if (rkFileIcon != null) { //Get the file contains the icon and the index of the icon in that file. object value = rkFileIcon.GetValue(""); if (value != null) { //Clear all unecessary " sign in the string to avoid error. string fileParam = value.ToString().Replace("\"", ""); iconsInfo.Add(keyName, fileParam); } rkFileIcon.Close(); } rkFileType.Close(); } rkRoot.Close(); return iconsInfo; } catch (Exception exc) { throw exc; } } /// /// Extract the icon from file. /// /// The params string, /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1". /// This method always returns the large size of the icon (may be 32x32 px). public static Icon ExtractIconFromFile(string fileAndParam) { try { EmbeddedIconInfo embeddedIcon = getEmbeddedIconInfo(fileAndParam); //Gets the handle of the icon. IntPtr lIcon = ExtractIcon(0, embeddedIcon.FileName, embeddedIcon.IconIndex); //Gets the real icon. return Icon.FromHandle(lIcon); } catch (Exception exc) { throw exc; } } /// /// Extract the icon from file. /// /// The params string, /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1". /// /// Determines the returned icon is a large (may be 32x32 px) /// or small icon (16x16 px). public static Icon ExtractIconFromFile(string fileAndParam, bool isLarge) { unsafe { uint readIconCount = 0; IntPtr[] hDummy = new IntPtr[1] { IntPtr.Zero }; IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero }; try { EmbeddedIconInfo embeddedIcon = getEmbeddedIconInfo(fileAndParam); if (isLarge) readIconCount = ExtractIconEx(embeddedIcon.FileName, 0, hIconEx, hDummy, 1); else readIconCount = ExtractIconEx(embeddedIcon.FileName, 0, hDummy, hIconEx, 1); if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero) { // Get first icon. Icon extractedIcon = (Icon)Icon.FromHandle(hIconEx[0]).Clone(); return extractedIcon; } else // No icon read return null; } catch (Exception exc) { // Extract icon error. throw new ApplicationException("Could not extract icon", exc); } finally { // Release resources. foreach (IntPtr ptr in hIconEx) if (ptr != IntPtr.Zero) DestroyIcon(ptr); foreach (IntPtr ptr in hDummy) if (ptr != IntPtr.Zero) DestroyIcon(ptr); } } } #endregion #region UTILITY METHODS /// /// Parses the parameters string to the structure of EmbeddedIconInfo. /// /// The params string, /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1". /// protected static EmbeddedIconInfo getEmbeddedIconInfo(string fileAndParam) { EmbeddedIconInfo embeddedIcon = new EmbeddedIconInfo(); if (String.IsNullOrEmpty(fileAndParam)) return embeddedIcon; //Use to store the file contains icon. string fileName = String.Empty; //The index of the icon in the file. int iconIndex = 0; string iconIndexString = String.Empty; int commaIndex = fileAndParam.IndexOf(","); //if fileAndParam is some thing likes that: "C:\\Program Files\\NetMeeting\\conf.exe,1". if (commaIndex > 0) { fileName = fileAndParam.Substring(0, commaIndex); iconIndexString = fileAndParam.Substring(commaIndex + 1); } else fileName = fileAndParam; if (!String.IsNullOrEmpty(iconIndexString)) { //Get the index of icon. iconIndex = int.Parse(iconIndexString); if (iconIndex < 0) iconIndex = 0; //To avoid the invalid index. } embeddedIcon.FileName = fileName; embeddedIcon.IconIndex = iconIndex; return embeddedIcon; } #endregion } }

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧