连接 DWORD

Concatenate DWORDs

我想连接 2 个非字符串类型,这样我就可以将它们作为一个类型使用。 这是我的代码的主要部分:

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

int main() {
    HANDLE hwnd = FindWindowA(NULL, "MyProgram");
    DWORD ProcessId; GetWindowThreadProcessId(hwnd, &ProcessId);
    HANDLE handler = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId);
    ... ??? (type im not sure about) = 0x;          ??? ...
    ... ??? (type im not sure about) MemoryAddress; ??? ...
    int ValueOfAddress;
    ReadProcessMemory(handle, (LPVOID)(concatenated 0x+MemoryAddress), &ValueOfAddress, sizeof(ValueOfAddress), 0);
    printf("Value Of Address %? Is %d", (concatenated 0x+MemoryAddress), ValueOfAddress);
    return 0;
}

我需要将 0x 连接到我通过 ReadProcessMemory 找到的内存地址(例如 0x0023DA44,0x 是 0x,0023DA44 是从内存中读取的值)。感谢您提供的任何帮助 :) 抱歉,如果这没有意义,我不太擅长解释。基本上我需要知道如何连接到 DWORD 数据类型以获得内存地址类型变量。非常感谢任何帮助!

变量MemoryAddress中的数据是指针,不是字符串。它需要先转换成一串字符,然后才能与其他字符组合。 C 不会自动执行此转换。

printf() 函数能够接受指针(以及整数和其他类型的数字)并在打印之前将它们转换为字符。这就是格式说明符的作用(例如 %d 表示 "get the next parameter and convert it from signed int into characters, then insert the characters at this point")。

换句话说,你可以这样做:

printf("Value At Address %p Is %d", (void *)MemoryAddress, ValueOfAddress);

问题在于 C 中的指针是抽象的,可能不是一个简单的数字(例如,它们可能是两部分,如 "segment:offset");以及指针的显示方式必须由实现定义,以便代码可以移植,并且由于它是实现定义的,它可能会或可能不会使用十六进制,并且可能会或许多人不会使用 0x 前缀(取决于编译器的感觉) .

换句话说; %p 表示 "get the next parameter and convert it from a void pointer into characters using whatever makes sense for the specific type of CPU, then insert the characters at this point".

强制指针为单个数字并强制printf()以十六进制显示该单个数字(不带0x后缀);如果您使用的是 C99(或更高版本),您可以 #include <inttypes.h> 并执行此操作:

printf("Value At Address " PRIXPTR " Is %d", (uintptr_t)MemoryAddress, ValueOfAddress);

现在我们回到最初的问题——如何连接0x后缀? C 在内部并不真正支持 运行-time 连接(例如,您需要使用与字符串相关的库函数 - 例如 strcat()),并且仅在内部支持编译时字符串连接;并将 MemoryAddress 转换为字符,然后与 strcat() 进行连接,然后打印连接后的字符串会很混乱。最简单的解决方案是避免连接并将 0x 放入格式字符串中,如下所示:

printf("Value At Address 0x" PRIXPTR " Is %d", (uintptr_t)MemoryAddress, ValueOfAddress);

如果我理解正确的话,您想从包含例如"DEADBEEF" 到整数值 0xDEADBEEF?您正在寻找 strtol 函数族。

void *ptr = (void *)strtoull("DEADBEEF", NULL, 16);

然后你可以这样做:

ReadProcessMemory(handle, ptr, &ValueOfAddress, sizeof(ValueOfAddress), 0);
printf("Value Of Address %p Is %d", ptr, ValueOfAddress);

请注意,该代码中没有错误检查(特别是,您应该检查该值是否适合指针以及字符串是否为有效的十六进制)。

您不需要进行任何串联。

0x 在此上下文中只是应用于数字常量的前缀,以指示给定值采用十六进制格式。任何分配给 MemoryAddress 的值(我假设是指针类型)都可以作为第二个参数单独传递给 ReadProcessMemory