Reading from a memory address

Blitz3D Forums/Blitz3D Programming/Reading from a memory address

D4NM4N(Posted 2006) [#1]
If got a bit of an unusual task, i have to emulate keystrokes from another windows application, currently being written by another person to my blitz program.

Ive thought of using peek but if my associate writes to an area of ram, how can i read it, as the peek command doesnt seem to have an address other than a handle from createbank.

i know very little about DMA and have never used these before.

Of course if anyone has an easier way please do tell


Kev(Posted 2006) [#2]
http://www.catch22.net/tuts/undoc01.asp

scroll down to the bottom theres some info on making two process talk to each other.

kev


BlackJumper(Posted 2006) [#3]
Someone did a decls for the WinAPI which has SendMessage() and PostMessage() functions available. These can be used to place keyboard messages directly into the message queue of a window that you know the windowsHandle for.

... might be a starting point for further investigation.


Picklesworth(Posted 2006) [#4]
WM_COPYDATA can be used to safely send data between programs. I don't think it can be used with Blitz3d, though, because it involves structures and pointers.


jfk EO-11110(Posted 2006) [#5]
Using a simple Peek and Poke to absolute adresses is possbile with an userlib extension:

.lib "kernel32.dll"
RtlMoveMemory%(Destination,Source*,Length) : "RtlMoveMemory"
RtlMoveMemory2%(Destination*,Source,Length) : "RtlMoveMemory"
RtlMoveMemory4%(Destination,Source,Length) : "RtlMoveMemory"

So RtlMovememory will move to destination what is found at the adress peeked at *source.
RtlMovememory2 will move from the Adress Source to the adress that is stored at the adress destination
RtlMoveMemory4 will move data from source to destination, without to peek any pointers.
At least that's what I guess :) So correct me if I'm wrong.

While this allows to peek and poke and shutdown the puter in 1 ms, or wipe the harddrive accidently, I still don's know how you would use it to simulate keystrokes.

BTW you can use Banks in userlibs, blitz will retrieve return values of lib function calls in banks when you use their handle as an adress in the function call (well if the function wants such an adress to write the answer to).

And then there is still this hack solution to determine the true physical adress of a bank in blitz, have a look at this example:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1518


Seldon(Posted 2006) [#6]
@JFK: You can't share two memory areas between different programs, because of memory protection system. It means a memory pointer is valid only inside the program that has requested it.

A good solution are 'memory mapped files' . They work as normal files, but they're created in memory. I used this method with Blitz and I think it's the best. Look on MSDN. Of course you need userlib declarations.

If you have a window, the best solution (as already suggested) is WM_COPYDATA , but in B3D, isn't very suitable.


BlackJumper(Posted 2006) [#7]
You can't share two memory areas between different programs, because of memory protection system


Not strictly true... You can use shared data segments - I had to do this to get lowlevel keyboard trapping working for my StickyKeys_dll

see this page for some example code...


jfk EO-11110(Posted 2006) [#8]
Seldon - but it seems to work for some reason. EG a lot of Api calls ask for an adress to write their return structures to. So basicly they do write to an area of an other program?


Seldon(Posted 2006) [#9]
@BlackJumper: Yes, it's true: that is a global shared DLL , it means each application opens the same instance of the DLL. It is another method of sharing data. But you can understand you don't access the memory area of another program. You simply let two (or more) programs access the same DLL and its memory area. Usually a DLL is mapped into the memory area of the calling process, so it is like an extension of your program.

Besides, if I remember exactly there is an API function that lets you access the address of a memory area allocated by another process from the handle you pass. But I've read on MSDN it's slow and it is not recommended.

As I said, memory mapped files are the fastest and most secure way and I'd say, it's rather easy as well since you can do it in Blitz with no external DLL.

@jfk: Most of (all) the API calls ask for an "HANDLE", that is a sort of ID number that the system keeps for various objects. If you'd pass a memory address, the OS would be much faster indeed :-) (see 68k AmigaOS for example... on PowerPC AmigaOS things are changed, but I've to say AmigaOS is faster for other reasons as well) , but you'd lose memory protection.