Nikolai Nikolov
Nikolai Nikolov

Reputation: 458

Check if file has executable rights windows c++

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.

Upvotes: 1

Views: 877

Answers (1)

Chris Becke
Chris Becke

Reputation: 36016

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();
    ....

Upvotes: 2

Related Questions