Click here to Skip to main content
15,885,998 members
Articles / Programming Languages / C++

Listing Used Files

Rate me:
Please Sign up or sign in to vote.
4.81/5 (95 votes)
29 Sep 2010CPOL11 min read 630.1K   23.2K   221  
A ShellExtension that lists all the used files in a folder.
// Utils.cpp: implementation of the HelperFunctions class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"
#include "Utils.h"
#include <Winsvc.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

const LPCTSTR DRV_DOS_NAME = _T("\\\\.\\ListFileDrv");
const LPCTSTR DRV_NAME = _T("ListOpenedFileDrv");
const LPCTSTR DRV_FILE_NAME = _T("ListOpenedFileDrv.sys");

// Function resolves the fosedevice name to drive name
BOOL GetDrive( LPCTSTR pszDosName, CString& csDrive )
{
	TCHAR tcDeviceName[50];
	TCHAR tcDrive[3] = _T("A:");

	// Iterating through the drive letters
	for ( TCHAR actDrive = _T('A'); actDrive <= _T('Z'); actDrive++ )
	{
		tcDrive[0] = actDrive;
		// Query the device for the drive letter
		if ( QueryDosDevice( tcDrive, tcDeviceName, 50 ) != 0 )
		{
            // Is this the drive letter we are looking for?
			if ( _tcsnicmp( tcDeviceName, pszDosName, _tcslen( tcDeviceName ) ) == 0 )
			{
				csDrive = tcDrive;				
				return TRUE;
			}
		}
	}
	return FALSE;
}

// This Function extracts the Driver in its resource to a file and then install,start it
HANDLE ExtractAndInstallDrv()
{
	SC_HANDLE hSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
	SC_HANDLE hService = OpenService( hSCManager , DRV_NAME, SERVICE_ALL_ACCESS);
	CString csPath;
	if( 0 == hService )
	{
		//Service not installed. So install the service.

		// First extract the resource
		HINSTANCE hModule= AfxGetInstanceHandle();
		HRSRC hRsrc = FindResource(hModule, MAKEINTRESOURCE(IDR_XPDRIVER),_T("BINARY"));
		HGLOBAL hDrvRsrc = LoadResource(hModule, hRsrc);
		DWORD dwDriverSize = SizeofResource(hModule, hRsrc);
		LPVOID lpvDriver = LockResource(hDrvRsrc);
		CFile File;
        if( !File.Open( DRV_FILE_NAME, CFile::modeCreate|CFile::modeWrite ))
        {
            return 0;
        }
        File.Write( lpvDriver, dwDriverSize );
		csPath = File.GetFilePath();
		File.Close();
		
		hService = CreateService( hSCManager, DRV_NAME, DRV_NAME, SERVICE_ALL_ACCESS,
								  SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
								  csPath, NULL, NULL, NULL, NULL, NULL );
		
		if( 0 == hService )
		{
			CloseServiceHandle(hSCManager);
			return 0;
		}
	}

	if( !StartService( hService, 0, NULL ))
	{
		if( GetLastError() != ERROR_SERVICE_ALREADY_RUNNING )
		{
			DeleteService(hService);
			CloseServiceHandle(hService);
			CloseServiceHandle(hSCManager);			
			DeleteFile( csPath );
			return 0;
		}
		
	}
	// Delete the temp file...
	DeleteFile( csPath );
	HANDLE hFile = CreateFile( DRV_DOS_NAME, GENERIC_READ|GENERIC_WRITE, 
								FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
								OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0 );
	if( INVALID_HANDLE_VALUE == hFile )
	{
		hFile = 0;
		SERVICE_STATUS  stSrvStatus = {0};
		ControlService( hService, SERVICE_CONTROL_STOP, &stSrvStatus );
	}	
	CloseServiceHandle(hService);
	CloseServiceHandle(hSCManager);
	return hFile;
}

// Function stops the service and then deletes it.
BOOL StopAndUninstallDrv( HANDLE hDrvHandle )
{
	CloseHandle( hDrvHandle );
	SC_HANDLE hSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
	SC_HANDLE hService = OpenService( hSCManager , DRV_NAME, SERVICE_ALL_ACCESS );
	SERVICE_STATUS  stSrvStatus = {0};
	ControlService( hService, SERVICE_CONTROL_STOP, &stSrvStatus );
	BOOL bDeleted = DeleteService(hService);
	CloseServiceHandle(hService);
	CloseServiceHandle(hSCManager);	
	return bDeleted;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions