A
A
always_drunkking2021-05-07 16:59:16
C++ / C#
always_drunkking, 2021-05-07 16:59:16

How to change a variable in a dll of another application?

There is this code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace delm
{
    
    
        public class DeleteAfterTime : MonoBehaviour
        {
            public Rect RT_MainMenu = new Rect(0f, 100f, 120f, 100f); //Rect это месторасположение меню по x,y и высота, ширина.
            public int ID_RTMainMenu = 0;
            public bool MainMenu = true;
            
            private void OnGUI()
            {
                if (this.MainMenu)
                {
                    this.RT_MainMenu = GUILayout.Window(this.ID_RTMainMenu, this.RT_MainMenu, new GUI.WindowFunction(this.Menu_MainMenu), "MainMenu", new GUILayoutOption[0]);
                }
            }
        private void Menu_MainMenu(int id) //Главное меню
        {
            if (GUILayout.Button("Set", new GUILayoutOption[0]))
            {
              maxForwardSpeed = 10f;  //Здесь код, который будет происходить при нажатии на эту кнопку
            }
        }
        private void Update() //Постоянно обновляемый метод, все что здесь будет написанно будет создаваться бесконечно
            {
                if (Input.GetKeyDown(KeyCode.V)) //Кнопка на которую будет открываться и закрываться меню, можно поставить другую
                {
                this.MainMenu = !this.MainMenu;
            }
            }
        }
    }

and a loader that is injected into the unity application through a sharp mono injector
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace delm
{
        public class Loader
        {
            public static GameObject MainClass;

            // Token: 0x040000FE RID: 2543
            public static GameObject DrawPlayers;

            // Token: 0x040000FF RID: 255
            public static GameObject DrawVeh;

            // Token: 0x04000100 RID: 256
            public static GameObject TestAim;

            // Token: 0x04000101 RID: 257
            public static GameObject GuiClass;

            public static void Load()
            {
                Loader.MainClass = new GameObject("fl_Main");
                MainClass.AddComponent<DeleteAfterTime>();
                UnityEngine.Object.DontDestroyOnLoad(Loader.MainClass);
            }
        }
    }

in general, when pressing V, a menu with a button appears in the game, when pressed, the variable that is located in Assembly-CSharp.dll should change, but the program does not see this variable, giving an error

Error CS0103 The name "maxForwardSpeed" does not exist in the current context.

there is a link to a dll with this variable in the project, I tried to register this dll in using, but then the error

error CS8370 appeared: The "top-level instructions" component is not available in C# 7.3. Use language version 9.0 or higher.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-05-07
@always_drunkking

You really don’t have a maxForwardSpeed ​​variable anywhere in your code, and it’s not clear how you are going to change it
UPD:
But seriously:

// Вроде как _hasOffceKey - это приватное поле и публичного сеттера у него нет, да и сам класс может быть internal.
object thiefController /* = каким-то образом достаём экземпляр ThiefController */;
var fieldInfo = thiefController.GetType().GetField("_hasOfficeKey", BindingFlags.Instance | BindingFlags.NonPublic);
if(fieldInfo == null)
  throw new InvalidOperationException("Что-то пошло не так");
fieldInfo.SetValue(thiefController, true);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question