【问题标题】:System.ComponentModel.Win32Exception: "Access is denied" - c#System.ComponentModel.Win32Exception:“访问被拒绝” - c#
【发布时间】:2018-03-28 17:06:55
【问题描述】:

今天我尝试为gta sa写一个简单的训练器。

这个程序应该定义播放器的位置

由于某种原因启动此程序后,我收到了这样的错误:

System.ComponentModel.Win32Exception: "Access is denied"

为什么会这样?

我做错了什么?

可能需要以 root 身份运行?或者我有什么不明白的......但是错误仍然存​​在

完整代码:

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)
        {

        }
    }
}

你能帮帮我吗?

P.S 对不起我的英语不好:D

【问题讨论】:

  • 您收到此错误很可能是因为您尝试访问受保护的内存并且没有这样做的权限。另外值得注意的是,您尝试做的事情可能会被 TOS 禁止。

标签: c#


【解决方案1】:

尝试在项目的属性下方添加一个 app.manifest,内容如下(替换):

<?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>

然后在项目属性 -> 应用程序 -> 下面的图标和清单文本框中的清单中引用清单:Properties\app.manifest

因此,您的应用程序确实需要您可以拥有的最高权限。开始调试时,Visual Studio 还会提示您以管理员身份重新启动以运行项目。

如果您仍然遇到此错误,则可能是您尝试在受保护的内存中写入。在其他进程内存中写入是一项艰巨的任务,并且由于安全原因可能会受到限制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多