Click here to Skip to main content
15,892,768 members
Articles / Programming Languages / C++

Eliminating Explorer's delay when deleting an in-use file

Rate me:
Please Sign up or sign in to vote.
4.99/5 (278 votes)
28 Sep 200519 min read 326.9K   1.9K   236  
How to track down and patch an annoyance in Windows Explorer's code.
#include <windows.h>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(int argc, wchar_t* argv[])
{
	// Build the fully-qualified pathname to the driver's image
	vector<wchar_t> buffer(MAX_PATH);
	GetCurrentDirectory(static_cast<int>(buffer.capacity()), &buffer[0]);
	
	wstring path;
	path.append(L"\\\\?\\");
	path.append(&buffer[0]);
	path.append(L"\\NoDeleteDelay.sys");

	// Make sure the file exists
	if (GetFileAttributes(path.c_str()) == INVALID_FILE_ATTRIBUTES)
	{
		cout << "File not found: " << path.c_str() << endl;
		return 1;
	}

	// Open the Service Control Manager
	SC_HANDLE hSc = OpenSCManager(NULL, NULL, GENERIC_READ | GENERIC_WRITE);
	if (hSc == NULL)
	{
		cout << "Error calling OpenSCManager: " << GetLastError() << endl;
		return 1;
	}

	// Create the service
	SC_HANDLE hService = CreateService(
		hSc,
		L"NoDeleteDelay",
		L"NoDeleteDelay",
		GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,
		SERVICE_KERNEL_DRIVER,
		SERVICE_AUTO_START,
		SERVICE_ERROR_NORMAL,
		path.c_str(),
		NULL,
		NULL,
		NULL,
		NULL,
		L""
		);

	if (hService == NULL)
	{
		cout << "Error calling CreateService: " << GetLastError() << endl;
		return 1;
	}

	cout << "Service created." << endl;

	// Start the service
	if (!StartService(hService, 0, NULL))
	{
		cout << "Error calling StartService: " << GetLastError() << endl;
		return 1;
	}

	cout << "Service started." << endl;

	return 0;
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions