简单的流程开放出错

时间:2016-08-23 21:31:51

标签: c# command-line

我试图打开一个进程,以便之后从中读取和写入字节。目前,我只想尝试打开它,这就是我的工作方式:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        const int PROCESS_WM_READ = 0x0010;

        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

        static void Main(string[] args)
        {
            /* Useful data */
            String bgb_path = @"C:\Users\xxxxxx\Desktop\STREAM\BGB\BGB.exe";
            String bgb_args = "-rom \"C:\\Users\\xxxxxx\\Desktop\\STREAM\\ROMs\\Pokemon Cristal\\Pokemon Crystal.gbc\"";

            Process bgb_process = new Process();

            bgb_process.StartInfo.FileName = bgb_path;
            bgb_process.StartInfo.Arguments = bgb_args;

            bgb_process.Start();

            IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, bgb_process.Id);
            Console.WriteLine("Last error : "+Marshal.GetLastWin32Error());
        }
    }
}

错误代码是299(与ReadAccessMemory或WriteAccessMemory相关),但我不使用此类方法。

希望你们能开导我:D

********编辑1 ********

我仍然从Marshal.GetLastWin32Error()获取299错误代码 这是我现在正在使用的新代码(我的一个朋友在没有任何错误的情况下使用它,在Windows 10 64位上,我拥有Windows 8.1 64位):

ProcessMemory.cs

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    public class ProcessMemory
    {

        public enum ProcessAccessType : int
        {
            PROCESS_ALL = (0x001F0FFF)
        }

        [DllImport("kernel32.dll")]
        public static extern int OpenProcess(ProcessAccessType dwDesiredAccess, bool bInheritHandle, int dwProcessId);

        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(int hObject);

        [DllImport("kernel32.dll")]
        public static extern bool ReadProcessMemory(int hProcess,
      int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

        Process process;
        private string processName;
        int processHandle;

        public ProcessMemory(string pName)
        {
            this.processName = pName;
        }

        public bool Open()
        {
            Process[] processList = Process.GetProcessesByName(processName);
            if (processList.Length == 0) return false;
            this.process = processList[0];
            processHandle = OpenProcess(ProcessAccessType.PROCESS_ALL, false, process.Id);
            return true;
        }

        public void Close()
        {
            CloseHandle(processHandle);
        }

        /** READ MEMORY **/

        private byte[] ReadMem(int address, int pSize)
        {
            int bytesRead = 0;
            byte[] buffer = new byte[pSize];
            if (this.processHandle == (int)IntPtr.Zero)
                throw new System.ComponentModel.Win32Exception();

            ReadProcessMemory(this.processHandle, address, buffer, pSize, ref bytesRead);
            return buffer;
        }

        public int ReadInt(int address)
        {
            return BitConverter.ToInt32(this.ReadMem(address, 4), 0);
        }
    }
}

Program.cs的

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
       {
            int addressOffset = 0;

            ProcessMemory pMemory = new ProcessMemory("bgb");
            if (pMemory.Open())
            {
                try
                {
                    int read = pMemory.ReadInt(addressOffset);
                    int error = Marshal.GetLastWin32Error();

                    if (error != 0)
                        throw new Exception("Marshal error "+error);        
                else
                    Console.WriteLine("Value : " + read);
                }
                catch (Exception e)
                {
                Console.WriteLine("Exception : " + e.ToString());
                }
                finally
                {
                    pMemory.Close();
                    Console.ReadKey();
                }
            }
        }
    }
}

app.manifest

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
    </application>
  </compatibility>
</assembly>

0 个答案:

没有答案