如何在C ++中返回存储在特定内存地址的变量的名称 [英] How to return the name of a variable stored at a particular memory address in C++

查看:92
本文介绍了如何在C ++中返回存储在特定内存地址的变量的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我获得了如此多的Google搜索结果后,我第一次在这里发布了内容.

first time posting here after having so many of my Google results come up from this wonderful site.

基本上,我想找到存储在特定内存地址中的变量的名称.我有一个编写的内存编辑应用程序,该应用程序可以编辑一个值,问题是每次修补包含此值的应用程序时,我都必须将新的内存地址硬编码到我的应用程序中,然后重新编译,这需要花费大量时间才能完成.维护它几乎不值得做.

Basically, I'd like to find the name of the variable stored at a particular memory address. I have a memory editing application I wrote that edits a single value, the problem being that every time the application holding this value is patched, I have to hardcode in the new memory address into my application, and recompile, which takes so much time to upkeep that its almost not worthwhile to do.

我想做的就是获取存储在某个内存地址中的变量的名称,这样我就可以在运行时找到其地址并将其用作要编辑的内存地址.

What I'd like to do is grab the name of the variable stored at a certain memory address, that way I can then find its address at runtime and use that as the memory address to edit.

这都是用C ++编写的.

This is all being written in C++.

提前谢谢!

好吧,我已经决定要从.txt文件中流式传输数据,但是我不确定如何将字符串转换为LPVOID以用作WriteProcessMemory()中的内存地址.这是我尝试过的:

Well I've decided I'd like to stream the data from a .txt file, but I'm not sure how to convert the string into an LPVOID for use as the memory address in WriteProcessMemory(). This is what I've tried:

    string fileContents;

    ifstream memFile("mem_address.txt");
        getline(memFile, fileContents);
    memFile.close();

    LPVOID memAddress = (LPVOID)fileContents.c_str();

    //Lots of code..

    WriteProcessMemory(WindowsProcessHandle, memAddress, &BytesToBeWrote, sizeof(BytesToBeWrote), &NumBytesWrote);

代码在语法上都是正确的,可以编译并运行,但是WriteProcessMemory错误,我只能想象它与我的错误LPVOID变量有关.抱歉,如果我扩大问题的使用范围是违反规则的,我将删除我的修改.

The code is all correct in terms of syntax, it compiles and runs, but the WriteProcessMemory errors and I can only imagine it has to do with my faulty LPVOID variable. I apologize if extending the use of my question is against the rules, I'll remove my edit if it is.

推荐答案

编译并生成一个所谓的map文件.使用Visual-C ++(/MAP链接器选项)可以轻松完成此操作.在这里,您将看到符号(功能,...)及其起始地址.使用此映射文件(警告:每次重新编译时都必须更新),您可以将地址与名称匹配.

Compile and generate a so called map file. This can be done easily with Visual-C++ (/MAP linker option). There you'll see the symbols (functions, ...) with their starting address. Using this map file (Caution: has to be updated each time you recompile) you can match the addresses to names.

这实际上不是那么容易,因为地址是相对于首选加载地址的,并且(随机化)可能与实际加载地址不同.

This is actually not so easy because the addresses are relative to the preferred load address, and probably will (randomization) be different from the actual load address.

有关检索正确地址的一些旧提示可以在以下位置找到: http ://home.hiwaay.net/~georgech/WhitePapers/MapFiles/MapFiles.htm

Some old hints on retrieving the right address can be found here: http://home.hiwaay.net/~georgech/WhitePapers/MapFiles/MapFiles.htm

这篇关于如何在C ++中返回存储在特定内存地址的变量的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