驅動中確認文件是否存在的一個方法

http://www.osronline.com/showThread.cfm?link=43778

//
// Check if a local file or dir exists (via open)
//
BOOL CheckElementExistsViaOpen( PUNICODE_STRING puPath )
{
	DBGASSERTIRQLEQUAL(PASSIVE_LEVEL);

	IO_STATUS_BLOCK IoStatus;

	NTSTATUS status = GetExistanceStatus( puPath, &IoStatus );

	if( NT_SUCCESS(status) )
	{
		return TRUE;
	}
	else if( (status == STATUS_OBJECT_NAME_NOT_FOUND) ||
			 (IoStatus.Information == FILE_DOES_NOT_EXIST) ||
			 (status == STATUS_OBJECT_PATH_NOT_FOUND) ||
			 (status == STATUS_OBJECT_NAME_INVALID) ||
			 (status == STATUS_OBJECT_PATH_INVALID) )
	{
		return FALSE;
	}

	return TRUE;
}

//
// Get element's existance status
//
NTSTATUS GetExistanceStatus( PUNICODE_STRING puPath, PIO_STATUS_BLOCK
pIoStatus )
{
	DBGASSERTIRQLEQUAL(PASSIVE_LEVEL);

	NTSTATUS status;
	OBJECT_ATTRIBUTES aFileAttrib;
	FILE_NETWORK_OPEN_INFORMATION aInfo;

#if (WINVER>=0x500)

	InitializeObjectAttributes( &aFileAttrib, puPath,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL );

#else

	InitializeObjectAttributes( &aFileAttrib, puPath,
OBJ_CASE_INSENSITIVE, NULL, NULL );

#endif

	//
	// Use FastOpen (if possible). If not that call will roll
	// create IRP.
	//

	status = IoFastQueryNetworkAttributes( &aFileAttrib, SYNCHRONIZE, 0,
pIoStatus, &aInfo);

	if( NT_SUCCESS(status) )
	{
		status = pIoStatus -> Status; 
	}

	return status;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章