1

I have a file name and I want to check whether it can be executed or not on windows through c++. I found _access and _access_s, but they only check for read/write.

My problem is that when I download a bat file for example, the windows blocks it as a security measure. When I run my program and try to execute it, windows blocks my program and asks user if he wants to continue anyway, because the file is risky. I want to avoid that by checking the file rights before executing it.

Nikolai Nikolov
  • 428
  • 3
  • 13
  • Possible duplicate of [C++ How to retrieve a file permission and ownership via win32 api](http://stackoverflow.com/questions/11755422/c-how-to-retrieve-a-file-permission-and-ownership-via-win32-api) – user4581301 Apr 13 '17 at 14:33
  • Mind you, this could be antivirus software at work. – user4581301 Apr 13 '17 at 14:35

1 Answers1

2

The windows filesystem, NTFS, does not support an executable attribute in the way you might expect if you have used a Unix based OS.

What you are seeing here is the shell reacting to an extra stream that was added to the file. And streams are a feature of NTFS.

Microsoft has some sample code showing how to access streams in a file:

In the case of files downloaded from the internet, Microsofts browsers (IE and Edge) add a stream called "Zone.Identifier", which ShellExecute and related APIs check for when asked to execute a file to present the user with a security prompt.

To cleanse the file so that the security prompt does not appear it is necessary to erase the stream.

BOOL didDeleteZoneIdentifier = DeleteFile(TEXT("Path To Batch File.bat:Zone.Idenfier"));
if(!didDeleteZoneIdentifier){
    int errorCode = GetLastError();
    ....
Chris Becke
  • 34,244
  • 12
  • 79
  • 148