vb.net win32api指向整数数组参数的指针

时间:2013-08-24 17:56:39

标签: vb.net winapi pointers icons

因为我需要从文件中提取图标,而不是第一个图标,所以我无法使用vb.net图标提取功能。应该执行此操作的WIN32API函数需要指向整数数组的指针。

如何将此类型作为参数提供?

Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" _
(ByVal lpszFile As String, _
ByVal nIconIndex As Integer, _
ByRef phiconLarge As Integer, _
ByRef phiconSmall As Integer, _
ByVal nIcons As Long) As Integer


    Dim icons As integer()
    ExtractIconEx("%systemroot%/shell32.dll", 15, icons, 0, 5)

我已经对System.Reflection.Pointer类?/ namespace?采取了措辞,但文档稀疏且不太合理。

IntPtr不支持数组afaikt

好的,我已经设法将签名更正为:

<Runtime.InteropServices.DllImport("shell32.dll", _
CharSet:=Runtime.InteropServicesCharSet.Auto)> _
Shared Function ExtractIconEx(ByVal szFileName As String, _
        ByVal nIconIndex As Integer, _
        ByRef phiconLarge() As IntPtr, _
        ByRef phiconSmall() As IntPtr, _
        ByVal nIcons As UInteger) As UInteger
End Function 

...

 Dim icons(8) As IntPtr, smicons(8) As IntPtr
    MsgBox(ExtractIconEx("%systemroot%/shell32.dll", 15, icons, smicons, 1))
    Try
        MsgBox(icons.Count)
    Catch ex As Exception
        MsgBox(ex.Message & " by " & ex.Source)

    End Try
...

后续调用总是会导致异常(Value cannot be null)。我得到一个返回值4294967295,这是最大32位整数值。

有关如何驯服此功能并使其有效的任何想法吗?

1 个答案:

答案 0 :(得分:2)

    ByRef phiconLarge() As IntPtr, _
    ByRef phiconSmall() As IntPtr, _

该网页上的VB.NET声明有一个错误,这些数组需要传递ByVal,而不是ByRef。请注意他们如何在页面底部的示例代码中更正确。

我编辑了页面以纠正错误。