How to find if a file is an exe?

后端 未结 6 1782
粉色の甜心
粉色の甜心 2020-12-05 14:51

How can I be sure that a file passed to my program is a valid exe file ?

actually my program takes a file as input and runs it, but user can input any file so I have

6条回答
  •  甜味超标
    2020-12-05 15:42

    You can check if a file is a valid PE format file using the PE Format DLL.

    PE files can contain more than just executable code. The can contain resources as well as code, or just resources and no code. PE files can also be native or managed, or native but linked to managed code etc. Depending on what you want to do, being able to check for these things would be useful.

    PE_EXE3 pef;
    int     ok = FALSE;
    
    if (pef.openFile(fileNameAndPath))
    {
        if (pef.IsValid())
        {
            ok = pef.GetHasExecutableCode();
        }
        pef.closeFile();
    }
    

    You may also find the following functions useful:

    pef.GetIsOnlyResource()
    pef.GetHasExecutableCode()
    pef.GetIsPureDotNetModule()
    

提交回复
热议问题