0

I am trying to code a memory cheat for a game but I want to create a menu so I decided to make it. And now I want that my program is opening the form and doing the cheat at the same time. Because now the cheat is doing it or otherwise the cheat is not working and the form is opening. I am pretty new in C# so sorry if I am noob ... :P

Thanks, IzzyMichiel

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        public static int Pbase = 0x00509B74;
        public static int Health = 0xf8;
        public static int mgammo = 0x150;
        public static int fly = 0x44;
        public void Main()
        {
            VAMemory vam = new VAMemory("ac_client");
            int localplayer = vam.ReadInt32((IntPtr)Pbase);
            {
                while (true)
                {
                   int address = localplayer + fly;
                    float aimlock = vam.ReadFloat((IntPtr)address);
                    vam.WriteFloat((IntPtr)address, -5);

                    address = localplayer + Health;
                    vam.WriteInt32((IntPtr)address, 1337);

                    address = localplayer + mgammo;
                    vam.WriteInt32((IntPtr)address, +1337);
                }
            }
        }
    }
}
Abion47
  • 22,211
  • 4
  • 65
  • 88

3 Answers3

0

Windows forms applications start out as single-threaded, so you can't do two things at once.

The typical way to handle this would be to start up a worker thread, e.g. using Task.Run.

However, since your application is so simple, we can use Application.DoEvents instead.

When you call DoEvents, your thread will check the message loop for the form and handle any pending events, such as menu clicks or whathaveyou. If there are no events pending, it'll let your main loop resume. This scheme lets you get away with running the form and the loop on the same thread.

while (true)
{
    int address = localplayer + fly;
    float aimlock = vam.ReadFloat((IntPtr)address);
    vam.WriteFloat((IntPtr)address, -5);

    address = localplayer + Health;
    vam.WriteInt32((IntPtr)address, 1337);

    address = localplayer + mgammo;
    vam.WriteInt32((IntPtr)address, +1337);

    Application.DoEvents();  //Yield control to the message loop
}

Also, make sure you are starting your message loop with Application.Run. See this answer for more information. Here's one way to do it:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • No, the thing is I am want to do the memory hack AND open the form But the form is not opening – Izzy Michiel Feb 12 '17 at 10:07
  • Oh, okay. I understand. But I just edited like you did above here but due some reasons now is my menu coming up but now is the cheat not doing anything. Why is that? Sorry for that much questions but I am pretty new in C#. I am coming from C++. And where do I need to write the cheat? In the program.cs? Or in Form1.cs? – Izzy Michiel Feb 12 '17 at 10:29
  • Okay, so I edited my code to this: http://pastebin.com/PmbDHyAS But now what it does its doing the cheat but still not opening my form. – Izzy Michiel Feb 12 '17 at 10:34
  • See my other answer. – John Wu Feb 12 '17 at 11:02
0

The problem with your code is that the constructor never exits, so the form can never be displayed.

Remove the call to Main from your constructor and put it in a Timer event handler instead. Since the timer will handle repetition for you, you can remove the while loop too.

John Wu
  • 50,556
  • 8
  • 44
  • 80
0

You can show the form and run your cheat loop simultaneously as following:

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

    public static int Pbase = 0x00509B74;
    public static int Health = 0xf8;
    public static int mgammo = 0x150;
    public static int fly = 0x44;

    /// <summary>The main entry point for the application.</summary>
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var f = new Form1();
        f.Show();

        VAMemory vam = new VAMemory("ac_client");
        int localplayer = vam.ReadInt32((IntPtr)Pbase);

        while (f.Visible) //loop while form is not closed
        {
            int address = localplayer + fly;
            float aimlock = vam.ReadFloat((IntPtr)address);
            vam.WriteFloat((IntPtr)address, -5);

            address = localplayer + Health;
            vam.WriteInt32((IntPtr)address, 1337);

            address = localplayer + mgammo;
            vam.WriteInt32((IntPtr)address, +1337);

            Application.DoEvents();  //Yield control to the message loop
        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //...
    }
}
S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61