Winapi 意外输出
下面的代码应该打印出指定exe(本例中为c:\linked list.exe)中所有节的名称,但它会产生一些奇怪的输出Winapi 意外输出,winapi,executable,exe,portable-executable,Winapi,Executable,Exe,Portable Executable,下面的代码应该打印出指定exe(本例中为c:\linked list.exe)中所有节的名称,但它会产生一些奇怪的输出 #include<iostream> #include<Windows.h> #include<stdio.h> #include<WinNT.h> int main() { FILE *fp; int i; if((fp = fopen("c:\\Linked List.exe","rb"))==N
#include<iostream>
#include<Windows.h>
#include<stdio.h>
#include<WinNT.h>
int main()
{
FILE *fp;
int i;
if((fp = fopen("c:\\Linked List.exe","rb"))==NULL)
std::cout<<"unable to open";
IMAGE_DOS_HEADER imdh;
fread(&imdh,sizeof(imdh),1,fp);
IMAGE_NT_HEADERS imnth;
fread(&imnth,sizeof(imnth),1,fp);
IMAGE_SECTION_HEADER *pimsh;
pimsh = (IMAGE_SECTION_HEADER *)malloc(sizeof(IMAGE_SECTION_HEADER) * imnth.FileHeader.NumberOfSections);
fread(pimsh,sizeof(IMAGE_SECTION_HEADER),imnth.FileHeader.NumberOfSections,fp);
for(i=0;i<imnth.FileHeader.NumberOfSections;i++)
{
printf("%s\n",pimsh->Name);
pimsh++;
}
}
#包括
#包括
#包括
#包括
int main()
{
文件*fp;
int i;
if((fp=fopen(“c:\\Linked List.exe”,“rb”))==NULL)
std::cout代码的问题是,您没有读取图像头结构的正确位置,必须使用fseek(fp,imdh.e\u lfanew,0);
将文件的偏移量设置为imdh.e\u lfanew
值,然后读取图像头
记录
试试这个修改过的代码
#include "stdafx.h"
#include<iostream>
#include<Windows.h>
#include<stdio.h>
#include<WinNT.h>
int main()
{
FILE *fp;
int i;
if((fp = fopen("c:\\Linked List.exe","rb"))==NULL)
std::cout<<"unable to open";
IMAGE_DOS_HEADER imdh;
fread(&imdh,sizeof(imdh),1,fp);
//set the pointer of the file to the location of the IMAGE_NT_HEADERS record
fseek(fp, imdh.e_lfanew, 0);
IMAGE_NT_HEADERS imnth;
fread(&imnth,sizeof(imnth),1,fp);
IMAGE_SECTION_HEADER *pimsh;
pimsh = (IMAGE_SECTION_HEADER *)malloc(sizeof(IMAGE_SECTION_HEADER) * imnth.FileHeader.NumberOfSections);
fread(pimsh,sizeof(IMAGE_SECTION_HEADER),imnth.FileHeader.NumberOfSections,fp);
for(i=0;i<imnth.FileHeader.NumberOfSections;i++)
{
printf("%s\n",pimsh->Name);
pimsh++;
}
getchar();
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
int main()
{
文件*fp;
int i;
if((fp=fopen(“c:\\Linked List.exe”,“rb”))==NULL)
std::cout是可以从映像中删除DOS存根。如果此存根存在,Windows加载程序会忽略它。如果此存根不存在,Windows加载程序会满意