Convert CopyMemory to 2008/2005 VB

S

SteelMillDon

Hi,
I'm really having problems converting the CopyMemory function from VB6 to
2005/2008 VB. I'm using UDP to catch data from a real time system (intel
MBII) then store data into SQL SERVER. Below is parts of my VB6 code. I'm not
sure if I should dump the SocketTool's (3rd party addon) and do all in VB.
Can some one please point me in the right direction?
Thanks
Don
Option Explicit

Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As
Long, ByVal nPriority As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As
Long, ByVal dwPriorityClass As Long) As Long
Const REALTIME_PRIORITY_CLASS = &H100
Const HIGH_PRIORITY_CLASS = &H80


Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(lpvDest As Any, lpvSrc As Any, ByVal dwSize&)
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Type tcsDataType ' Real data coming from TM TCS System
MsgID As Long 'DINT
SeqCount As Long
QDIntelSysCntr As Long
StripID As String * 12 'in TCS as StripID(0 To 11) As Byte
ExitStripID As String * 12 'in TCS as StripID(0 To 11) As
Byte
StripWidth As Single
StripSetThick As Single
S1ActStripLength As Single
S2ActStripLength As Single
S3ActStripLength As Single
S4ActStripLength As Single
S5ActStripLength As Single
S5Speed As Single
C1RefTot As Single
C1OpTrim As Single
C1Act As Single
C2RefTot As Single
C2OpTrim As Single
C2Act As Single
ActFlatRef(0 To 53) As Single
ActFlatAct(0 To 53) As Single
C4Act As Single
SpareR01 As Single
SpareR02 As Single
SpareR03 As Single
SpareR04 As Single
SpareR05 As Single
SpareR06 As Single
SpareR07 As Single
SpareR08 As Single
SpareR09 As Single
SpareR10 As Single
SpareI01 As Long 'DINT
End Type

Private fromData As tcsDataType
Private fromBuffer(572) As Byte 'Was 272 'Make 572
Private sendBuffer(572) As Byte 'To CMCCEV
Private nBytes As Integer
Private nBytes2 As Integer


Private Sub Socket1_Read(DataLength As Integer, IsUrgent As Integer)

'Get Number of Bytes Recived from TCS
nBytes = Socket1.ReadBytes(fromBuffer)

'put into var
CopyMemory fromData, fromBuffer(0), Len(fromData)
CopyMemory sendBuffer(0), fromData, Len(fromData)
nBytes2 = Socket2.WriteBytes(sendBuffer)

' Put data into array that will be put to database table
With fromData
DataToDBTCS_Main(1) = .MsgID
DataToDBTCS_Main(2) = .SeqCount
DataToDBTCS_Main(3) = .QDIntelSysCntr
DataToDBTCS_Main(4) = timeGetTime
DataToDBTCS_Main(5) = Now
DataToDBTCS_Main(6) = .QDIntelSysCntr
DataToDBTCS_PDI(1) = .StripID
DataToDBTCS_PDI(2) = .ExitStripID
DataToDBTCS_PDI(3) = .StripWidth
DataToDBTCS_PDI(4) = .StripSetThick
DataToDBTCS_StripData(1) = .S1ActStripLength
DataToDBTCS_StripData(2) = .S2ActStripLength
DataToDBTCS_StripData(3) = .S3ActStripLength
DataToDBTCS_StripData(4) = .S4ActStripLength
DataToDBTCS_StripData(5) = .S5ActStripLength
DataToDBTCS_ProcessDataAct(1) = .S5Speed
DataToDBTCS_ProcessDataAct(2) = .S5Speed

'Put In Stand 5 Shape Data ACTUAL
DataToDBTCS_FlatnessS5Act(1) = .C1OpTrim
DataToDBTCS_FlatnessS5Act(2) = .C1Act
DataToDBTCS_FlatnessS5Act(3) = .C2OpTrim
DataToDBTCS_FlatnessS5Act(4) = .C2Act
DataToDBTCS_FlatnessS5Act(5) = .C4Act
For Index = 6 To 59
DataToDBTCS_FlatnessS5Act(Index) = .ActFlatAct(Index - 6)
Next Index

