I'm trying to search for all instances of a null-terminated string the memory of a process. I enumed all the alloced memory areas with VirtualQueryEx, then I read them with ReadProcessMemory to a byte array and search using this algo (which I found here and the author claims to be the fastest)
public static unsafe List<long> IndexesOf(byte[] Haystack, byte[] Needle) {
List<long> Indexes = new List<long>();
fixed (byte* H = Haystack) fixed (byte* N = Needle) {
long i = 0;
for (byte* hNext = H, hEnd = H + Haystack.LongLength; hNext < hEnd; i++, hNext++) {
bool Found = true;
for (byte* hInc = hNext, nInc = N, nEnd = N + Needle.LongLength; Found && nInc < nEnd; Found = *nInc == *hInc, nInc++, hInc++) ;
if (Found) Indexes.Add(i);
}
return Indexes;
}
}
It works, but it's too slow. Is there a way to memory map the process or somehow search faster in its memory?