System.ArgumentException:传递给Icon的Win32句柄无效或类型错误

时间:2014-10-20 18:22:57

标签: vb.net system.drawing shell32.dll

我有以下代码使用Shell32 dll加载图标。它在我的机器上工作正常。但生产环境中的一个系统有一个例外,说“传递给Icon的System.ArgumentException: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

2 个答案:

答案 0 :(得分:2)

试试这个:

<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

答案 1 :(得分: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