|
Post by held12345 on Apr 7, 2023 9:53:20 GMT -5
Hi good afternoon.
I would like in a struct array : struct testarray a as long[20]
write 20 values (long) via a for/next in the struct and then read 20 values (long) output from struct and print.
how is it please
Thanks
|
|
|
Post by Rod on Apr 7, 2023 10:04:46 GMT -5
|
|
|
Post by held12345 on Apr 7, 2023 12:20:42 GMT -5
thanks for the help.
greeting
|
|
|
Post by held12345 on Apr 8, 2023 2:03:20 GMT -5
Hi good afternoon.
How can you now please find out the address of the reserved memory in order to use the reserved memory with a dll?
calldll #handle, "struct" , memory address...... print .....
Thanks.
OPEN "lbb_dll.dll" for dll as #handle
elements = 99 Dim structArray(elements) struct test, b As long sizeofTest = Len(test.struct) memBlockSize = (elements+1)*sizeofTest print memBlockSize Open "kernel32.dll" For DLL As #kernel hSArray = GlobalAlloc(memBlockSize) ptrSarray = GlobalLock( hSArray ) For i = 0 to 10 test.b.struct = i *11 dest = ptrSarray+(i*sizeofTest) CallDLL #kernel,"RtlMoveMemory", dest As long, test As ptr, sizeofTest As long, ret As void structArray(i) = dest Next i For i = 0 to 10 test.struct = structArray(i) B = test.b.struct Print (B) Next i wait [quit] ret = GlobalFree(hSArray) Close #kernel End Function GlobalAlloc( dwBytes ) CallDLL #kernel, "GlobalAlloc", _GMEM_MOVEABLE As long, dwBytes As ulong, GlobalAlloc As long End Function Function GlobalLock( hMem ) CallDLL #kernel, "GlobalLock", hMem As long, GlobalLock As long End Function Function GlobalFree( hMem ) CallDLL #kernel, "GlobalFree", hMem As ulong, GlobalFree As long End Function
|
|
|
Post by Chris Iverson on Apr 8, 2023 22:44:56 GMT -5
If you're using movable shared memory with GlobalAlloc, calling GlobalLock() returns a pointer.
In fact, you've got it written in your code.
ptrSarray is the pointer to the usable global memory block, that variable contains the address. By doing the math, you're getting the addresses you need.
HOWEVER... what is it you're trying to do? Are you trying to fill an array with the addresses? Or the values?
RtlMoveMemory is used to move the VALUES around, not the addresses. If you want the addresses in the array, just store the "dest" value you calculate.
|
|
|
Post by held12345 on Apr 9, 2023 2:57:04 GMT -5
I would like to print the values from the pointer address. for z=0 to 10 I would like to print 10 values from the pointer address Print (B) next greeting this is the dll: ------------------------ ProcedureCDLL.l scrstructtest(*adr) For a.l = 0 To 20 PokeL(*adr,a.l) *adr=*adr+4 Next ProcedureReturn EndProcedure -------------------------
-------------------------
elements = 99 struct test, b As long sizeofTest = Len(test.struct) memBlockSize = (elements+1)*sizeofTest print memBlockSize Open "kernel32.dll" For DLL As #kernel OPEN "lbb_dll.dll" for dll as #handle hSArray = GlobalAlloc(memBlockSize) ptrSarray = GlobalLock( hSArray ) print calldll #handle, "scrstructtest" , ptrSarray as long , ret as void for z=0 to 10 I would like to print 10 values from the pointer address Print (B) next wait ret = GlobalFree(hSArray) Close #kernel End Function GlobalAlloc( dwBytes ) CallDLL #kernel, "GlobalAlloc", _GMEM_MOVEABLE As long, dwBytes As ulong, GlobalAlloc As long End Function Function GlobalLock( hMem ) CallDLL #kernel, "GlobalLock", hMem As long, GlobalLock As long End Function Function GlobalFree( hMem ) CallDLL #kernel, "GlobalFree", hMem As ulong, GlobalFree As long End Function ------------------------------
|
|
|
Post by Chris Iverson on Apr 9, 2023 3:33:58 GMT -5
Ok, in that case, you've almost got it.
Here's your previous example, modified to properly store and retrieve the values in the memory array.
Also, as a matter of good practice, you should unlock the global memory section before freeing it. DON'T unlock it while you still are using pointers to it, though, or you'll invalidate those pointers.
elements = 99 Dim structArray(elements)
struct test, b As long
sizeofTest = Len(test.struct) memBlockSize = (elements+1)*sizeofTest
print memBlockSize
Open "kernel32.dll" For DLL As #kernel
hSArray = GlobalAlloc(memBlockSize) ptrSarray = GlobalLock( hSArray )
For i = 0 to 10 test.b.struct = i *11 dest = ptrSarray+(i*sizeofTest)
'Use ULONG for pointers, to prevent any possible signed value issues 'Pass structs using "AS STRUCT" CallDLL #kernel,"RtlMoveMemory", dest As ulong, test As struct, sizeofTest As long, ret As void structArray(i) = dest Next i
For i = 0 to 10 'Do the same as above in reverse to retrieve the values src = structArray(i) CallDLL #kernel,"RtlMoveMemory", test as struct, src as ulong, sizeofTest As long, ret As void B = test.b.struct Print (B) Next i
input a
[quit] ret = GlobalUnlock(hSArray) ret = GlobalFree(hSArray) Close #kernel End
Function GlobalAlloc( dwBytes ) CallDLL #kernel, "GlobalAlloc", _GMEM_MOVEABLE As long, dwBytes As ulong, GlobalAlloc As long End Function
Function GlobalLock( hMem ) CallDLL #kernel, "GlobalLock", hMem As long, GlobalLock As long End Function
Function GlobalUnlock( hMem) CallDLL #kernel, "GlobalUnlock", hMem as ulong, GlobalUnlock as long End Function
Function GlobalFree( hMem ) CallDLL #kernel, "GlobalFree", hMem As ulong, GlobalFree As long End Function
|
|
|
Post by held12345 on Apr 9, 2023 6:04:50 GMT -5
hello thanks, it runs wonderfully with the dll.
------------------------------ ProcedureCDLL.l scrstructtest(*adr) For a.l = 0 To 20 PokeL(*adr,a.l*1111) *adr=*adr+4 Next ProcedureReturn EndProcedure -------------------------------
OPEN "lbb_dll.dll" for dll as #handle elements = 99 Dim structArray(elements)
struct test, b As long
sizeofTest = Len(test.struct) memBlockSize = (elements+1)*sizeofTest
print memBlockSize print
Open "kernel32.dll" For DLL As #kernel
hSArray = GlobalAlloc(memBlockSize) ptrSarray = GlobalLock( hSArray )
For i = 0 to 99 dest = ptrSarray+(i*sizeofTest) CallDLL #kernel,"RtlMoveMemory", dest As ulong, test As struct, sizeofTest As long, ret As void structArray(i) = dest Next i calldll #handle, "scrstructtest" , ptrSarray as ptr , erg as void for z=0 to 15 src = structArray(z) CallDLL #kernel,"RtlMoveMemory", test as struct, src as ulong, sizeofTest As long, ret As void B = test.b.struct Print (B) next
wait
[quit] ret = GlobalUnlock(hSArray) ret = GlobalFree(hSArray) Close #kernel End
Function GlobalAlloc( dwBytes ) CallDLL #kernel, "GlobalAlloc", _GMEM_MOVEABLE As long, dwBytes As ulong, GlobalAlloc As long End Function
Function GlobalLock( hMem ) CallDLL #kernel, "GlobalLock", hMem As long, GlobalLock As long End Function
Function GlobalUnlock( hMem) CallDLL #kernel, "GlobalUnlock", hMem as ulong, GlobalUnlock as long End Function
Function GlobalFree( hMem ) CallDLL #kernel, "GlobalFree", hMem As ulong, GlobalFree As long End Function[/div][div]
if i don't do this run, it doesn't work and the program crashes. why?
---------------------------------- For i = 0 to 99 dest = ptrSarray+(i*sizeofTest) CallDLL #kernel,"RtlMoveMemory", dest As ulong, test As struct, sizeofTest As long, ret As void structArray(i) = dest Next i ----------------------------------
Attachments:
|
|
|
Post by held12345 on Apr 9, 2023 8:06:00 GMT -5
Hello, I have now created my dll with freebasic. work wonderfully.
Thanks to you.
liberty-demo freebasic-dll with struct : OPEN "free_dll.dll" for dll as #handle
struct winrect, orgx as long , orgy as long
x=6789 y=107 calldll #handle, "addnum@8" , x as long, y as long , erg as long print(erg)
print
x=133 y=109 calldll #handle, "addasm@8" , x as long, y as long , erg as long print(erg)
print
x=134 y=107 calldll #handle, "multasm@8" , x as long, y as long , erg as long print(erg)
print
winrect.orgx.struct=7893 winrect.orgy.struct=1234 calldll #handle, "ptrsendadd@4" , winrect as struct , erg as long print erg
print
calldll #handle, "ptrsend@4" , winrect as struct , erg as void print winrect.orgx.struct print winrect.orgy.struct
print
wait
liberty-demo freebasic-dll with struct-arry: OPEN "free_dll.dll" for dll as #handle elements = 99 Dim structArray(elements)
struct test, b As long
sizeofTest = Len(test.struct) memBlockSize = (elements+1)*sizeofTest
print memBlockSize print
Open "kernel32.dll" For DLL As #kernel
hSArray = GlobalAlloc(memBlockSize) ptrSarray = GlobalLock( hSArray )
For i = 0 to 99 dest = ptrSarray+(i*sizeofTest) CallDLL #kernel,"RtlMoveMemory", dest As ulong, test As struct, sizeofTest As long, ret As void structArray(i) = dest Next i calldll #handle, "ptrsendarry@4" , ptrSarray as ptr , erg as void for z=0 to 15 src = structArray(z) CallDLL #kernel,"RtlMoveMemory", test as struct, src as ulong, sizeofTest As long, ret As void B = test.b.struct Print (B) next
wait
[quit] ret = GlobalUnlock(hSArray) ret = GlobalFree(hSArray) Close #kernel End
Function GlobalAlloc( dwBytes ) CallDLL #kernel, "GlobalAlloc", _GMEM_MOVEABLE As long, dwBytes As ulong, GlobalAlloc As long End Function
Function GlobalLock( hMem ) CallDLL #kernel, "GlobalLock", hMem As long, GlobalLock As long End Function
Function GlobalUnlock( hMem) CallDLL #kernel, "GlobalUnlock", hMem as ulong, GlobalUnlock as long End Function
Function GlobalFree( hMem ) CallDLL #kernel, "GlobalFree", hMem As ulong, GlobalFree As long End Function[/div][div]
dll freebasic : Function addnum Alias "addnum"( ByVal a As Integer, ByVal b As Integer ) As Integer Export Function = a + b End Function
Function ptrsend Alias "ptrsend" (c As Integer Ptr) As integer Export c[0]=12345 c[1]=67890 End Function
Function ptrsendadd Alias "ptrsendadd" (c As Integer Ptr) As integer Export Dim f As Integer Dim g As Integer f=c[0] g=c[1] Function = f + g End Function
Function ptrsendarry Alias "ptrsendarry" (c As Integer Ptr) As integer Export Dim f As Integer For f=0 To 20 c[f]=f*115 Next End function
Function addasm Alias "addasm" (Byval a As Integer, ByVal b As Integer) As Integer Export Asm mov eax, [a] Add eax, [b] mov [Function], eax End Asm End Function
Function multasm Alias "multasm" (Byval a As Integer, ByVal b As Integer) As Integer Export Asm mov eax, [a] imul eax, [b] mov [Function], eax End Asm End Function
|
|