题目1:将记事本的.exe文件读取到内存,并返回读取后在内存中的地址.
#include "stdafx.h"
#include <malloc.h>
int file_length(FILE *fp);
void fun_02()
{
FILE *fp1 = NULL;
int FpSize = 0;
fp1 = fopen("C:\\Windows\\System32\\notepad.exe","rb");
FpSize = file_length(fp1);
char * FileBuffer = (char *)malloc(FpSize);
if(FileBuffer != NULL)
{
fread(FileBuffer,FpSize,1,fp1);
}
int addr = (int)FileBuffer;
printf("%x",FileBuffer);
free(FileBuffer);
fclose(fp1);
}
int file_length(FILE *fp)
{
int num;
fseek(fp,0,SEEK_END);
num = ftell(fp);
fseek(fp,0,SEEK_SET);
return num;
}
int main(int argc, char* argv[])
{
fun_02();
getchar();
return 0;
}
题目2:将内存中的数据存储到一个文件中,(.exe格式),然后双击打开,看是否能够使用
#include "stdafx.h"
#include <malloc.h>
int file_length(FILE *fp);
void fun_02()
{
FILE *fp1 = NULL;
FILE *fp2 = NULL;
int FpSize = 0;
fp1 = fopen("C:\\Windows\\System32\\notepad.exe","rb");
fp2 = fopen("D:\\Lib\\test.exe","wb");
FpSize = file_length(fp1);
char * FileBuffer = (char *)malloc(FpSize);
if(FileBuffer != NULL)
{
fread(FileBuffer,FpSize+1,1,fp1);
fwrite(FileBuffer,FpSize,1,fp2);
}
free(FileBuffer);
fclose(fp1);
fclose(fp2);
}
int file_length(FILE *fp)
{
int num;
fseek(fp,0,SEEK_END);
num = ftell(fp);
fseek(fp,0,SEEK_SET);
return num;
}
int main(int argc, char* argv[])
{
fun_02();
getchar();
return 0;
}