-
-
Save caiorss/bad57134dd6e95e394a04b8b727a825f to your computer and use it in GitHub Desktop.
Winapi/win32 Files attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <windows.h> | |
#include <string> | |
#define disp(expr) std::cerr << std::boolalpha << __FILE__ << ":" << __LINE__ << ":" \ | |
<< " ; " << #expr << " = " << (expr) << std::endl | |
void showFileAttributes(std::string path) | |
{ | |
DWORD attr = GetFileAttributesA(path.c_str()); | |
disp(attr); | |
bool isInvalid = attr == INVALID_FILE_ATTRIBUTES; | |
bool exists = !isInvalid; | |
bool isDirectory = exists && static_cast<bool>(attr & FILE_ATTRIBUTE_DIRECTORY); | |
bool isFile = exists && !static_cast<bool>(attr & FILE_ATTRIBUTE_DIRECTORY); | |
std::cout << "File attributes for path = " << path << std::endl; | |
std::cout << std::boolalpha | |
<< "Is invalid file attribute = " << isInvalid << std::endl | |
<< "File exists = " << exists << std::endl | |
<< "Is directory = " << isDirectory << std::endl | |
<< "Is file = " << isFile << std::endl; | |
std::cout << "--------------------------------" << std::endl | |
<< std::endl; | |
} | |
bool fileExists(std::string path) | |
{ | |
DWORD attr = GetFileAttributesA(path.c_str()); | |
return !(attr == INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY); | |
} | |
bool dirExists(std::string path) | |
{ | |
DWORD attr = GetFileAttributesA(path.c_str()); | |
return !(attr == INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY); | |
} | |
// Get Path to current program / Application | |
std::string pathToThisProgram() | |
{ | |
std::string result(MAX_PATH, 0); | |
int n = GetModuleFileNameA(NULL, &result[0], MAX_PATH); | |
result.resize(n); | |
return result; | |
} | |
std::string getEnvironment(std::string varname) | |
{ | |
static int size = 32767; | |
std::string result(size, 0); | |
DWORD n = GetEnvironmentVariableA(varname.c_str(), &result[0], size); | |
result.resize(n); | |
return result; | |
} | |
int main() | |
{ | |
std::cout << "Path to this program = " | |
<< pathToThisProgram() << std::endl; | |
std::cout << "Evironment variable => %USERPROFILE% = " | |
<< getEnvironment("USERPROFILE") << std::endl; | |
std::cout << "Evironment variable => %SYSTEMROOT% = " | |
<< getEnvironment("SYSTEMROOT") << std::endl; | |
std::cout << "Evironment variable => %WRONGVAR% = " | |
<< getEnvironment("WRONGVAR") << std::endl; | |
std::string thisprog = pathToThisProgram(); | |
std::string dest = getEnvironment("USERPROFILE") + "\\Desktop\\app.exe"; | |
// Copy file to Desktop | |
if (fileExists(dest)) | |
{ | |
std::cout << " ==> File " << dest << " exists - removing it ... " << std::endl; | |
DeleteFileA(dest.c_str()); | |
} | |
bool result = CopyFileA(thisprog.c_str(), dest.c_str(), TRUE); | |
std::cout << "Copy successful? = " << std::boolalpha << result << std::endl; | |
//Delete this executable | |
bool result2 = DeleteFileA(thisprog.c_str()); | |
std::cout << "Delete successful? = " << std::boolalpha << result2 << std::endl; | |
std::cout << "-------------------------------" << std::endl; | |
showFileAttributes("C:\\Windows\\System32"); | |
showFileAttributes("C:/Windows/System32"); | |
showFileAttributes("C:\\Windows\\System32\\cmd.exe"); | |
showFileAttributes("C:\\Windows\\System32DONOT_EXISTS"); | |
disp(fileExists("C:\\Windows\\System32")); | |
disp(fileExists("C:/Windows/System32")); | |
// Windows file system is case insensitive! | |
disp(fileExists("C:\\Windows\\System32\\cmd.exe")); | |
disp(fileExists("C:\\Windows\\System32\\CMD.EXE")); | |
disp(fileExists("C:\\Windows\\System32\\kernel32.dll")); | |
disp(fileExists("C:\\Windows\\System32DONOT_EXISTS")); | |
std::cout << std::endl; | |
disp(dirExists("C:\\Windows\\System32")); | |
disp(dirExists("C:/Windows/System32")); | |
disp(dirExists("C:\\Windows\\System32\\cmd.exe")); | |
disp(dirExists("C:\\Windows\\System32\\CMD.EXE")); | |
disp(dirExists("C:\\Windows\\System32\\kernel32.dll")); | |
disp(dirExists("C:\\Windows\\System32DONOT_EXISTS")); | |
disp(dirExists("C:\\$Recycle.bin")); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REM Build script for compiling with MSVC (aka Visual C++, cl.exe) on Windows | |
@echo OFF | |
REM Automate CMake Compilation on Windows | |
cmake -H. -B_build -G "Visual Studio 15 2017 Win64" | |
cmake --build _build --target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.9) | |
project(WindowsExploration) | |
#========== Global Configurations =============# | |
#----------------------------------------------# | |
set(CMAKE_CXX_STANDARD 17) | |
set(CMAKE_VERBOSE_MAKEFILE ON) | |
#========== Targets Configurations ============# | |
add_executable(attrib attrib.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GNU Make file script for easier compilation from | |
# command line | |
debug: | |
cmake -B_build -H. -DCMAKE_BUILD_TYPE=Debug | |
cmake --build _build --target | |
release: | |
cmake -B_build -H. -DCMAKE_BUILD_TYPE=Release | |
cmake --build _build --target | |
clean: | |
rm -rfv ./_build | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment