April 09, 2020 Re: To get memory from another process. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Quantium | On Thursday, 9 April 2020 at 17:23:19 UTC, Quantium wrote: > Ok. For training example, we're using Windows 10 Por. We can use WinAPI. Are there any D libs to use WinAPI? I have used the Windows API to read/write into a different process before. Here is some example code in case it's useful: (I removed some stuff without recompiling so it may have some errors) ``` version(Windows): pragma(lib, "Kernel32.lib"); pragma(lib, "Psapi.lib"); struct WinProcess { import core.sys.windows.winbase: OpenProcess, ReadProcessMemory, WriteProcessMemory, CloseHandle; import core.sys.windows.windows : PROCESS_VM_READ, PROCESS_VM_WRITE, PROCESS_QUERY_INFORMATION, PROCESS_VM_OPERATION, HANDLE; import std.bitmanip; import std.exception: enforce; int processId = -1; /// Id of the process this is attached to HANDLE processHandle = null; /// Windows handle of the process this(int processId) { this.processId = processId; const access = PROCESS_VM_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_WRITE | PROCESS_VM_OPERATION; this.processHandle = OpenProcess(access, false, processId); enforce(processHandle, "could not open process"); } import std.traits: isNumeric; void write(T)(void* address, T value) if (isNumeric!T) { enforce(processHandle != null, "not attached to a process yet"); size_t bytesWritten = 0; ubyte[T.sizeof] buffer; auto b = buffer[]; b.write(value, 0); WriteProcessMemory(processHandle, address, cast(void*) buffer, buffer.sizeof, &bytesWritten); enforce(bytesWritten == T.sizeof, "could not write all bytes"); } T read(T)(void* address) if (isNumeric!T) { enforce(processHandle != null, "not attached to a process yet"); size_t bytesRead = 0; ubyte[T.sizeof] buffer; ReadProcessMemory(processHandle, address, cast(void*) buffer, buffer.sizeof, &bytesRead); enforce(bytesRead == T.sizeof, "could not read all bytes"); auto b = buffer[]; // lvalue return b.read!T; } } ``` |
April 09, 2020 Re: To get memory from another process. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis | I see this code imports drivers and does it depend on processor architecture? Would it work only on 64-bit or 32-bit or some special architechtures? |
April 09, 2020 Re: To get memory from another process. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Quantium | On Thursday, 9 April 2020 at 19:27:16 UTC, Quantium wrote:
> I see this code imports drivers and does it depend on processor architecture? Would it work only on 64-bit or 32-bit or some special architechtures?
kernel32.dll and psapi.dll should be present on any normal Windows 10 installation.
Windows only runs on x86 and ARM processors as far as I know. I have never used Windows with an ARM processor, but I assume such a Windows installation has the full WinAPI implemented, in which case it should work.
As for 32-bit/64-bit on x86:
- 32-bit OMF: might work, but I often get errors because the Digital Mars import libraries for Windows dll's are outdated so I don't recommend this target
- 32-bit COFF: pretty sure it works
- 64-bit COFF: definitely works, I use this regularly.
In any case, I suggest you just try these out to see yourself.
|
April 10, 2020 Re: To get memory from another process. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis | On 10/04/2020 7:42 AM, Dennis wrote:
> On Thursday, 9 April 2020 at 19:27:16 UTC, Quantium wrote:
>> I see this code imports drivers and does it depend on processor architecture? Would it work only on 64-bit or 32-bit or some special architechtures?
>
> kernel32.dll and psapi.dll should be present on any normal Windows 10 installation.
>
> Windows only runs on x86 and ARM processors as far as I know. I have never used Windows with an ARM processor, but I assume such a Windows installation has the full WinAPI implemented, in which case it should work.
>
> As for 32-bit/64-bit on x86:
>
> - 32-bit OMF: might work, but I often get errors because the Digital Mars import libraries for Windows dll's are outdated so I don't recommend this target
> - 32-bit COFF: pretty sure it works
> - 64-bit COFF: definitely works, I use this regularly.
>
> In any case, I suggest you just try these out to see yourself.
These API's are old and well used. They will work no problem on all targets.
|
Copyright © 1999-2021 by the D Language Foundation