C ++ SHFileOperation 64位操作系统

时间:2016-11-07 10:41:55

标签: c vb.net winapi pinvoke

这个旧的C ++函数适用于32位操作系统,但不适用于64位操作系统。 我想复制一个文件夹。 你知道我要改变什么吗?

    ************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at WindowsApplication1.FolderCopy.SHFileOperation(SHFILEOPSTRUCT& lpFileOp)
   at WindowsApplication1.FolderCopy.CopyFolder(String strSource, String strTarget)
   at WindowsApplication1.Form1.Restore()
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

这是功能:

    'Declaration of the API
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
         "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
#End Region

Public Shared Function CopyFolder(ByVal strSource As String, ByVal strTarget As String) As Boolean
    Dim _ShFile As New SHFILEOPSTRUCT
    Dim lngUsedFlags As Long
    lngUsedFlags = lngUsedFlags Or FO_Func.FOF_NOCONFIRMATION
    lngUsedFlags = lngUsedFlags Or FO_Func.FOF_NOCONFIRMMKDIR
    lngUsedFlags = lngUsedFlags Or FO_Func.FOF_NOCOPYSECURITYATTRIBS
    lngUsedFlags = lngUsedFlags Or FO_Func.FOF_NOERRORUI

    _ShFile.wFunc = FO_Func.FO_COPY
    _ShFile.fFlags = lngUsedFlags
    _ShFile.pFrom = strSource & vbNullChar
    _ShFile.pTo = strTarget & vbNullChar
    If SHFileOperation(_ShFile) = 0 Then
        Return True
    Else
        Return False
    End If
End Function

1 个答案:

答案 0 :(得分:0)

我认为问题出在hNameMappings字段。该字段的C声明为LPVOID hNameMappings,可能是4或8个字节,具体取决于体系结构(32位与64位)。我认为您需要为该字段使用IntPtr类型,因为IntPtr将是32位中的4个字节和64位中的8个字节。但是我不确定字符串字段会发生什么,因为这些指针在64位的情况下也会增长。但您可以启动并检查IntPtr字段使用hNameMappings是否有效。