问题比较复杂,善心的朋友加我QQ 24358512 MSN flovein2000@163.com
由于程序IP限制帮我调试只能通过 远程控制我机器进行。
问题根述:通过朋友介绍的文档,试了一遍又一遍,改了一次又一次运行结果总有错误。
程序是一个省短信端口调用程序,调用的是东软提供的C++DLL,在C++DLL中有许多结构类型,
其中包括联合,结构引用正常,但用到联合时总出现问题......
1 个解决方案
#1
出现以上问题是因为C#模拟的C++联合有问题,所以采用Byte数组拷贝的方式来进行比较好,下面是示例代码[楼主用的是C#,这里的是我原来做的VB.NET复制程序,请改为C#即可。]
''' <summary>
''' 内存数据复制
''' </summary>
''' <param name="hpvDest"></param>
''' <param name="hpvSource"></param>
''' <param name="cbCopy"></param>
''' <remarks>
''' </remarks>
Private Overloads Sub CopyMemory(ByRef hpvDest As Object, ByRef hpvSource As Object, ByVal cbCopy As Integer)
If TypeOf hpvSource Is Array Then
If TypeOf hpvDest Is Array Then '数组之间的数据复制
Array.Copy(hpvSource, hpvDest, Math.Min(cbCopy, Math.Min(hpvSource.Length, hpvDest.length)))
Else '数组数据复制到结构
Dim destPtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvDest))
Try
Marshal.Copy(hpvSource, 0, destPtr, Math.Min(cbCopy, Marshal.SizeOf(hpvDest)))
hpvDest = Marshal.PtrToStructure(destPtr, hpvDest.GetType())
Marshal.FreeHGlobal(destPtr)
Catch ex As Exception
Marshal.FreeHGlobal(destPtr)
Throw ex
End Try
End If
ElseIf TypeOf hpvDest Is Array Then '结构数据复制到数组
Dim SourcePtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvSource))
Try
Marshal.StructureToPtr(hpvSource, SourcePtr, True)
Marshal.Copy(SourcePtr, hpvDest, 0, Math.Min(Marshal.SizeOf(SourcePtr), Math.Min(cbCopy, hpvDest.Length())))
Marshal.FreeHGlobal(SourcePtr)
Catch ex As Exception
Marshal.FreeHGlobal(SourcePtr)
Throw ex
End Try
Else '结构之间的数据交换
Dim SourcePtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvSource))
Try
Marshal.StructureToPtr(hpvSource, SourcePtr, True)
hpvDest = Marshal.PtrToStructure(sourceptr, hpvDest.GetType())
Marshal.FreeHGlobal(SourcePtr)
Catch ex As Exception
Marshal.FreeHGlobal(SourcePtr)
Throw ex
End Try
End If
End Sub
楼主如不明白,请用MSN联系:softpig@hotmail.com
''' <summary>
''' 内存数据复制
''' </summary>
''' <param name="hpvDest"></param>
''' <param name="hpvSource"></param>
''' <param name="cbCopy"></param>
''' <remarks>
''' </remarks>
Private Overloads Sub CopyMemory(ByRef hpvDest As Object, ByRef hpvSource As Object, ByVal cbCopy As Integer)
If TypeOf hpvSource Is Array Then
If TypeOf hpvDest Is Array Then '数组之间的数据复制
Array.Copy(hpvSource, hpvDest, Math.Min(cbCopy, Math.Min(hpvSource.Length, hpvDest.length)))
Else '数组数据复制到结构
Dim destPtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvDest))
Try
Marshal.Copy(hpvSource, 0, destPtr, Math.Min(cbCopy, Marshal.SizeOf(hpvDest)))
hpvDest = Marshal.PtrToStructure(destPtr, hpvDest.GetType())
Marshal.FreeHGlobal(destPtr)
Catch ex As Exception
Marshal.FreeHGlobal(destPtr)
Throw ex
End Try
End If
ElseIf TypeOf hpvDest Is Array Then '结构数据复制到数组
Dim SourcePtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvSource))
Try
Marshal.StructureToPtr(hpvSource, SourcePtr, True)
Marshal.Copy(SourcePtr, hpvDest, 0, Math.Min(Marshal.SizeOf(SourcePtr), Math.Min(cbCopy, hpvDest.Length())))
Marshal.FreeHGlobal(SourcePtr)
Catch ex As Exception
Marshal.FreeHGlobal(SourcePtr)
Throw ex
End Try
Else '结构之间的数据交换
Dim SourcePtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvSource))
Try
Marshal.StructureToPtr(hpvSource, SourcePtr, True)
hpvDest = Marshal.PtrToStructure(sourceptr, hpvDest.GetType())
Marshal.FreeHGlobal(SourcePtr)
Catch ex As Exception
Marshal.FreeHGlobal(SourcePtr)
Throw ex
End Try
End If
End Sub
楼主如不明白,请用MSN联系:softpig@hotmail.com
#1
出现以上问题是因为C#模拟的C++联合有问题,所以采用Byte数组拷贝的方式来进行比较好,下面是示例代码[楼主用的是C#,这里的是我原来做的VB.NET复制程序,请改为C#即可。]
''' <summary>
''' 内存数据复制
''' </summary>
''' <param name="hpvDest"></param>
''' <param name="hpvSource"></param>
''' <param name="cbCopy"></param>
''' <remarks>
''' </remarks>
Private Overloads Sub CopyMemory(ByRef hpvDest As Object, ByRef hpvSource As Object, ByVal cbCopy As Integer)
If TypeOf hpvSource Is Array Then
If TypeOf hpvDest Is Array Then '数组之间的数据复制
Array.Copy(hpvSource, hpvDest, Math.Min(cbCopy, Math.Min(hpvSource.Length, hpvDest.length)))
Else '数组数据复制到结构
Dim destPtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvDest))
Try
Marshal.Copy(hpvSource, 0, destPtr, Math.Min(cbCopy, Marshal.SizeOf(hpvDest)))
hpvDest = Marshal.PtrToStructure(destPtr, hpvDest.GetType())
Marshal.FreeHGlobal(destPtr)
Catch ex As Exception
Marshal.FreeHGlobal(destPtr)
Throw ex
End Try
End If
ElseIf TypeOf hpvDest Is Array Then '结构数据复制到数组
Dim SourcePtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvSource))
Try
Marshal.StructureToPtr(hpvSource, SourcePtr, True)
Marshal.Copy(SourcePtr, hpvDest, 0, Math.Min(Marshal.SizeOf(SourcePtr), Math.Min(cbCopy, hpvDest.Length())))
Marshal.FreeHGlobal(SourcePtr)
Catch ex As Exception
Marshal.FreeHGlobal(SourcePtr)
Throw ex
End Try
Else '结构之间的数据交换
Dim SourcePtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvSource))
Try
Marshal.StructureToPtr(hpvSource, SourcePtr, True)
hpvDest = Marshal.PtrToStructure(sourceptr, hpvDest.GetType())
Marshal.FreeHGlobal(SourcePtr)
Catch ex As Exception
Marshal.FreeHGlobal(SourcePtr)
Throw ex
End Try
End If
End Sub
楼主如不明白,请用MSN联系:softpig@hotmail.com
''' <summary>
''' 内存数据复制
''' </summary>
''' <param name="hpvDest"></param>
''' <param name="hpvSource"></param>
''' <param name="cbCopy"></param>
''' <remarks>
''' </remarks>
Private Overloads Sub CopyMemory(ByRef hpvDest As Object, ByRef hpvSource As Object, ByVal cbCopy As Integer)
If TypeOf hpvSource Is Array Then
If TypeOf hpvDest Is Array Then '数组之间的数据复制
Array.Copy(hpvSource, hpvDest, Math.Min(cbCopy, Math.Min(hpvSource.Length, hpvDest.length)))
Else '数组数据复制到结构
Dim destPtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvDest))
Try
Marshal.Copy(hpvSource, 0, destPtr, Math.Min(cbCopy, Marshal.SizeOf(hpvDest)))
hpvDest = Marshal.PtrToStructure(destPtr, hpvDest.GetType())
Marshal.FreeHGlobal(destPtr)
Catch ex As Exception
Marshal.FreeHGlobal(destPtr)
Throw ex
End Try
End If
ElseIf TypeOf hpvDest Is Array Then '结构数据复制到数组
Dim SourcePtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvSource))
Try
Marshal.StructureToPtr(hpvSource, SourcePtr, True)
Marshal.Copy(SourcePtr, hpvDest, 0, Math.Min(Marshal.SizeOf(SourcePtr), Math.Min(cbCopy, hpvDest.Length())))
Marshal.FreeHGlobal(SourcePtr)
Catch ex As Exception
Marshal.FreeHGlobal(SourcePtr)
Throw ex
End Try
Else '结构之间的数据交换
Dim SourcePtr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hpvSource))
Try
Marshal.StructureToPtr(hpvSource, SourcePtr, True)
hpvDest = Marshal.PtrToStructure(sourceptr, hpvDest.GetType())
Marshal.FreeHGlobal(SourcePtr)
Catch ex As Exception
Marshal.FreeHGlobal(SourcePtr)
Throw ex
End Try
End If
End Sub
楼主如不明白,请用MSN联系:softpig@hotmail.com