C
C#•3y ago
Hanami

Marshal class

So, why i cant read 8 bytes from unmanaged memory? Base address is 40000x16 and 4194304x10, not null
38 Replies
Buddy
Buddy•3y ago
It doesnt work like that What are you trying to make?
Hanami
HanamiOP•3y ago
I'm trying to read the data of another program from RAM and cram it into my program for future use
Hanami
HanamiOP•3y ago
Actually here they are
Akseli
Akseli•3y ago
because you need read access to the processes memory
Hanami
HanamiOP•3y ago
and how do I get it?
Akseli
Akseli•3y ago
if you are on windows, you can use winapi functions to do it
Hanami
HanamiOP•3y ago
I know, moreover, I already have such a solution. But I thought it was possible to use the functionality of this class. Anyway thanks for the answer
Buddy
Buddy•3y ago
what's the program that you want to read from?
Hanami
HanamiOP•3y ago
Anno 1404, game
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
ero
ero•3y ago
i do a lot with memory, maybe i can help what problem are you running into exactly? what do 40000x16 and 4194304x10 mean? base 16 and base 10? what the others said is correct, you must p/invoke win32api and do the read from there. Marshal is for managed memory within your own process
Hanami
HanamiOP•3y ago
That is, the Marshal class cannot read data from third-party processes?
ero
ero•3y ago
that's correct for inter-process (memory) reads, you must invoke kernel functions on all OSes
Hanami
HanamiOP•3y ago
Okay, thanks a lot for the answer, now everything falls into place
ero
ero•3y ago
[DllImport("kernel32")]
static extern unsafe int ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
void* lpBuffer,
UIntPtr nSize,
UIntPtr* lpNumberOfBytesRead);
[DllImport("kernel32")]
static extern unsafe int ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
void* lpBuffer,
UIntPtr nSize,
UIntPtr* lpNumberOfBytesRead);
i highly implore you upgrade to a .NET 7 preview which has much better library imports on previous versions you'll lose speed marshalling data the above can also be written as this in .net 6 and lower
[DllImport("kernel32")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
UIntPtr nSize,
out UIntPtr lpNumberOfBytesRead);
[DllImport("kernel32")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
UIntPtr nSize,
out UIntPtr lpNumberOfBytesRead);
but again, this is gonna cost you over the above for .NET 7, you can just do that, and it'll translate that into native calls with source generation for you
Jester
Jester•3y ago
wdym you dont need net7 for this
ero
ero•3y ago
yeah. that's why i said that's how you can also write it in .net 6 and lower
Jester
Jester•3y ago
i miss read Prayge
ero
ero•3y ago
[LibraryImport("kernel32")]
[return: MarshalAs(UnmanagedType.Bool)]
static partial unsafe bool ReadProcessMemory(
nint hProcess,
nint lpBaseAddress,
void* lpBuffer,
nuint nSize,
out nuint lpNumberOfBytesRead);
[LibraryImport("kernel32")]
[return: MarshalAs(UnmanagedType.Bool)]
static partial unsafe bool ReadProcessMemory(
nint hProcess,
nint lpBaseAddress,
void* lpBuffer,
nuint nSize,
out nuint lpNumberOfBytesRead);
this is how i declare it in .net 7
Jester
Jester•3y ago
isnt this breaking a TOS though? in that case this should be removed
ero
ero•3y ago
not necessarily?
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
ero
ero•3y ago
unless someone can dig up some TOS of Anno 1440 that says no memory reading lol
Jester
Jester•3y ago
looks quite likely
ero
ero•3y ago
i highly doubt it
Jester
Jester•3y ago
games just allow you to do that? Thinkies
ero
ero•3y ago
why not?
Jester
Jester•3y ago
because you can get an advantage over other players???
ero
ero•3y ago
it's a single player game from 2009...
Jester
Jester•3y ago
doesnt matter
ero
ero•3y ago
yes, it literally does matter
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Hanami
HanamiOP•3y ago
Guys, I've already used this method to read memory and there were no problems with it, so there's no need to argue🙃
Jester
Jester•3y ago
the mods decide i dont
ero
ero•3y ago
there's nothing to argue what you're doing cannot work it's impossible Marshal does not do that
Hanami
HanamiOP•3y ago
I'm not talking about Marshal, but about this
ero
ero•3y ago
so why'd you try it with Marshal? just to see if it would work?
Hanami
HanamiOP•3y ago
Yes )
Want results from more Discord servers?
Add your server