End With

End Sub


Private Sub Form_Load()
Dim Temp As Integer
OldSeqCount = 0
ErrorCountCycleTime = 0
ZeroShapeDataCount = 0
StripMovementInMeters = 0#
StripMovementInMetersResetAt10 = 0#
OldS5ActStripLength = 9999999
OldS5ActStripLengthForNewEntryCoil = 0#
StripMovementInMetersResetAt10 = 0#
OldEntryStripID = "123"
OldEntryStripID2 = "123"
OldExitStripID = "123"
NewEntryCoil = False
NewEntryCoilByZeroLengthCount = 0
ShapeDisplayAvgCount = 0
WaterFallDisplayAvgCount = 0
Timer1.Interval = 5000 ' Set Interval to 5 sec
QDIntelSysCntrOldValue = 0
TCSLinkDown = 0
TCSLinkDownResetCount = 0
DoubleW.Text = TCSLinkDownResetCount
'Zero Out Shape Display Data
For Index = 1 To 5
DataToDBTCS_ViolationParameters(Index) = 0#
DataToDBTCS_ViolationParametersZeroValue(Index) = 0#
Next Index
For Index = 0 To 53
DataToDBTCS_ShapeDisplay(Index) = 0#
DataToDBTCS_ShapeDisplayZeroValue(Index) = 0#
Next Index
'
' Initialize the socket control
'
Socket2.AddressFamily = AF_INET
Socket2.Binary = True
Socket2.Blocking = False
Socket2.Protocol = IPPROTO_IP
Socket2.SocketType = SOCK_DGRAM
'Socket2.LocalPort = 2500
Socket2.RemotePort = 3500
Socket2.HostName = "10.176.26.63"

Socket2.Action = SOCKET_OPEN
Socket1.AddressFamily = AF_INET
Socket1.Binary = True
Socket1.Blocking = False
Socket1.Protocol = IPPROTO_IP
Socket1.SocketType = SOCK_DGRAM
Socket1.LocalPort = 3000
Socket1.Action = SOCKET_OPEN


End Sub

Private Sub Form_Unload(Cancel As Integer)
If Socket1.Handle <> -1 Then Socket1.Action = SOCKET_CLOSE
MyConnection.Close
End Sub

Private Sub Socket1_LastError(ErrCode As Integer, ErrMsg As String, Response
As Integer)
MsgBox ErrMsg
End Sub

Private Sub SocketReset_Click()
Socket1.Action = SOCKET_FLUSH
If Socket1.Handle <> -1 Then Socket1.Action = SOCKET_CLOSE
Socket1.Action = SOCKET_OPEN
'Debug.Print "SOCKET RESET"
TCSLinkDownResetCount = TCSLinkDownResetCount + 1
DoubleW.Text = TCSLinkDownResetCount
End Sub
 
S

SteelMillDon

Hi Bill,
I would prefer to use traditional .NET VB code. Is there sample code that
will help in the conversion?
 
B

Bill McCarthy

Hi Don,

In this case you need to first translate your types. For example, Integer in
Vb6 is 16 bit, in .NET it's 32 bit, so you'd get a mismatch. What I suggest
is use Int16 where the VB6 code said Integer, and Int32 where it said Long.
The fields such as StripID are probably ANSI strings, so you need to apply
the MarshalAs attribute to them marking them as a fixed length string
(ByValTStr) as well as the SizeConst,
You'd also need to mark the UDT (which becomes a Structure) with the
StructLayout, e.g:


<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure tcsDataType
' ..... other fields here
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=12)> _
Public StripID As String
'more fields here
End Structure


Now given the socket you are suing requires you to read bytes, you need to
pin the byte array, then copy into the structure. e.g:

Dim handle = GCHandle.Alloc(fromBuffer, GCHandleType.Pinned)

Dim data As tcsDataType =
CType(Marshal.PtrToStructure(handle.AddrOfPinnedObject,
GetType(tcsDataType)), tcsDataType)

handle.Free()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top