jtwc 发表于 2022-5-27 19:06

各位老师,C++读取内存字符串数据出错?

本帖最后由 jtwc 于 2022-5-27 19:18 编辑

各位老师,C++读取内存字符串数据出错?如何改呢?谢谢了
代码如下:
char dwsunshineAddressValue = 0;
if (FALSE == ReadProcessMemory(hProcess, (void*)0x11D17D61, &dwsunshineAddressValue, sizeof(char), NULL))

                {
                        //printf("读取静态地址.\n");

                }
char a[] = { dwsunshineAddressValue };
printf(“%s\n", a);

unmask 发表于 2022-5-27 19:20

本帖最后由 unmask 于 2022-5-27 19:22 编辑

你只声明了一位char,并且也只读取了一位char,所以请打印char:printf(“%c\n", a),打印string会出现越界乱码现象。
或者读取6位字符:ReadProcessMemory(hProcess, (void*)0x11D17D61, &dwsunshineAddressValue, 6, NULL),可是可能会内存越界,因为你的栈目前只有一位char是合法的。

jtwc 发表于 2022-5-27 19:22

unmask 发表于 2022-5-27 19:20
你只声明了一位char,并且也只读取了一位char,所以请打印char:printf(“%c\n", a),打印string会出现越界 ...

老师,也出错,谢谢了

unmask 发表于 2022-5-27 19:37

jtwc 发表于 2022-5-27 19:22
老师,也出错,谢谢了

printf(“%c\n", a)

sam喵喵 发表于 2022-5-27 19:39

把char a[]改成char* a试试

jtwc 发表于 2022-5-27 19:41

unmask 发表于 2022-5-27 19:37
printf(“%c\n", a)

老师,这样只能输出1位,要输出整个字符串,谢谢了

jtwc 发表于 2022-5-27 19:44

sam喵喵 发表于 2022-5-27 19:39
把char a[]改成char* a试试

老师,试过了,不行,谢谢了

wangyujie96 发表于 2022-5-27 19:48

BOOL ReadProcessMemory(
HANDLEhProcess,
LPCVOID lpBaseAddress,
LPVOIDlpBuffer,
SIZE_TnSize,
SIZE_T*lpNumberOfBytesRead
);
Parameters
hProcessA handle to the process with memory that is being read. The handle must have PROCESS_VM_READ access to the process.
lpBaseAddressA pointer to the base address in the specified process from which to read. Before any data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for read access, and if it is not accessible the function fails.
lpBufferA pointer to a buffer that receives the contents from the address space of the specified process.
nSizeThe number of bytes to be read from the specified process.
lpNumberOfBytesReadA pointer to a variable that receives the number of bytes transferred into the specified buffer. If lpNumberOfBytesRead is NULL, the parameter is ignored.

unmask 发表于 2022-5-27 19:49

jtwc 发表于 2022-5-27 19:41
老师,这样只能输出1位,要输出整个字符串,谢谢了

int main(int argc, char const *argv[])
{
    char dwsunshineAddressValue = {0};
    if (FALSE == ReadProcessMemory(hProcess, (void *)0x11D17D61, dwsunshineAddressValue, sizeof(dwsunshineAddressValue) - 1, NULL))
    {
      // printf("读取静态地址.\n");
    }
    printf("%s\n", dwsunshineAddressValue);
}

jtwc 发表于 2022-5-27 19:54

unmask 发表于 2022-5-27 19:49
int main(int argc, char const *argv[])
{
    char dwsunshineAddressValue

老师,非常正确,谢谢了
页: [1] 2
查看完整版本: 各位老师,C++读取内存字符串数据出错?