将记事本的.exe文件读取到内存,并返回读取后在内存中的地址.

#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
int main()
{
    FILE *f = NULL;//文件指针
    f = fopen("C:\\Windows\\System32\\notepad.exe", "rb");
    if (f == NULL)
    {
        printf("文件打开失败!");
    }
    fseek(f, 0L, SEEK_END);//文件指针移动到末尾
    int length;
    //printf("写入成功!");
    length = ftell(f);//检测文件指针移动了多字节
    fseek(f, 0L, SEEK_SET);//文件指针移动到开头,不然读不出内容
    char* str = (char*)calloc(length + 1, sizeof(char));//动态分配内存,并且初始化
    if (str != NULL)
        fread(str, length, sizeof(char), f);//写入内存
    int p = (int)str;//强制转化为int
    printf("%x", p);
    free(str);//释放动态分配的空间
    fclose(f);//关闭流
    return 0;
}

将内存中的数据存储到一个文件中,(.exe格式),然后双击打开,看是否能够使用.

#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
int main()
{
    FILE* f = NULL;
    FILE* p = NULL;
    f = fopen("C:\\Windows\\System32\\notepad.exe", "rb");//读取的文件
    p = fopen("D:\\d.exe", "wb");//写出
    fseek(f, 0L, SEEK_END);
    int length;
    length = ftell(f);
    fseek(f, 0L, SEEK_SET);
    char* str = (char*)calloc(length + 1, sizeof(char));
    if (str != NULL)
    {
        fread(str, length + 1, sizeof(char), f);
        fwrite(str, length+1 , 1, p);
    }
    free(str);
    fclose(f);
    fclose(p);
    return 0;
}

最后修改:2020 年 09 月 15 日
如果觉得我的文章对你有用,请随意赞赏