【问题标题】:Reading a character array or string of unknown size into a wstring using ReadProcessMemory()使用 ReadProcessMemory() 将未知大小的字符数组或字符串读入 wstring
【发布时间】:2018-06-02 17:29:30
【问题描述】:

编辑:解决了我的问题..我一直使用错误的地址...

如何使用 C++ 中的函数 ReadProcessMemory 从进程中读取大小未知的字符串或字符数组?

我尝试过的:

    std::string temp;
    ReadProcessMemory(*hProcess, (LPCVOID)(address+offset), &temp, sizeof(temp), &bytesRead);               
    mywString = string2wstring(temp);

函数string2wstring(): Source of function

std::wstring string2wstring(const std::string& str)
{
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
    std::wstring wstrTo(size_needed, 0);
    MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    return wstrTo;
}

我能够成功读取字符串,但是在运行此命令时我不断收到读取访问冲突:

#include <iostream>
#include <windows.h>
#include <string>


void main()
{
    HANDLE hProcess;
    DWORD pID = 000;
    SIZE_T bytesRead;
    uintptr_t address = 0x000;

    //HWND gameWindow = FindWindow(NULL, L"TEXTCHECK");
    //GetWindowThreadProcessId(gameWindow, &pID);

    hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pID);


    std::string temp;
    if (ReadProcessMemory(hProcess, (LPCVOID)(address), &temp, sizeof(temp), &bytesRead))
    {
    }

    std::cout << temp;

    system("pause");

}

测试:

#include <iostream>
#include <string>
#include <Windows.h>
#include <stdlib.h>

using namespace std;

int main() {

    int varInt = 123456;
    string varString = "DefaultString";
    const char arrChar[128] = "Long char array AABBCCDDEEFFGGHHIIJJKKLL"; 
    int* ptr2int = &varInt;
    int** ptr2ptr = &ptr2int;
    int*** ptr2ptr2 = &ptr2ptr;



    while (1) {
        cout << "Process ID: " << GetCurrentProcessId() << "\n";
        cout << "\n";
        cout << "varInt     (0x" << &varInt << ") = " << varInt << "\n";
        cout << "varString  (0x" << &varString << ") = " << varString << "\n";
        cout << "arrChar    (0x" << &arrChar << ") = " << arrChar << "\n";
        cout <<"\n";
        cout << "ptr2int    (0x" << &ptr2int << ") = 0x" << ptr2int << "\n";
        cout << "ptr2ptr    (0x" << &ptr2ptr << ") = 0x" << ptr2ptr << "\n";
        cout << "ptr2ptr2   (0x" << &ptr2ptr2 << ") = 0x" << ptr2ptr2 << "\n";

        cout << "\n";
        cout << "Press ENTER to print again.";
        cout << "\n";
        cout << flush;
        cin.get();
        cout << "----------------------------------------\n\n\n";
        //system("CLS");

    }
    return 0;
}

在 GETSTRINGFROMMEM.EXE [16984] 时发生托管 win32 异常

【问题讨论】:

    标签: c++ string windows readprocessmemory


    【解决方案1】:

    您必须知道要读取的确切大小。如果您不知道尺寸,您唯一的希望是:

    • 字符串数据以可以读取的大小为前缀

    • 字符串数据为空终止符,在这种情况下,您必须一次读取 1 个字符,直到找到空终止符。

    【讨论】:

    • 我该怎么做?我没有找到任何关于它的信息。我正在尝试的方法不起作用,我只是遇到了异常。
    • @JwGaming 如果您不知道要阅读的内容的确切格式,则无法有效阅读。您不能只从任意进程中读取任意数据。你到底想完成什么?
    猜你喜欢
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多