c - 尝试运行代码时出错

标签 c memory

每当我尝试运行以下代码时,都会出现错误。 Image_Error

Image2_Error 添加了来自 Visual Studio 的另一个错误图像。 Image3_error 添加了错误的确切来源。

The following bit of code here is : 

void update_memblock(MEMBLOCK *mb)
{
    static unsigned char tempbuf[128 * 1024];
    unsigned int bytes_left;
    unsigned int total_read;
    unsigned int bytes_to_read;
    unsigned int bytes_read;


        bytes_left = mb->size;
        total_read = 0;


        while (bytes_left)
        {
            bytes_to_read = (bytes_left > sizeof(tempbuf)) ? sizeof(tempbuf) : bytes_left;
            ReadProcessMemory(mb->hProc, mb->addr + total_read, tempbuf, bytes_to_read, (DWORD*)&bytes_read);
            if (bytes_read != bytes_to_read) break;

            memcpy(mb->buffer + total_read, tempbuf, bytes_read);

            bytes_left -= bytes_read;
            total_read += bytes_read;
        }

        mb->size = total_read;

}



The Structure looks as follows : 
typedef struct _MEMBLOCK { 
    HANDLE hProc;   
unsigned char *addr;    
int size;   
unsigned char *buffer;
    struct _MEMBLOCK *next;

} MEMBLOCK;

尝试了相当多的切换,从更改数组大小到删除变量并将其切换为另一个,显然它仍然会抛出此错误。请帮我找出问题所在。谢谢。

最佳答案

如果您阅读 documentation ,您会注意到 ReadProcessMemory 的参数应该是 SIZE_TDWORD 类型为 32 位,在 64 位平台上大小为 64 位。虽然 bytes_to_read 并不重要,因为它是按值传递的,但 8 个字节将写入 bytes_read 指向的指针,其 sizeof 是 4。

此外,您应该得到一个指针类型不匹配,因为在这种情况下 DWORD * SIZE_T * 兼容。

关于c - 尝试运行代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50269214/

相关文章:

c - 以 root 身份打开文件,但在读取文件之前放弃权限?

计数变量,lex/flex 中的数组

c - 大约运行时间

iphone - Iphone App在4.0或更低版本的SDK上崩溃

Java使用HashMap进行内存管理

ios - UIImagePickerController 不会从/tmp/capture 中删除临时视频捕获文件

C 结构体内存存储顺序

c++ - 为什么当klee执行Objectfile时sleep()函数不能工作?

sql-server - SQL Server CLR 内存分配

c - 如何正确释放我的阵列而不会出错?