Source Code How to Read null terminated char array with C#

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,061
78,998
2,370
How long you been coding/hacking?
5 years
How to Read null terminated char array with C#

I was just screwing around in C# and came up with this:

C++:
public static string ReadNullTerminatedString(IntPtr handle, IntPtr addr, int maxlength)
{
    var bytearray = new byte[maxlength];

    IntPtr bytesread = IntPtr.Zero;

    ReadProcessMemory(handle, addr, bytearray, maxlength, out bytesread);

    int nullterm = 0;
    while (nullterm < bytesread.ToInt64() && bytearray[nullterm] != 0)
    {
        nullterm++;
    }

    string s = Encoding.ASCII.GetString(bytearray, 0, nullterm);

    return s;
}
This is how you read a null terminated wide char array (unicode)

C#:
public static string ReadNullTerminatedWString(IntPtr handle, IntPtr addr, int maxlength)
{
    var bytearray = new byte[maxlength * 2];

    IntPtr bytesread = IntPtr.Zero;

    ReadProcessMemory(handle, addr, bytearray, maxlength * 2, out bytesread);

    int nullterm = 0;
    while (nullterm < bytesread.ToInt64() && bytearray[nullterm] != 0)
    {
        nullterm = nullterm + 2;
    }

    string s = Encoding.Unicode.GetString(bytearray, 0, nullterm);

    return s;
}
 
Last edited:
  • Like
Reactions: CorrM

Akaion

Wizard
Meme Tier VIP
Trump Tier Donator
Oct 13, 2018
230
7,948
14
How to Read null terminated char array with C#

I was just screwing around in C# and came up with this:

C++:
public static string ReadNullTerminatedString(IntPtr handle, IntPtr addr, int maxlength)
{
    var bytearray = new byte[maxlength];

    IntPtr bytesread = IntPtr.Zero;

    ReadProcessMemory(handle, addr, bytearray, maxlength, out bytesread);

    int nullterm = 0;
    while (nullterm < bytesread.ToInt64() && bytearray[nullterm] != 0)
    {
        nullterm++;
    }

    string s = Encoding.ASCII.GetString(bytearray, 0, nullterm);

    return s;
}
Can you share some similar methods? I'm interested how others are doing it
I don't think there's really any other way to do it aside from keep calling RPM until you find null terminator which is arguably less efficient than what you wrote. Of course unless you know the length of the array before hand.
 
  • Like
Reactions: Rake

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,061
78,998
2,370
This is how you read a null terminated wide char array (unicode)

C#:
public static string ReadNullTerminatedWString(IntPtr handle, IntPtr addr, int maxlength)
{
    var bytearray = new byte[maxlength * 2];

    IntPtr bytesread = IntPtr.Zero;

    ReadProcessMemory(handle, addr, bytearray, maxlength * 2, out bytesread);

    int nullterm = 0;
    while (nullterm < bytesread.ToInt64() && bytearray[nullterm] != 0)
    {
        nullterm = nullterm + 2;
    }

    string s = Encoding.Unicode.GetString(bytearray, 0, nullterm);

    return s;
}
 

GainOSaurusFIex

Full Member
Apr 6, 2020
46
253
1
Is there a way to Use this method without bytesread? Cause Now I am using a driver (copyvirtualmem) and doesn't return a bytesread
 

Not2EXceL

The rust BC is trash...wait no im a shit dev
Dank Tier Donator
Nobleman
Jan 20, 2013
130
1,743
3
Is there a way to Use this method without bytesread? Cause Now I am using a driver (copyvirtualmem) and doesn't return a bytesread
You really shouldn't be screwing around in kernel land if you have to ask this question
 
  • Like
Reactions: Rake

GainOSaurusFIex

Full Member
Apr 6, 2020
46
253
1
I already got it working all gud. I mean I am here to learn, I have been researching anti cheat bypasses and kernel drivers for a month. A now I am in the testing phase, yes my pc has crashed a couple of times but I am at a point where I understand everyhint and I no longer have any crashes.
 

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,061
78,998
2,370
Writing to strings in C#, I honestly barely know wtf I'm doing in C# lmao but here we go

ANSI null terminated char array, this pinvoke WPM can be used for byte arrays and other similar types
C#:
//USE THIS FOR BYTES ARRAYS,
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [MarshalAs(UnmanagedType.AsAny)] object lpBuffer, int dwSize, out IntPtr lpNumberOfBytesWritten);

public static bool WriteNullTerminatedString(IntPtr handle, IntPtr addr, string newString)
{
    newString += "\x00";

    var bytearray = Encoding.ASCII.GetBytes(newString);

    IntPtr byteswritten = IntPtr.Zero;

    bool result = WriteProcessMemory(handle, addr, bytearray, bytearray.Length, out byteswritten);

    if (result && byteswritten == new IntPtr(bytearray.Length))
        return true;
    else return false;
}
unicode null terminated char array
this WPM pinvoke is for unicode strings only
C#:
//USE THIS FOR UNICODE STRINGS ONLY
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, string lpBuffer, int dwSize, out IntPtr lpNumberOfBytesWritten);

public static bool WriteNullTerminatedWString(IntPtr handle, IntPtr addr, string newString)
{
    newString += "\x00";

    IntPtr byteswritten = IntPtr.Zero;

    VirtualProtectEx(handle, addr, new IntPtr(newString.Length * 2), 0x00000040, out oldprotect); // 0x000000040 = execute read write

    bool result = WriteProcessMemory(handle, addr, newString, newString.Length * 2, out byteswritten);

    if (result && byteswritten == new IntPtr(newString.Length * 2))
        return true;
    else return false;
}
You can't pinvoke two WPM's with the same function declaration, you can't have 1 do ANSI and the other do Unicode, so idk this is my shitty solution for right now

@KISKE just informed me I can use EntryPoint to use different function names for differently declared pinvokes
DllImportAttribute.EntryPoint Field (System.Runtime.InteropServices)

noice
 
Last edited:
  • Like
  • Love
Reactions: KISKE and Petko123
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods