Send Byte to CopyMemory

hello
I need send byte value to copymemory Function

Soft Declare Sub CopyMemory Lib “kernel32” Alias “RtlMoveMemory” ( Destination As Byte , Source As WString , Length As Integer )
Dim Pt As Integer
Dim Data( ) As Byte
1 -
Call CopyMemory( Data( pt ) , “Xojo” , 4 )
2 -
Call CopyMemory( Data( pt ) + 4 , “Xojo” , 4 )
3 -
Call CopyMemory( Data( 0 ) , “Xojo” , 4 )

I converted Byte to MemoryBlock
1 -
Dim mb As New MemoryBlock( 1 )
mb.Byte( 0 ) = Data( Pt )
2 -
Dim mb As New MemoryBlock( UBound( Data ) + 1 )
For I As Integer = 0 To UBound( Data )
mb.Byte( I ) = Data( I )
Next I
but i have problem !!!
convert mb to
( Data( pt ) + 4 ) and ( Data( 0 ) )

Thanks for any help .

Unless you’re dealing with memory not allocated by Xojo, I don’t see why you would want to use Win32’s CopyMemory function instead of Xojo’s built-in string and memory types.

That being said, the issue is that the source and destination arguments should be declared as Ptrs since they point to the memory locations. I would not use WString because CopyMemory doesn’t know about null terminators or multi-byte characters anyway.

For both the source and destination arguments (declared as Ptrs) pass in MemoryBlocks (and use MemoryBlock.Size instead of hard-coded byte counts.)

e.g.

  Soft Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( Destination As Ptr , Source As Ptr , Length As Integer )

  Dim source As MemoryBlock = "Xojo"
  Dim length As Integer = source.Size
  Dim destination As New MemoryBlock(length)
  CopyMemory(destination, source, length) 

thank you Andrew for reply
but how i can passing Data to CopyMemory

Data( pt )
Call CopyMemory( Data( pt ) , source , length)
and
Data( pt ) + Number
Call CopyMemory( Data( pt ) + 4 , source , length)
and
Data( 0 ) ’ Zero Value
Call CopyMemory( Data( 0 ) , source , length )

I’m afraid I don’t know what you’re trying to do. Array elements cannot be used as the destination, and a Byte value cannot be a string or a pointer.

Sory my English is bad …
I mean sending all this values
Data( Pt )
Data( Pt ) + 4
Data( 0 )
to Windows API
after converting Byte to MemoryBlock and passing to CopyMemory

Dim mb As New MemoryBlock( 1 )
mb.Byte( 0 ) = Data( Pt )

my problem is here
mb ’ OK
mb + 4 ’ Error
mb( 0 ) ’ !!!

VB Code

Call CopyMemory( Data( pt ) , X , X )
add 4 to Data( pt )
Call CopyMemory( Data( pt ) + 4 , X , X )
and Passing Data( 0 ) to api Function
Call WriteProcessMemory( X , X , Data( 0 ) , X , X )

thank you

are you trying to create a block of memory and populate it with some kind of 4 byte header?

eg
a Memoryblock of 10bytes

0123456789

needs to be
ABCD0123456789

You can just create a 4 byte memory block, and a 10 byte memory block
There is an override on the Add function so can literally just add these two together and the function returns a new (combined) memoryblock

The memory block can be used as a PTR in declares

hi Jeff
what is mean you create 4 byte ?
I use byte not long in MemoryBlock .

method 1
Dim mb As New MemoryBlock( 1 )
mb.Byte( 0 ) = Data( Pt )

method 2
Dim mb As New MemoryBlock( UBound( Data ) + 1 )
For I As Integer = 0 To UBound( Data )
mb.Byte( I ) = Data( I )
Next I

I think method 2 is correct

Simply
I want Get Data( 0 ) value
in vb is = 67
I can’t Get value from MemoryBlock
+
and add ( 4 ) to MemoryBlock

Call CopyMemory( Data( pt ) , X , X ) ’ OK

Call CopyMemory( Data( pt ) + 4 , X , X ) ’ !

Call WriteProcessMemory( X , X , Data( 0 ) , X , X ) ’ !

sorry my English is bad

[quote]what is mean you create 4 byte ?
I use byte not long in MemoryBlock .[/quote]

I didn’t use the term long.??

A memoryblock just holds data.
What the data is , is simply byte after byte after byte, until you use those bytes as INT32, Double, Strings…

mb.Byte( I ) treats the memoryblock as an array of bytes.
But its still a memoryblock
and mb is a pointer to the first byte.

[code]Soft Declare Sub CopyMemory Lib “kernel32” Alias “RtlMoveMemory” (destination As Ptr , source As Ptr , length As Integer)

Dim s As String = “Xojo”
Dim length As Integer = s.Len()

Dim source As New MemoryBlock(length)
source.StringValue(0, length) = s

Dim destination As New MemoryBlock(length)

CopyMemory(destination, source, length)

Dim copy As String = destination.StringValue(0, length)

MsgBox(copy)[/code]

Problem 1
VB
Call CopyMemory( Data( pt ) + 4 , X , X ) ’ work
Xojo
Call CopyMemory( Data( pt ).Byte + 4 , X , X ) ’ not work I can not pass Byte Value
Call CopyMemory( mb + 4 , X , X ) ’ Error

Problem 2
VB
Call WriteProcessMemory( X , X , Data( 0 ) , X , X ) ’ Work
Call WriteProcessMemory( X , X , 67 , X , X ) ’ VB Value
Xojo
Call WriteProcessMemory( X , X , Data( 0 ) , X , X ) ’ not work I can not pass Byte Value
Call WriteProcessMemory( X , X , mb , X , X ) ’ incorrect Value
Call WriteProcessMemory( X , X , 139 , X , X ) ’ Xojo Value

VB = Data( 0 ) = 67
Xojo = Data( 0 ).Byte = 139

Xojo is not VB6, and please read the docs (and maybe try my example, which answers your initial question).

I’m out of here…