J
John Koisch
All,
In vb6.0, some of our co-developers used the following ...
Private Declare Sub MoveMemory Lib "kernel32.dll"
Alias "RtlMoveMemory" (ByVal dst As Any, ByVal src As
Any, ByVal cb As Long)
This gave us some errors when we tried to use in VS.Net.
We did change it to typed:
Private Declare Sub MoveMemory Lib "kernel32.dll"
Alias "RtlMoveMemory" (ByVal dst As Byte, ByVal src As
String, ByVal cb As Long)
However, now we're getting
An unhandled exception of
type 'System.NullReferenceException' occurred in
ConsoleApplication2.exe
Additional information: Object reference not set to an
instance of an object.
Pardon the length, but code follows. Any help - this is
urgent ...
Thx
John
Module Module1Attribute
Private Declare Sub MoveMemory Lib "kernel32.dll"
Alias "RtlMoveMemory" (ByVal dst As Byte, ByVal src As
String, ByVal cb As Long)
Private Declare Sub ReMoveMemory Lib "kernel32.dll"
Alias "RtlMoveMemory" (ByVal src As String, ByVal dst As
Byte, ByVal cb As Long)
Sub Main()
Dim message As String
message = "Junk Junk Junk Junk"
Console.WriteLine(EncodeString(message))
End Sub
Public Function EncodeString(ByVal InString As
String, Optional ByVal StringToBinaryConv As Boolean =
False) As String
Dim OutString As String
Dim I As Long
Dim UnCodedArray() As Byte
Dim CodedArray() As Byte
' Begin:SCR #35900; Developer: Mag 05/08/2003
12:32 PM
If Trim$(InString) = "" Then
EncodeString = ""
Exit Function
End If
' End: SCR #35900;
'To handle compressed encounter data
'If StringToBinaryConv Then
'InString = StrConv(InString, vbUnicode)
'End If
'Pad will null characters if necessary
If Len(InString) Mod 3 <> 0 Then
Dim x As Int32
For x = 1 To (3 - Len(InString) Mod 3)
InString = InString & vbNullChar
Next
'nString = InString & String(3 - Len
(InString) Mod 3, Chr(0))
End If
'Convert string to a byte array. This is MUCH
faster than the Asc/Chr combo.
StringToByteArray(InString, UnCodedArray)
'Make sure our output array is the correct size
ReDim CodedArray(((Len(InString) / 3) * 4) - 1)
For I = 0 To (Len(InString) / 3) - 1
'Encode 4 bytes at a time
CodedArray(I * 4 + 0) = UnCodedArray(I * 3 +
0) \ 4 + 32
CodedArray(I * 4 + 1) = ((UnCodedArray(I * 3
+ 0) Mod 4) * 16) + (UnCodedArray(I * 3 + 1) \ 16 + 32)
CodedArray(I * 4 + 2) = ((UnCodedArray(I * 3
+ 1) Mod 16) * 4) + (UnCodedArray(I * 3 + 2) \ 64 + 32)
CodedArray(I * 4 + 3) = (UnCodedArray(I * 3 +
2) Mod 64) + 32
'Check for spaces and eliminate them
If CodedArray(I * 4 + 0) = 32 Then CodedArray
(I * 4 + 0) = 96
If CodedArray(I * 4 + 1) = 32 Then CodedArray
(I * 4 + 1) = 96
If CodedArray(I * 4 + 2) = 32 Then CodedArray
(I * 4 + 2) = 96
If CodedArray(I * 4 + 3) = 32 Then CodedArray
(I * 4 + 3) = 96
Next I
ByteArrayToString(CodedArray, OutString)
'Replace characters that cannot be sent in XML
'OutString = FilterRtfToXml(OutString)
EncodeString = OutString
End Function
Private Sub StringToByteArray(ByVal StringIn As
String, ByVal ByteArray() As Byte)
Dim lBytes As Long
If Len(StringIn) = 0 Then Exit Sub
lBytes = Len(StringIn)
ReDim ByteArray(lBytes - 1)
MoveMemory(ByteArray(0), StringIn, lBytes)
End Sub
Private Sub ByteArrayToString(ByVal ByteArray() As
Byte, ByVal StringOut As String)
Dim lBytes As Long
If LBound(ByteArray) > 0 Then Exit Sub 'lBound
MUST be 0
lBytes = UBound(ByteArray) + 1
StringOut = Convert.ToString(lBytes, 0)
ReMoveMemory(StringOut, ByteArray(0), lBytes * 2)
End Sub
End Module
In vb6.0, some of our co-developers used the following ...
Private Declare Sub MoveMemory Lib "kernel32.dll"
Alias "RtlMoveMemory" (ByVal dst As Any, ByVal src As
Any, ByVal cb As Long)
This gave us some errors when we tried to use in VS.Net.
We did change it to typed:
Private Declare Sub MoveMemory Lib "kernel32.dll"
Alias "RtlMoveMemory" (ByVal dst As Byte, ByVal src As
String, ByVal cb As Long)
However, now we're getting
An unhandled exception of
type 'System.NullReferenceException' occurred in
ConsoleApplication2.exe
Additional information: Object reference not set to an
instance of an object.
Pardon the length, but code follows. Any help - this is
urgent ...
Thx
John
Module Module1Attribute
Private Declare Sub MoveMemory Lib "kernel32.dll"
Alias "RtlMoveMemory" (ByVal dst As Byte, ByVal src As
String, ByVal cb As Long)
Private Declare Sub ReMoveMemory Lib "kernel32.dll"
Alias "RtlMoveMemory" (ByVal src As String, ByVal dst As
Byte, ByVal cb As Long)
Sub Main()
Dim message As String
message = "Junk Junk Junk Junk"
Console.WriteLine(EncodeString(message))
End Sub
Public Function EncodeString(ByVal InString As
String, Optional ByVal StringToBinaryConv As Boolean =
False) As String
Dim OutString As String
Dim I As Long
Dim UnCodedArray() As Byte
Dim CodedArray() As Byte
' Begin:SCR #35900; Developer: Mag 05/08/2003
12:32 PM
If Trim$(InString) = "" Then
EncodeString = ""
Exit Function
End If
' End: SCR #35900;
'To handle compressed encounter data
'If StringToBinaryConv Then
'InString = StrConv(InString, vbUnicode)
'End If
'Pad will null characters if necessary
If Len(InString) Mod 3 <> 0 Then
Dim x As Int32
For x = 1 To (3 - Len(InString) Mod 3)
InString = InString & vbNullChar
Next
'nString = InString & String(3 - Len
(InString) Mod 3, Chr(0))
End If
'Convert string to a byte array. This is MUCH
faster than the Asc/Chr combo.
StringToByteArray(InString, UnCodedArray)
'Make sure our output array is the correct size
ReDim CodedArray(((Len(InString) / 3) * 4) - 1)
For I = 0 To (Len(InString) / 3) - 1
'Encode 4 bytes at a time
CodedArray(I * 4 + 0) = UnCodedArray(I * 3 +
0) \ 4 + 32
CodedArray(I * 4 + 1) = ((UnCodedArray(I * 3
+ 0) Mod 4) * 16) + (UnCodedArray(I * 3 + 1) \ 16 + 32)
CodedArray(I * 4 + 2) = ((UnCodedArray(I * 3
+ 1) Mod 16) * 4) + (UnCodedArray(I * 3 + 2) \ 64 + 32)
CodedArray(I * 4 + 3) = (UnCodedArray(I * 3 +
2) Mod 64) + 32
'Check for spaces and eliminate them
If CodedArray(I * 4 + 0) = 32 Then CodedArray
(I * 4 + 0) = 96
If CodedArray(I * 4 + 1) = 32 Then CodedArray
(I * 4 + 1) = 96
If CodedArray(I * 4 + 2) = 32 Then CodedArray
(I * 4 + 2) = 96
If CodedArray(I * 4 + 3) = 32 Then CodedArray
(I * 4 + 3) = 96
Next I
ByteArrayToString(CodedArray, OutString)
'Replace characters that cannot be sent in XML
'OutString = FilterRtfToXml(OutString)
EncodeString = OutString
End Function
Private Sub StringToByteArray(ByVal StringIn As
String, ByVal ByteArray() As Byte)
Dim lBytes As Long
If Len(StringIn) = 0 Then Exit Sub
lBytes = Len(StringIn)
ReDim ByteArray(lBytes - 1)
MoveMemory(ByteArray(0), StringIn, lBytes)
End Sub
Private Sub ByteArrayToString(ByVal ByteArray() As
Byte, ByVal StringOut As String)
Dim lBytes As Long
If LBound(ByteArray) > 0 Then Exit Sub 'lBound
MUST be 0
lBytes = UBound(ByteArray) + 1
StringOut = Convert.ToString(lBytes, 0)
ReMoveMemory(StringOut, ByteArray(0), lBytes * 2)
End Sub
End Module