我使用以下VB.net(Framework 4)代码打开文件属性 当target设置为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并且没有异常或错误消息
我搜索了两天并尝试了一些东西,但我没有找到解决方案。这有什么想法吗?
谢谢你的帮助!
答案 0 :(得分:1)
Public hInstApp As Integer
你在结构声明中有几个错误,句柄和指针是IntPtr
,而不像你声明的那样是Integer。使用32位代码,而不是64位代码,因为这些字段现在占用8个字节。使用pinvoke.net网站上提供的声明。
它还向您展示了为什么Marshal.GetLastError()没有给出正确的错误代码,您忘记在[DllImport]属性中使用SetLastError
属性。您还应该在结构和函数声明中使用CharSet:=CharSet.Auto
以从Unicode中受益。