手动创建.exe抛出错误“此应用程序无法在您的PC上运行”

时间:2017-02-16 20:24:45

标签: c exe portable-executable

我创建了一个程序来手动创建一个只有1个.text / .code部分的.exe(PE可执行文件)。但是,每当我打开它时都会抛出以下错误:“此应用程序无法在您的PC上运行”。为什么会抛出这个错误?是因为我错误地填写了数据还是错过了什么?

这是代码(它只是一个基本的,大多数值都是硬编码的)。如果我错了一些,请告诉我。 PE文件格式详细信息:link

#include<stdio.h>

int main(){

     char DOS_MESSAGE[] = {14,31,186,14,0,180,9,205,33,184,1,76,
      205,33,84,104,105,115,32,112,114,111,103,114,97,109,32,99,97,110,110,
      111,116,32,98,101,32,114,117,110,32,105,110,32,68,79,83,32,109,111,
      100,101,46,13,13,10,36,0,0,0,0,0,0,0};
     char CODE_RAW[] = {85,72,139,5,184,19,0,0};

     int PAD = 0;
     int i = 0;

     struct data_directory
     { 
        long VirtualAddress;
        long Size;
     };

     struct DOS_Header 
     {
         char signature[2];
         short lastsize;
         short nblocks;
         short nreloc;
         short hdrsize;
         short minalloc;
         short maxalloc;
         short ss;
         short sp;
         short checksum;
         short ip;
         short cs;
         short relocpos;
         short noverlay;
         short reserved1[4];
         short oem_id;
         short oem_info;
         short reserved2[10];
         long  e_lfanew;
    };

    struct PE_Header{
         char signature[4];
    };


   struct COFFHeader
   {
       short Machine;
       short NumberOfSections;
       long TimeDateStamp;
       long PointerToSymbolTable;
       long NumberOfSymbols;
       short SizeOfOptionalHeader;
       short Characteristics;
   };

   struct PEOptHeader
   {

      short signature;
      char MajorLinkerVersion; 
      char MinorLinkerVersion;
      long SizeOfCode;
      long SizeOfInitializedData;
      long SizeOfUninitializedData;
      long AddressOfEntryPoint;  
      long BaseOfCode;
      long BaseOfData;
      long ImageBase;
      long SectionAlignment;
      long FileAlignment;
      short MajorOSVersion;
      short MinorOSVersion;
      short MajorImageVersion;
      short MinorImageVersion;
      short MajorSubsystemVersion;
      short MinorSubsystemVersion;
      long Win32VersionValue;
      long SizeOfImage;
      long SizeOfHeaders;
      long Checksum;
      short Subsystem;
      short DLLCharacteristics;
      long SizeOfStackReserve;
      long SizeOfStackCommit;
      long SizeOfHeapReserve;
      long SizeOfHeapCommit;
      long LoaderFlags;
      long NumberOfRvaAndSizes;
      struct data_directory DataDirectory[16];     
   };               

struct SECTION{
    char SectionName[8];
    long VirtualSize;
    long VirtualAddress;
    long SizeOfRawData;
    long PointerToRawData;
    long PointerToRelocations;
    long PointerToLineNumbers;
    short NumberOfRealocations;
    short NumberOfLineNumbers;
    long Characteristics;
};


FILE *pfile = NULL;
struct DOS_Header DOSHEADER,*PTRDOSHEADER = NULL;
struct PE_Header PEHEADER,*PTRPEHEADER = NULL;
struct COFFHeader COFFHEADER, *PTRCOFFHEADER = NULL;
struct PEOptHeader PEOPTHEADER, *PTRPEOPTHEADER = NULL;
struct SECTION CODE,*PCODE = NULL;

DOSHEADER.signature[0] = 'M';
DOSHEADER.signature[1] = 'Z';
DOSHEADER.lastsize = 144;
DOSHEADER.nblocks = 3;
DOSHEADER.nreloc = 0;
DOSHEADER.hdrsize = 4;
DOSHEADER.minalloc = 0;
DOSHEADER.maxalloc = 65535;
DOSHEADER.ss = 0;
DOSHEADER.sp = 0;
DOSHEADER.checksum = 0;
DOSHEADER.ip = 0;
DOSHEADER.cs = 0;
DOSHEADER.reserved1[0] = 0;
DOSHEADER.reserved1[1] = 0;
DOSHEADER.reserved1[2] = 0;
DOSHEADER.reserved1[3] = 0;
DOSHEADER.oem_id = 0;
DOSHEADER.oem_info = 0;
DOSHEADER.reserved2[0] = 0;
DOSHEADER.reserved2[1] = 0;
DOSHEADER.reserved2[2] = 0;
DOSHEADER.reserved2[3] = 0;
DOSHEADER.reserved2[4] = 0;
DOSHEADER.reserved2[5] = 0;
DOSHEADER.reserved2[6] = 0;
DOSHEADER.reserved2[7] = 0;
DOSHEADER.reserved2[8] = 0;
DOSHEADER.reserved2[9] = 0;
DOSHEADER.e_lfanew = 0;



PEHEADER.signature[0] = 'P';
PEHEADER.signature[1] = 'E';
PEHEADER.signature[2] = '\0';
PEHEADER.signature[3] = '\0';

COFFHEADER.Machine = 332;
COFFHEADER.NumberOfSections = 1;
COFFHEADER.TimeDateStamp = 1484783481;
COFFHEADER.PointerToSymbolTable = 0;
COFFHEADER.NumberOfSymbols = 0;
COFFHEADER.SizeOfOptionalHeader = sizeof(struct PEOptHeader);
COFFHEADER.Characteristics = 2;

PEOPTHEADER.signature = 267;
PEOPTHEADER.MajorLinkerVersion = 2;
PEOPTHEADER.MinorLinkerVersion = 18;
PEOPTHEADER.SizeOfCode = sizeof(CODE);
PEOPTHEADER.SizeOfInitializedData = 0;
PEOPTHEADER.SizeOfUninitializedData = 0;
PEOPTHEADER.AddressOfEntryPoint = 4096;
PEOPTHEADER.BaseOfCode = 4096;
PEOPTHEADER.BaseOfData = 0;
PEOPTHEADER.ImageBase = 4194304;
PEOPTHEADER.SectionAlignment = 4096;
PEOPTHEADER.FileAlignment = 512;
PEOPTHEADER.MajorOSVersion = 1;
PEOPTHEADER.MinorOSVersion = 0;
PEOPTHEADER.MajorImageVersion = 1;
PEOPTHEADER.MinorImageVersion = 0;
PEOPTHEADER.MajorSubsystemVersion = 4;
PEOPTHEADER.MinorSubsystemVersion = 0;
PEOPTHEADER.Win32VersionValue = 0;
PEOPTHEADER.SizeOfImage = 1024 + sizeof(CODE_RAW);
PEOPTHEADER.SizeOfHeaders = sizeof(PEHEADER) + sizeof(COFFHEADER) + sizeof(PEOPTHEADER);
PEOPTHEADER.Checksum = 0;
PEOPTHEADER.Subsystem = 3;
PEOPTHEADER.DLLCharacteristics = 0;
PEOPTHEADER.SizeOfStackReserve = 1048576;
PEOPTHEADER.SizeOfStackCommit = 4096;
PEOPTHEADER.SizeOfHeapReserve = 1048576;
PEOPTHEADER.SizeOfHeapCommit = 4096;
PEOPTHEADER.LoaderFlags = 0;
PEOPTHEADER.NumberOfRvaAndSizes = 16;

PEOPTHEADER.DataDirectory[0].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[0].Size = 0;

PEOPTHEADER.DataDirectory[1].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[1].Size = 0;

PEOPTHEADER.DataDirectory[2].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[2].Size = 0;

PEOPTHEADER.DataDirectory[3].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[3].Size = 0;

PEOPTHEADER.DataDirectory[4].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[4].Size = 0;

PEOPTHEADER.DataDirectory[5].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[5].Size = 0;

PEOPTHEADER.DataDirectory[6].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[6].Size = 0;

PEOPTHEADER.DataDirectory[7].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[7].Size = 0;

PEOPTHEADER.DataDirectory[8].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[8].Size = 0;

PEOPTHEADER.DataDirectory[9].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[9].Size = 0;

PEOPTHEADER.DataDirectory[10].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[10].Size = 0;

PEOPTHEADER.DataDirectory[11].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[11].Size = 0;

PEOPTHEADER.DataDirectory[12].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[12].Size = 0;

PEOPTHEADER.DataDirectory[13].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[13].Size = 0;

PEOPTHEADER.DataDirectory[14].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[14].Size = 0;

PEOPTHEADER.DataDirectory[15].VirtualAddress = 0;
PEOPTHEADER.DataDirectory[15].Size = 0;




CODE.SectionName[0] = 46;
CODE.SectionName[1] = 116;
CODE.SectionName[2] = 101;
CODE.SectionName[3] = 120;
CODE.SectionName[4] = 116;
CODE.SectionName[5] = 0;
CODE.SectionName[6] = 0;
CODE.SectionName[7] = 0;

CODE.VirtualSize = sizeof(CODE_RAW);
CODE.VirtualAddress = PEOPTHEADER.BaseOfCode;
CODE.SizeOfRawData = sizeof(CODE_RAW);
CODE.PointerToRawData = 1024;
CODE.PointerToRelocations = 0;
CODE.PointerToLineNumbers = 0;
CODE.NumberOfRealocations = 0;
CODE.NumberOfLineNumbers = 0;
CODE.Characteristics = 1615855712;




PTRDOSHEADER = &DOSHEADER;
PTRPEHEADER = &PEHEADER;
PTRCOFFHEADER =  &COFFHEADER;
PTRPEOPTHEADER  = &PEOPTHEADER;
PCODE = &CODE;




pfile = fopen("ZETA.exe","wb");
if(pfile == NULL){
    printf("Error Creating File!\n");
}
else{
    fwrite(PTRDOSHEADER,sizeof(DOSHEADER),1,pfile);
    fwrite(DOS_MESSAGE,sizeof(char),sizeof(DOS_MESSAGE),pfile);
    fwrite(PTRPEHEADER,sizeof(PEHEADER),1,pfile);
    fwrite(PTRCOFFHEADER,sizeof(COFFHEADER),1,pfile);
    fwrite(PTRPEOPTHEADER,sizeof(PEOPTHEADER),1,pfile);
    fwrite(PCODE,sizeof(CODE),1,pfile);
    for(i=0; i < 1024 - (sizeof(DOSHEADER)+sizeof(DOS_MESSAGE)+sizeof(PEHEADER)+sizeof(COFFHEADER)+sizeof(PEOPTHEADER)+sizeof(CODE));i++){
         fwrite(&PAD,sizeof(char),1,pfile);
      }
     fwrite(CODE_RAW,sizeof(char),sizeof(CODE_RAW),pfile);

     fclose(pfile);
  }

  return 0;

}

1 个答案:

答案 0 :(得分:0)

你的处理器64位,你尝试运行32位DOS程序(没有64位DOS程序)?如果是,那么请参阅https://superuser.com/questions/961171/run-32bit-applications-on-windows-10-64-bit(32位模拟器获胜7/8/9/10)