Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winapi 意外输出_Winapi_Executable_Exe_Portable Executable - Fatal编程技术网

Winapi 意外输出

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

下面的代码应该打印出指定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"))==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加载程序会满意