Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: Mangix on September 01, 2005, 12:47 AM

Title: [VB .NET]CopyMemory
Post by: Mangix on September 01, 2005, 12:47 AM
ok im trying to Declare the CopyMemory API into VB 2005 but im running into a few problems

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long) is the correct usage in VB however in VB .NET i cant use As Any. so i made it As Variant. when i did it made it As Object which is really pissing me off.

does anyone know how i can fix this?
Title: Re: [VB .NET]CopyMemory
Post by: dxoigmn on September 01, 2005, 01:41 AM
http://www.google.com/search?hl=en&q=RtlMoveMemory+VB.NET&btnG=Google+Search
Title: Re: [VB .NET]CopyMemory
Post by: MyndFyre on September 01, 2005, 02:33 AM
NONONONONONONONONONO

There is NO REASON to use CopyMemory in VB.NET.

Use the BitConverter class.
Title: Re: [VB .NET]CopyMemory
Post by: dxoigmn on September 01, 2005, 04:09 AM
Quote from: MyndFyre on September 01, 2005, 02:33 AM
NONONONONONONONONONO

There is NO REASON to use CopyMemory in VB.NET.

Use the BitConverter class.

Hehe. I was debating whether or not to point that out but I figured let him learn the hard way since he'd probably learn a lot that way.
Title: Re: [VB .NET]CopyMemory
Post by: Mangix on September 01, 2005, 01:06 PM
@MyndFire: thanks :)

edit: sooooooo how would i go on about implementing the BitConverter Class in my class?

also note im fairly new to .NET so i dont know everything
Title: Re: [VB .NET]CopyMemory
Post by: Warrior on September 01, 2005, 01:14 PM
Isn't there a MemoryStream or something? I saw a hint of that in Jinx's buffer I was taking a look at, of course it can be C# specific
Title: Re: [VB .NET]CopyMemory
Post by: MyndFyre on September 01, 2005, 01:45 PM
Quote from: Mangix on September 01, 2005, 01:06 PM
@MyndFire: thanks :)

edit: sooooooo how would i go on about implementing the BitConverter Class in my class?

also note im fairly new to .NET so i dont know everything

The BitConverter class (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystembitconverterclasstopic.asp) is already implemented -- you don't need to implement it in your class.

I used BitConverter in my Buffer (http://cvs.sourceforge.net/viewcvs.py/jinxbot/Jinx/Jinx.Core/Net/Buffer.cs?rev=1.1&view=auto) and DataReader (http://cvs.sourceforge.net/viewcvs.py/jinxbot/Jinx/Jinx.Core/Net/DataReader.cs?rev=1.1&view=auto) classes because the StreamReader/StreamWriter classes (Part of System.IO) don't write data as consistently as I would like in my protocol.
Title: Re: [VB .NET]CopyMemory
Post by: Mangix on September 01, 2005, 01:50 PM
erm should have said that diffrently

i meant how to use it

i did "Private Bit As System.BitConverter" but it didnt work
Title: Re: [VB .NET]CopyMemory
Post by: K on September 01, 2005, 02:27 PM
Quote from: Mangix on September 01, 2005, 01:50 PM
erm should have said that diffrently

i meant how to use it

i did "Private Bit As System.BitConverter" but it didnt work

The BitConverter class is static (or "Shared" in VB.NET).  You don't need to create an instance of it.


int32 value = System.BitConverter.ToInt32(SomeByteArray, SomeIndex);
Title: Re: [VB .NET]CopyMemory
Post by: Mangix on September 01, 2005, 02:35 PM
i see

thanks :)
Title: Re: [VB .NET]CopyMemory
Post by: dxoigmn on September 01, 2005, 06:12 PM
Quote from: MyndFyre on September 01, 2005, 01:45 PM
... because the StreamReader/StreamWriter classes (Part of System.IO) don't write data as consistently as I would like in my protocol.

Hmm? What do you mean "don't write data as consistently"?
Title: Re: [VB .NET]CopyMemory
Post by: MyndFyre on September 01, 2005, 06:19 PM
Quote from: dxoigmn on September 01, 2005, 06:12 PM
Quote from: MyndFyre on September 01, 2005, 01:45 PM
... because the StreamReader/StreamWriter classes (Part of System.IO) don't write data as consistently as I would like in my protocol.

Hmm? What do you mean "don't write data as consistently"?

I mean that I wanted it to be able to write C- or Pascal-style strings and BinaryWriter/BinaryReader doesn't support it.  It seemed silly to have an extra object when a non-object class supported the same operations I wanted.
Title: Re: [VB .NET]CopyMemory
Post by: OlOOOlll on December 03, 2005, 09:55 AM
Quote from: MyndFyre on September 01, 2005, 02:33 AM
NONONONONONONONONONO

There is NO REASON to use CopyMemory in VB.NET.

Use the BitConverter class.
well i wish i had seen this before i spent 3 hrs makeing these ;/


    Public Sub CopyMemory(ByRef Destination As String, ByRef Source As Integer, ByVal Length As Short)
        Dim [String] As String = Hex$(Source)
        Dim i As Integer
        Dim l As Short
        Destination = ""
        For i = 1 To Len([String]) Step 2
            If Len([String]) > 1 Then
                l = 2
            Else : l = 1
            End If
            Destination = Destination & Right([String], l)
            [String] = Left([String], Len([String]) - l)
        Next
        [String] = Destination
        Destination = ""

        For i = 1 To Len([String]) Step 2
            Destination = Destination & Chr(CInt("&H" & Mid$([String], i, 2)))
        Next i

        While Len(Destination) < Length
            Destination = Destination & Chr(0)
        End While
    End Sub

    Public Sub CopyMemory(ByRef Destination As Integer, ByVal Source As String)
        Dim i As Integer
        Dim [String] As String = ""
        For i = 1 To Len(Source)
            [String] = Hex(Asc(Mid(Source, i, 2))) & [String]
        Next i
        Destination = CInt("&H" & [String])
    End Sub
Title: Re: [VB .NET]CopyMemory
Post by: MyndFyre on December 03, 2005, 04:36 PM
Quote from: OlOOOlll on December 03, 2005, 09:55 AM
well i wish i had seen this before i spent 3 hrs makeing these ;/
Yeah.... that's a pretty good example of how not to do things in .NET.