程序仅适用于Visual Studio

时间:2015-11-01 01:13:23

标签: c# visual-studio kernel32

我最近创建了一个监视程序内存的C#应用​​程序。它在visual studio中完全正常运行,但是当我尝试运行调试文件夹中的可执行文件时,它无法正常工作。该程序使用Kernel32.dll库。我已经尝试使用管理员权限运行该程序,但它仍然无法正常工作。另外,创建Kernel32.dll的副本并将其放在调试文件夹中也没有帮助。

如果有帮助,这是我的代码的基础:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Memory_Seeker_CMD
{
    class Program
    {
        public static void Main()
        {
            Kernel kernel = new Kernel();

            Dictionary<string, int> memoryList = new Dictionary<string, int>();
            memoryList = kernel.SearchMemoryByInt("Memory Tester", 25);

            foreach (KeyValuePair<string, int> memory in memoryList)
                Console.WriteLine("{0}: {1}", memory.Key, memory.Value);

            Console.ReadKey();
            memoryList = kernel.SearchMemoryByInt("Memory Tester", 5, memoryList);

            foreach (KeyValuePair<string, int> memory in memoryList)
                Console.WriteLine("{0}: {1}", memory.Key, memory.Value);
            Console.ReadKey();
        }
    }

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

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

        [DllImport("kernel32.dll")]
        static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);

        const int PROCESS_QUERY_INFORMATION = 0x0400;
        const int MEM_COMMIT = 0x00001000;
        const int PAGE_READWRITE = 0x04;
        const int PROCESS_VM_READ = 0x0010;

        public struct MEMORY_BASIC_INFORMATION
        {
            public int BaseAddress;
            public int AllocationBase;
            public int AllocationProtect;
            public int RegionSize;
            public int State;
            public int Protect;
            public int lType;
        }

        public struct SYSTEM_INFO
        {
            public ushort processorArchitecture;
            ushort reserved;
            public uint pageSize;
            public IntPtr minimumApplicationAddress;
            public IntPtr maximumApplicationAddress;
            public IntPtr activeProcessorMask;
            public uint numberOfProcessors;
            public uint processorType;
            public uint allocationGranularity;
            public ushort processorLevel;
            public ushort processorRevision;
        }

        public Dictionary<string, int> SearchMemoryByInt(string processName, int value)
        {
            //this function searches through a program's memory to find a value
        }

        public Dictionary<string, int> SearchMemoryByInt(string processName, int value, Dictionary<string, int> memoryList)
        {
            //this function searches through a program's memory to find a value
        }

        public Dictionary<string, int> SearchAllMemory(string processName)
        {
            //this function searches through a program's memory and returns all results
        }
    }
}

不要介意该程序的文件目录,因为我已经尝试将可执行文件放在我的本地驱动器中。

Running with Visual Studio

Running without Visual Studio

0 个答案:

没有答案