change the ByRef in the parameter list to ByVal and you should be good
to go.
On 07/17/2012 08:13 AM, Rene Malingre wrote:
Hello all. Please be nice, I am trying to learn VB.NET. I do not
know c# at all.
I wish to convert a routine from c# to vb.net. I think I am messing
up the use of byte data type. Can someone please tell me if I have
gone wrong?
This is sample code from Microsoft; I am trying to write a program
that generates an XPS file, and I am ending up with zero length
resources, and I think it is from this routine.
Thanks so much.
*C# routine:*
private void WriteObfuscatedStream(
string resourceName, Stream stream, string resource)
{
int bufSize = 0x1000;
int guidByteSize = 16;
int obfuscatedByte = 32;
// Get the GUID byte from the resource name. Typical Font name:
// /Resources/Fonts/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.ODTTF
int startPos = resourceName.LastIndexOf('/') + 1;
int length = resourceName.LastIndexOf('.') - startPos;
resourceName = resourceName.Substring(startPos, length);
Guid guid = new Guid(resourceName);
string guidString = guid.ToString("N");
// Parsing the guid string and coverted into byte value
byte[] guidBytes = new byte[guidByteSize];
for (int i = 0; i < guidBytes.Length; i++)
{
guidBytes[i] = Convert.ToByte(guidString.Substring(i * 2,
2), 16);
}
using (FileStream filestream = new FileStream(resource,
FileMode.Open))
{
// XOR the first 32 bytes of the source
// resource stream with GUID byte.
byte[] buf = new byte[obfuscatedByte];
filestream.Read(buf, 0, obfuscatedByte);
for (int i = 0; i < obfuscatedByte; i++)
{
int guidBytesPos = guidBytes.Length - (i %
guidBytes.Length) - 1;
buf[i] ^= guidBytes[guidBytesPos];
}
stream.Write(buf, 0, obfuscatedByte);
// copy remaining stream from source without obfuscation
buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = filestream.Read(buf, 0, bufSize)) > 0)
{
stream.Write(buf, 0, bytesRead);
}
}
}// end:WriteObfuscatedStream()
}// end:class XpsCreate
My attempt at VB.NET
' ------------------------- WriteObfuscatedStream
------------------------
Private Sub WriteObfuscatedStream(ByRef resourceName As String,
ByRef stream As Stream, ByRef resource As String)
Const bufSize As Integer = 4096
Dim guidByteSize As Integer = 16
Dim obfuscatedByte As Integer = 32
' Get the GUID byte from the resource name. Typical Font name:
' /Resources/Fonts/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.ODTTF
Dim startPos As Integer = resourceName.LastIndexOf("/") + 1
Dim length As Integer = resourceName.LastIndexOf(".") - startPos
resourceName = resourceName.Substring(startPos, length)
Dim guid As Guid = New Guid(resourceName)
Dim guidString As String = guid.ToString("N")
' Parsing the guid string and coverted into byte value
Dim guidBytes(0 To guidByteSize - 1) As Byte
Dim i As Integer
For i = 0 To guidBytes.Length - 1
guidBytes(i) = Convert.ToByte(guidString.Substring(i * 2,
2), 16)
Next
Dim filestream As FileStream = New FileStream(resource,
FileMode.Open)
' XOR the first 32 bytes of the source
' resource stream with GUID byte.
Dim buf(0 To obfuscatedByte - 1) As Byte
filestream.Read(buf, 0, obfuscatedByte)
Dim guidBytesPos As Integer
For i = 0 To obfuscatedByte - 1
guidBytesPos = guidBytes.Length - (i Mod guidBytes.Length) - 1
buf(i) = buf(i) Xor guidBytes(guidBytesPos)
Next
stream.Write(buf, 0, obfuscatedByte)
' copy remaining stream from source without obfuscation
ReDim buf(0 To bufSize - 1)
Dim bytesRead As Integer = 0
While ((bytesRead = filestream.Read(buf, 0, bufSize)) > 0)
stream.Write(buf, 0, bytesRead)
End While
filestream.Close()
End Sub ':WriteObfuscatedStream()
--
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to dotnetdevelopment@googlegroups.com
To unsubscribe from this group, send email to
dotnetdevelopment+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net
--
Jeff K. Steinkamp (N7YG)
Tucson, AZ
Scud Missle Coordinates
N32.229 W110.875
--
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to dotnetdevelopment@googlegroups.com
To unsubscribe from this group, send email to
dotnetdevelopment+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net