0

Today I tried to write a simple trainer for gta sa.

This program should defined position of player

After launch this programm for some reason i got such error:

System.ComponentModel.Win32Exception: "Access is denied"

Why is this happening?

What am I doing wrong?

may need to run as root? Or I do not understand something ... But the error still exists

Full code:

namespace projSanAndreasTrainer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Process[] SA;
        ProcessModule mainModule;
        ProcessMemoryReader mem = new ProcessMemoryReader();

        struct offsets
        {
            static public int health, baseAddr;
            static public int[] xPos, zPos, yPos;
            // the game which uses XZY and not XYZ
        }

        bool gameFound = false;

        float[,] teleCoords = new float[2 , 3];

        private void btnAttach_Click(object sender , EventArgs e)
        {
            try
            {
                SA = Process.GetProcessesByName("gta_sa");
                mainModule = SA[0].MainModule; // - this line causes an error
                mem.ReadProcess = SA[0];
                mem.OpenProcess();
                gameFound = true;

                offsets.baseAddr = 0xB6F5F0;
                offsets.health = 0x540;
                offsets.xPos = new int[] { 0x14 , 0x30 };
                offsets.zPos = new int[] { 0x14 , 0x34 };
                offsets.yPos = new int[] { 0x14 , 0x38 };

                btnAttach.BackColor = Color.Green; //User Feedback
                btnAttach.Enabled = false;
            }
            catch (IndexOutOfRangeException ex)
            {
                MessageBox.Show("Game not found!");
                //throw ex;
            }
        }

        private void tmrProcess_Tick(object sender , EventArgs e)
        {
            if (gameFound && !SA[0].HasExited)
            {
                // Only the base address or multiLevel addresses need to be established first
                int playerBaseAddress = mem.ReadInt(offsets.baseAddr);
                // Stores an address which is the base address of the player struct.
                int XAddress = mem.ReadMultiLevelPointer(offsets.baseAddr , 4 , offsets.xPos);
                // Multi level pointer with 2 offsets is needed to find the address of X position,
                // then this address can be read as a float.
                int ZAddress = mem.ReadMultiLevelPointer(offsets.baseAddr , 4 , offsets.zPos);
                int YAddress = mem.ReadMultiLevelPointer(offsets.baseAddr , 4 , offsets.yPos);


                lblX.Text = mem.ReadFloat(XAddress).ToString();
                lblZ.Text = mem.ReadFloat(ZAddress).ToString();
                lblY.Text = mem.ReadFloat(YAddress).ToString();

                lblHealth.Text = mem.ReadFloat(playerBaseAddress + offsets.health).ToString();

                int Hotkey = ProcessMemoryReaderApi.GetKeyState(0x74);//F5
                if ((Hotkey & 0x8000) != 0)
                {
                    // Teleport to Grove Street
                    mem.WriteFloat(XAddress , 2495);
                    mem.WriteFloat(ZAddress , -1668);
                    mem.WriteFloat(YAddress , 13);
                }

                int Hotkey2 = ProcessMemoryReaderApi.GetKeyState(0x75);//F6
                if ((Hotkey2 & 0x8000) != 0)
                {
                    // Teleport to Dome Stadium Roof
                    mem.WriteFloat(XAddress , 2737);
                    mem.WriteFloat(ZAddress , -1760);
                    mem.WriteFloat(YAddress , 44);
                }

                int Hotkey3 = ProcessMemoryReaderApi.GetKeyState(0x76);//F7
                if ((Hotkey3 & 0x8000) != 0)
                {
                    // Teleport to Skyscraper
                    mem.WriteFloat(XAddress , 1544);
                    mem.WriteFloat(ZAddress , -1353);
                    mem.WriteFloat(YAddress , 330);
                }
            }
            else
            {
                // Game has ended so stop performing readMemory etc
                gameFound = false;
                btnAttach.BackColor = Color.Red;
                btnAttach.Enabled = true;
            }
        }//tmrProcess

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

You can help me?

P.S Sorry my bad english :D

  • You get this error most likely because you try to access protected memory and lack the rights to do so. Also it is noteworthy that what you're trying to do may be prohibited by the TOS. – Vulpex Mar 28 '18 at 09:02
  • Possible duplicate of [Process.MainModule --> "Access is denied"](https://stackoverflow.com/questions/8431298/process-mainmodule-access-is-denied) – Sinatr Mar 28 '18 at 09:31

1 Answers1

0

Try to add an app.manifest below Properties of your project with the following content (replacing ):

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="<yourProjectName>" />
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="highestAvailable" uiAccess="false" />
      </requestedPrivileges>
      <applicationRequestMinimum>
        <defaultAssemblyRequest permissionSetReference="Custom" />
        <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
      </applicationRequestMinimum>
    </security>
  </trustInfo>
</asmv1:assembly>

Afterwards reference the manifest in project properties -> Application -> Below Icon and manifest in the Manifest textbox: Properties\app.manifest

By this your application does demand the highest possible privileges you can have. When starting debug, Visual Studio will also to prompt you to restart as Administrator for running the project.

If you still experience this error it may be possible that you try to write in protected memory. Writing in other process memory is a difficult task and could be restricted due to security reasons.

dsdel
  • 1,042
  • 1
  • 8
  • 11