dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] get a list of files from a specific windows directory

  1. #1
    Join Date
    Mar 2019
    Posts
    11

    [RESOLVED] get a list of files from a specific windows directory

    Hi,

    i want to have a file list from a specific Windows directory ("C:\Windows\System32\WDI"). I tried it with fs :: directory_iterator, findfirstFile and CFileFind. I always get ERROR_PATH_NOT_FOUND. I also tried with "run as admin" and elevation - nothing helps. Does anyone have an idea?

    Thanks in advance.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,526

    Re: get a list of files from a specific windows directory

    1. Have a look at this FAQ
    2. Show your code.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,877

    Re: get a list of files from a specific windows directory

    You don't have access to that folder - even with administrator access. To get access you'll need to invoke SeBackupPrivilege programmatically which requires admin access. See https://docs.microsoft.com/en-us/win...ore-privileges
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.5)

  4. #4
    Join Date
    Mar 2019
    Posts
    11

    [RESOLVED] Re: get a list of files from a specific windows directory

    Quote Originally Posted by 2kaud View Post
    You don't have access to that folder - even with administrator access. To get access you'll need to invoke SeBackupPrivilege programmatically which requires admin access. See https://docs.microsoft.com/en-us/win...ore-privileges
    Thanks for the fast help!

  5. #5
    Join Date
    Mar 2019
    Posts
    11

    Re: get a list of files from a specific windows directory

    Hi,

    now i have try it out - but with the same result or i have something missunderstood from this side (See https://docs.microsoft.com/en-us/win...ore-privileges)

    Code:
    #include "pch.h"
    #include <windows.h>
    #include <stdio.h>
    #include <string>
    #include <iostream>
    #include <filesystem>
    #include <fstream>
    namespace fs = std::filesystem;
    
    BOOL SetPrivilege(
    	HANDLE hToken,          // access token handle
    	LPCTSTR lpszPrivilege,  // name of privilege to enable/disable
    	BOOL bEnablePrivilege   // to enable or disable privilege
    )
    {
    	TOKEN_PRIVILEGES tp;
    	LUID luid;
    
    	if (!LookupPrivilegeValue(
    		NULL,            // lookup privilege on local system
    		lpszPrivilege,   // privilege to lookup 
    		&luid))        // receives LUID of privilege
    	{
    		printf("LookupPrivilegeValue error: %u\n", GetLastError());
    		return FALSE;
    	}
    
    	tp.PrivilegeCount = 1;
    	tp.Privileges[0].Luid = luid;
    	if (bEnablePrivilege)
    		tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    	else
    		tp.Privileges[0].Attributes = 0;
    
    	// Enable the privilege or disable all privileges.
    
    	if (!AdjustTokenPrivileges(
    		hToken,
    		FALSE,
    		&tp,
    		sizeof(TOKEN_PRIVILEGES),
    		(PTOKEN_PRIVILEGES)NULL,
    		(PDWORD)NULL))
    	{
    		printf("AdjustTokenPrivileges error: %u\n", GetLastError());
    		return FALSE;
    	}
    
    	if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
    
    	{
    		printf("The token does not have the specified privilege. \n");
    		return FALSE;
    	}
    
    	return TRUE;
    }
    int main()
    {
    	HRESULT hr = S_OK;
    	HANDLE hToken = NULL;
    
    	if (!OpenProcessToken(GetCurrentProcess(),
    		TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
    		&hToken))
    	{
    		printf("Failed OpenProcessToken\n");
    		return ERROR_FUNCTION_FAILED;
    	}
    
    	hr = SetPrivilege(hToken, SE_BACKUP_NAME, TRUE);
    
    	if (!SUCCEEDED(hr)) {
    		printf("\nFailed to modify privilege.\n");
    
    	}
    	else {
    		printf("\nSuccessfully modified privilege.\n");
    
    		try {
    //			std::string path = "C:\\Windows\\System32";                    // work
    			std::string path = "C:\\Windows\\System32\\WDI";
    			for (const auto & entry : fs::directory_iterator(path))
    				std::cout << entry.path() << std::endl;
    		}
    		catch (...) {
    			std::cout << "dir failed" << std::endl;
    		}
    
    	}
    	CloseHandle(hToken);
    	system("break");
    	return 0;
    
    }

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,877

    Re: get a list of files from a specific windows directory

    Code:
    !AdjustTokenPrivileges(
    		hToken,
    		FALSE,
    		&tp,
    		sizeof(TOKEN_PRIVILEGES),
    		(PTOKEN_PRIVILEGES)NULL,
    		(PDWORD)NULL))
    This should be:

    Code:
    !AdjustTokenPrivileges(
    		hToken,
    		FALSE,
    		&tp,
    		0,
    		(PTOKEN_PRIVILEGES)NULL,
    		(PDWORD)NULL))
    PS. I've tried it on a Windows 7 system and I don't get an error - but all I do get is:

    Code:
    perftrack
    I've tried it with directory_iterator and Windows Find_first_file() et al. Hmmm.......
    Last edited by 2kaud; April 29th, 2019 at 05:51 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.5)

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,877

    Re: get a list of files from a specific windows directory

    OK. The issue is file system redirection when running Windows 64. Before you enumerate the folder, you have to disable wow64 re-direction. See https://msdn.microsoft.com/en-us/44b...8-8278a7efe9b7 Consider:

    Code:
    BOOL SetPrivilege(
    	HANDLE hToken,          // access token handle
    	LPCTSTR lpszPrivilege,  // name of privilege to enable/disable
    	BOOL bEnablePrivilege   // to enable or disable privilege
    )
    {
    	TOKEN_PRIVILEGES tp;
    	LUID luid;
    
    	if (!LookupPrivilegeValue(
    		NULL,            // lookup privilege on local system
    		lpszPrivilege,   // privilege to lookup
    		&luid))        // receives LUID of privilege
    	{
    		printf("LookupPrivilegeValue error: %u\n", GetLastError());
    		return FALSE;
    	}
    
    	tp.PrivilegeCount = 1;
    	tp.Privileges[0].Luid = luid;
    	if (bEnablePrivilege)
    		tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    	else
    		tp.Privileges[0].Attributes = 0;
    
    	// Enable the privilege or disable all privileges.
    
    	if (!AdjustTokenPrivileges(
    		hToken,
    		FALSE,
    		&tp,
    		0, //sizeof(TOKEN_PRIVILEGES),
    		(PTOKEN_PRIVILEGES)NULL,
    		(PDWORD)NULL))
    	{
    		printf("AdjustTokenPrivileges error: %u\n", GetLastError());
    		return FALSE;
    	}
    
    	if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
    
    	{
    		printf("The token does not have the specified privilege. \n");
    		return FALSE;
    	}
    
    	return TRUE;
    }
    
    int main()
    {
    	HANDLE hToken = NULL;
    	PVOID OldValue = NULL;
    
    	Wow64DisableWow64FsRedirection(&OldValue);
    
    	if (!OpenProcessToken(GetCurrentProcess(),
    		TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
    		&hToken))
    	{
    		printf("Failed OpenProcessToken\n");
    		return ERROR_FUNCTION_FAILED;
    	}
    
    	const HRESULT hr = SetPrivilege(hToken, SE_BACKUP_NAME, TRUE);
    
    	if (!SUCCEEDED(hr)) {
    		printf("\nFailed to modify privilege.\n");
    
    	}
    	else {
    		printf("\nSuccessfully modified privilege.\n");
    
    		try {
    			//std::string path = "C:\\Windows\\System32";                    // work
    			std::string path = "C:\\Windows\\System32\\WDI";
    			for (const auto& entry : fs::directory_iterator(path))
    				std::cout << entry.path() << std::endl;
    		}
    		catch (...) {
    			std::cout << "dir failed" << std::endl;
    		}
    
    	}
    
    	CloseHandle(hToken);
    	Wow64RevertWow64FsRedirection(OldValue);
    
    	return 0;
    }
    Which on my computer now gives:

    Code:
    Successfully modified privilege.
    "C:\\Windows\\System32\\WDI\\BootPerformanceDiagnostics_SystemData.bin"
    "C:\\Windows\\System32\\WDI\\ERCQueuedResolutions.dat"
    "C:\\Windows\\System32\\WDI\\LogFiles"
    "C:\\Windows\\System32\\WDI\\perftrack"
    "C:\\Windows\\System32\\WDI\\ShutdownPerformanceDiagnostics_SystemData.bin"
    "C:\\Windows\\System32\\WDI\\SuspendPerformanceDiagnostics_SystemData_S3.bin"
    "C:\\Windows\\System32\\WDI\\SuspendPerformanceDiagnostics_SystemData_S4.bin"
    "C:\\Windows\\System32\\WDI\\{284ddb2f-beea-4c9d-91e8-e3670ed91517}.bin"
    "C:\\Windows\\System32\\WDI\\{533a67eb-9fb5-473d-b884-958cf4b9c4a3}"
    "C:\\Windows\\System32\\WDI\\{67144949-5132-4859-8036-a737b43825d8}"
    "C:\\Windows\\System32\\WDI\\{72dbb5ac-6a91-46e6-885b-d429828bea2e}.bin"
    "C:\\Windows\\System32\\WDI\\{86432a0b-3c7d-4ddf-a89c-172faa90485d}"
    "C:\\Windows\\System32\\WDI\\{88d4896f-f553-446a-9c75-9dec124ff8b7}.bin"
    "C:\\Windows\\System32\\WDI\\{95c162b7-5b71-44f8-82e4-abfd3108f40f}.bin"
    "C:\\Windows\\System32\\WDI\\{ffc42108-4920-4acf-a4fc-8abdcc68ada4}"
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.5)

  8. #8
    Join Date
    Mar 2019
    Posts
    11

    Re: get a list of files from a specific windows directory

    Quote Originally Posted by 2kaud View Post
    OK. The issue is file system redirection when running Windows 64. Before you enumerate the folder, you have to disable wow64 re-direction. See https://msdn.microsoft.com/en-us/44b...8-8278a7efe9b7 Consider:
    .
    .
    .
    Great that was the last piece of the puzzle - thank you for the great help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)