Transfer Object by using NamePipe [Inter-Process Communication] VB.NET - vb.net

So far i have done the part that able to let me transfer text between the sender & receiver. Is there anyway to transfer an object by using namepipe? eg. arraylist

In vb.net you can also do this like C ….
Use serialize object and convert it to byte array transfer it and Deserialize on other end
Serialize
Dim BytArray() As Byte
Using MS As MemoryStream = Memory.Serialize(_Object)
BytArray = MS.GetBuffer()
End Using
Deserialize
Dim _Return As objType = Nothing
Using MS As System.IO.MemoryStream = New System.IO.MemoryStream(BytArray)
_Return = Memory.Deserialize(Of objType)(MS)
End Using

I am not sure about .NET or VB usage with named pipes but in Visual C++ I would pack the raw data from the array or object into a byte array and write this to the pipe. After reading this out of the pipe at the other end I would then rebuild the array or object from the raw data.

Related

Read Data From The Byte Array Returned From Web Service

I have a web service,which return data in byte array.Now i want to read that data in my console project.How can i do that,i already add the desire references to access that web service.I am using vb.net VS2012.Thanks.My web service method is as follow.
Public Function GetFile() As Byte()
Dim response As Byte()
Dim filePath As String = "D:\file.txt"
response = File.ReadAllBytes(filePath)
Return response
End Function
Something like,
Dim result As String
Using (Dim data As New MemoryStream(response))
Using (Dim reader As New StreamReader(data))
result = reader.ReadToEnd()
End Using
End Using
if you knew the encoding, lets say it was UTF-8 you could do,
Dim result = System.Text.UTF8Encoding.GetString(response)
Following on from your comments, I think you are asserting this.
Dim response As Byte() 'Is the bytes of a Base64 encoded string.
So, we know all the bytes will be valid ASCII (because its Base64,) so the string encoding is interchangable.
Dim base64Encoded As String = System.Text.UTF8Encoding.GetString(response)
Now, base64Encoded is the string Base64 representation of some binary.
Dim decodedBinary As Byte() = Convert.FromBase64String(base64Encoded)
So, we've changed the encoded base64 into the binary it represents. Now, because I can see that in your example, you are reading a file called "D:/file.txt" I'm going to make the assumption that the contents of the file is a character encoded string, but I don't know the encoding of the string. The StreamReader class has some logic in the constructor that can make an educated guess at character encoding.
Dim result As String
Using (Dim data As New MemoryStream(decodedBinary))
Using (Dim reader As New StreamReader(data))
result = reader.ReadToEnd()
End Using
End Using
Hopefully, now result contains the context of the text file.

XmlSerialize directly to GZipStream throws magic number exception on Decompression

I am trying to Serialize an object to XML however my object is a generic list containing many records and causes the serializer to consume lots of memory. So I tried to serialize directly to a GZipStream with the following code:
Dim formatter As XmlSerializer = XmlSerializerFactory.GetSerializerForType(_type)
Using _ms As New MemoryStream()
Using gzStream As New GZipStream(_ms, CompressionMode.Compress, True)
_ms.Position = 0
formatter.Serialize(gzStream, obj)
_ms.Position = 0
gzStream.Flush()
gzStream.Close()
End Using
_ms.Position = 0
Dim decompressData() As Byte
Using gzStream As New GZipStream(_ms, CompressionMode.Decompress)
ReDim decompressData(9000 - 1) 'this number doesn't matter, the data in my test sample is small
Dim Len As Integer = gzStream.Read(decompressData, 0, decompressData.Length)
End Using
End Using
However I run into an InvalidDataException The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. when trying to read the data into the decompressData array.
When I Serialize to a separate memory stream first and then compress that stream such as:
Dim formatter As XmlSerializer = XmlSerializerFactory.GetSerializerForType(_type)
Using _ms As New MemoryStream()
Dim uc_fileBytes() As Byte
Dim uc_len As Integer
Using _ms101 As New MemoryStream()
formatter.Serialize(_ms101, obj)
uc_fileBytes = _ms101.GetBuffer()
uc_len = _ms101.Length
End Using
Using gzStream As New GZipStream(_ms, CompressionMode.Compress, True)
_ms.Position = 0
gzStream.Write(uc_fileBytes, 0, uc_len)
gzStream.Flush()
gzStream.Close()
End Using
Dim decompressData() As Byte
Using gzStream As New GZipStream(_ms, CompressionMode.Decompress)
ReDim decompressData(9000 - 1)
Dim Len As Integer = gzStream.Read(decompressData, 0, decompressData.Length)
End Using
End Using
It works fine without error. But why does it fail when I serialize directly to the GZipStream?
The cause of the problem is because the GZipStream behaves differently (obviously) to the MemoryStream when writing to it. It doesn't handle paged writes very well.

