0

我有下面的代码来使用 Shell32 dll 加载图标。它在我的机器上运行良好。但是生产环境中的一个系统出现异常,提示“System.ArgumentException:传递给 Icon 的 Win32 句柄无效或类型错误”。知道为什么我们会收到此错误吗?谢谢!

Public Function GetExecutableIcon() As Icon
    Dim large As IntPtr
    Dim small As IntPtr
    ExtractIconEx(Application.ExecutablePath, 0, large, small, 1)

    Return Icon.FromHandle(small)

End Function

<DllImport("Shell32")> _
Public Shared Function ExtractIconEx(ByVal sFile As String, ByVal iIndex As Integer, 
                 ByRef piLargeVersion As IntPtr, ByRef piSmallVersion As IntPtr, 
                 ByVal amountIcons As Integer) As Integer

End Function
4

2 回答 2

1

你的声明正确吗? http://www.pinvoke.net/default.aspx/shell32.ExtractIconEx显示

<DllImport("shell32.dll", CharSet:=CharSet.Auto)> _
 Shared Function ExtractIconEx(ByVal szFileName As String, _
             ByVal nIconIndex As Integer, _
             ByVal phiconLarge() As IntPtr, _
             ByVal phiconSmall() As IntPtr, _
             ByVal nIcons As UInteger) As UInteger
 End Function
于 2014-10-20T18:30:26.010 回答
1

尝试这个:

<DllImport("Shell32")> _
Public Shared Function ExtractIconEx(ByVal sFile As String, ByVal iIndex As Integer, 
             ByRef piLargeVersion As IntPtr, ByRef piSmallVersion As IntPtr, 
             ByVal amountIcons As Integer) As Integer

Public Function GetExecutableIcon() As Icon
    Dim num As Integer = 10
    Dim large(num - 1) As IntPtr
    Dim small(num - 1) As IntPtr

    ExtractIconEx("C:\Windows\System32\Shell32.dll", 0, large(0), small(0), num)

    Return Icon.FromHandle(small(6)) 'change the index accordingly

End Function
于 2014-10-20T19:00:58.207 回答