🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.ms-windows.programmer.nt.kernel-mode
Article #6

Re: Renaming a file

#6
From: jjaimon
Date: Tue, 28 Apr 1998 00:00
78 lines
3424 bytes

> > Does anyone know how to rename a file from NT Driver?
> > I can't find any functions to do that in NT DDK help.
> > Please give me some hints.
> >
>
> The function is IoCallDriver()
> The rename code may look like this:
>
> ==============================================================
> PFILE_RENAME_INFORMATION Rename = ... ;
> (initialize FILE_RENAME_INFORMATION with target file name)
>
> Irp = IoAllocateIrp( DeviceObject->StackSize, FALSE );
> if ( ! Irp ) return STATUS_INSUFFICIENT_RESOURCES;
>
> Irp->Flags |= IRP_BUFFERED_IO;
> Irp->RequestorMode = KernelMode;
> Irp->UserIosb = &IoStatus;
> Irp->UserEvent = &Event;
> Irp->Tail.Overlay.OriginalFileObject = FileObject;
> Irp->Tail.Overlay.Thread = PsGetCurrentThread();
> Irp->AssociatedIrp.SystemBuffer = (PVOID)&Rename;
>
> IrpSp = IoGetNextIrpStackLocation( Irp );
> IrpSp->MajorFunction = IRP_MJ_SET_INFORMATION;
> IrpSp->FileObject = FileObject;
> IrpSp->DeviceObject = DeviceObject;
> IrpSp->Parameters.SetFile.Length = Length; // length of Rename
> IrpSp->Parameters.SetFile.FileInformationClass = FileRenameInformation;
> IrpSp->Parameters.SetFile.FileObject = FileObject;
> IrpSp->Parameters.SetFile.ReplaceIfExists = Rename->ReplaceIfExists;
>
> Status = IoCallDriver( DeviceObject, Irp );
> if (Status == STATUS_PENDING)
>     KeWaitForSingleObject( &Event, Executive, KernelMode,
>                            FALSE, (PLARGE_INTEGER)NULL);
>
> if (!NT_SUCCESS(Status)) IoStatus.Status = Status;
> return IoStatus.Status;
> ==============================================================
>
> Good luck,
> -Slava
>
> P.S FILE_RENAME_INFORMATION defined in one of Microsoft header
>     files as follows:
>
> typedef struct _FILE_RENAME_INFORMATION {
>     BOOLEAN ReplaceIfExists;
>     HANDLE RootDirectory;
>     ULONG FileNameLength;
>     WCHAR FileName[1];
> } FILE_RENAME_INFORMATION, *PFILE_RENAME_INFORMATION;
>

Hi,
  using an IRP would be a difficult process.  Instead you can use
NtSetInformationFile(..) routine.  The prototype for the function is
NTSYSAPI
NTSTATUS
NTAPI
NtSetInformationFile(
    IN HANDLE FileHandle,
    OUT PIO_STATUS_BLOCK IoStatusBlock,
    IN PVOID FileInformation,
    IN ULONG Length,
    IN FILE_INFORMATION_CLASS FileInformationClass
    );

FileInformation field will have the structure type FILE_RENAME_INFORMATION and
FileInformationClass will have enumerated type FileRenameInformation (ntddk.h)

Jaimon

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
Technical Details
Message-ID: <6i4b8h$iil$1@nnrp1.dejanews.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <353FD7C0.28FE2EA0@vet.com.au> <3542A43B.D77B4C4@ftp.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 113dfb,839b322f296bb490
X-Google-Attributes: gid113dfb,public
X-Google-Thread: 10c4a9,839b322f296bb490
X-Google-Attributes: gid10c4a9,public
X-Google-Thread: 10e551,839b322f296bb490
X-Google-Attributes: gid10e551,public
From: jjaimon@hotmail.com
Subject: Re: Renaming a file
Date: 1998/04/28
Message-ID: <6i4b8h$iil$1@nnrp1.dejanews.com>
X-Deja-AN: 348374665
References: <353FD7C0.28FE2EA0@vet.com.au> <3542A43B.D77B4C4@ftp.com>
X-Http-User-Agent: Mozilla/4.04 [en] (WinNT; I)
Organization: Deja News - The Leader in Internet Discussion
X-Article-Creation-Date: Tue Apr 28 10:32:49 1998 GMT
Newsgroups: comp.os.ms-windows.programmer.nt.kernel-mode,cis.winnt.drivers,comp.os.ms-windows.programmer.win32