Deserialize a Digital Persona template in VB.net

Reading binary data out of the database, and I need to convert it back into a Digital Persona fingerprint template. I'm not familiar with serialization and deserialization, so I could use a bit of help. Here's what I tried:
Dim rsBioData As SqlDataReader = SQL.ExecuteReader
Dim byteTemplate As Byte
Dim memStreamTemplate As MemoryStream
If rsBioData.HasRows Then
While rsBioData.Read
byteTemplate = rsBioData("BiometricData")
memStreamTemplate = New MemoryStream(byteTemplate)
Me.Template = DirectCast(template.DeSerialize(memStreamTemplate), DPFP.Template)
End While
End If
rsBioData.Close()
I receive an error that template.DeSerialize(memStreamTemplate) does not create a value.
For kicks, here's how I serialized the object to place it into the database. I assume this part is working, since the binary data shows up in SQL server--just can't read it back out to see.
Dim str As New MemoryStream
Enroller.Template.Serialize(str)
Dim serializedTemplate As Byte() = str.ToArray()
SQL.Parameters.AddWithValue("biometricData", serializedTemplate)
Thanks
Here's how I was finally able to do it. I was SO close the first time around.
byteTemplate = rsBioData("BiometricData")
memStreamTemplate = New MemoryStream(byteTemplate)
Me.Template.DeSerialize(memStreamTemplate)

Can I use an extension method with a Structure in VB.NET?

I am wondering whether I can use DataContractJsonSerializer to serialize a Structure type, or does it have to be a reference/Class type?
I have the following code:
<Extension()> Public Function ToJSON(ByVal target As Object) As String
Dim serializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(target.GetType)
Using ms As MemoryStream = New MemoryStream()
serializer.WriteObject(ms, target)
ms.Flush()
Dim bytes As Byte() = ms.GetBuffer()
Dim json As String = Encoding.UTF8.GetString(bytes, 0, bytes.Length).Trim(Chr(0))
Return json
End Using
End Function
And yet if I call it on a Structure type, such as a KeyValuePair(Of T1, T2), I get the following error:
Public member 'ToJSON' on type 'KeyValuePair(Of String,Object)' not found.
The error message does not have anything to do with DataContractJsonSerializer or anything inside your method. It cannot find the method itself. That suggests to me that you have forgotten to add a reference to the namespace in which this extension method is defined. I apologise I don’t know the VB equivalent, but in C# it is the using clause I’m talking about.

RAW Data from embedded resource image

I am trying to retrieve an image from an Embedded resource, and displaying it in its RAW DATA format (i.e. -> junk text data).
Basically, I am running into a wall with everything I attempt.
Can someone show me how to do this properly?
You can use the following msdn ref
System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(fileName)
That will give you a Stream which you can then use code cite to convert to a byte array which is pretty much the raw data.
private Function GetStreamAsByteArray(ByVal stream As System.IO.Stream) As Byte()
Dim streamLength As Integer = Convert.ToInt32(stream.Length)
Dim fileData As Byte() = New Byte(streamLength) {}
' Read the file into a byte array
stream.Read(fileData, 0, streamLength)
stream.Close()
Return fileData
End Function