0

我使用以下 VB.net (Framework 4) 代码打开文件属性。
当目标设置为 x86 时,这可以正常工作。

<StructLayout(LayoutKind.Sequential)> _
Public Structure SHELLEXECUTEINFO
    Public cbSize As Integer
    Public fMask As UInteger
    Public hwnd As IntPtr
    Public lpVerb As [String]
    Public lpFile As [String]
    Public lpParameters As [String]
    Public lpDirectory As [String]
    Public nShow As Integer
    Public hInstApp As Integer
    Public lpIDList As Integer
    Public lpClass As [String]
    Public hkeyClass As Integer
    Public dwHotKey As UInteger
    Public hIcon As Integer
    Public hProcess As Integer
End Structure

Private Const SW_SHOW As Integer = 5
Private Const SEE_MASK_INVOKEIDLIST As UInteger = 12 ' 0x0000000C

<DllImport("shell32.dll")> _
Private Shared Function ShellExecuteEx(ByRef lpExecInfo As SHELLEXECUTEINFO) As Boolean
End Function

Public Shared Sub ShowProperties(ByVal path As String)
    Dim fi As New IO.FileInfo(path)
    Dim info As New SHELLEXECUTEINFO()
    info.cbSize = Marshal.SizeOf(info)
    info.lpVerb = "properties"
    info.lpFile = fi.Name
    info.lpDirectory = fi.DirectoryName
    info.nShow = SW_SHOW
    info.fMask = SEE_MASK_INVOKEIDLIST
    ShellExecuteEx(info)       
End Sub

现在我的项目需要 x64 目标,上面的代码不再起作用。:(
GetLastError返回 0 并且没有异常或错误消息。
我搜索了两天并尝试了一些东西,但我没有找到解决方案。这里有什么想法吗?

感谢帮助!

4

1 回答 1

1
  Public hInstApp As Integer

你在结构声明中有几个错误,句柄和指针是IntPtr,而不是像你声明的那样 Integer。在 32 位代码中工作,而不是在 64 位代码中工作,因为这些字段现在占用 8 个字节。使用pinvoke.net网站上提供的声明。

它还向您展示了为什么 Marshal.GetLastError() 没有给您正确的错误代码,您忘记使用SetLastError[DllImport] 属性中的属性。您还应该CharSet:=CharSet.Auto在结构和函数声明中使用以从 Unicode 中受益。

于 2014-01-10T13:47:08.963 回答