You should not allocate the DEVICE_OBJECT or FILE_OBJECT. This is why
you pass a pointer to a pointer to these functions. They return to you
a valid pointer value. You are then freeing that value, which you are
not allowed to free since you are freeing an NT object (which also means
you are leaking the 2 allocations since you lose them on successful
calls to Ob.
D
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Gleb Suhatski
Sent: Thursday, December 15, 2005 9:33 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Retrieving full pathname from handle
Unfortunately I cannot use this method in this particular driver.
Anyhow, I checked OSR lists archive and Google and found a way to do it.
I wrote a function that receives a handle and returns a drive letter,
ex. C:
but it crashes.
(By the way, I’m aware that this is a legacy driver and that it should
be a
file system driver. nevertheless, I have to sort this
problem out since it’s part of my current project.)
Does anyone see what’s wrong with this function?
Thanks in advance.
NTSTATUS FileDirectoryHandleToUnicodeDosDriveName(IN HANDLE Handle,
OUT PUNICODE_STRING pUnicodeName)
{
IO_STATUS_BLOCK ioStatusBlock;
POBJECT_NAME_INFORMATION pobjObjectNameInfo;
NTSTATUS status;
ULONG ulLength = 0;
PFILE_OBJECT pFileObject;
PDEVICE_OBJECT pDeviceObject;
IndexerDebugPrint(DBG_INIT, DBG_TRACE, FUNCTION"++");
//
// Allocating memory for pobjObjectNameInfo
//
pobjObjectNameInfo = (POBJECT_NAME_INFORMATION)ExAllocatePoolWithTag(
NonPagedPool,
sizeof(OBJECT_NAME_INFORMATION) + (MAX_PATH*2),
INDEXER_POOL_TAG
);
if(pobjObjectNameInfo == NULL)
{
status = STATUS_INSUFFICIENT_RESOURCES;
IndexerDebugPrint(DBG_INIT, DBG_ERR, FUNCTION": Failed to allocate
memory for pobjObjectNameInfo");
return status;
}
//
// Allocating memory for pFileObject
//
pFileObject = (PFILE_OBJECT)ExAllocatePoolWithTag(
NonPagedPool,
sizeof(FILE_OBJECT),
INDEXER_POOL_TAG
);
if(pFileObject == NULL)
{
status = STATUS_INSUFFICIENT_RESOURCES;
ExFreePoolWithTag(pobjObjectNameInfo, INDEXER_POOL_TAG);
IndexerDebugPrint(DBG_INIT, DBG_ERR, FUNCTION": Failed to allocate
memory for pFileObject");
return status;
}
//
// Allocating memory for pDeviceObject
//
pDeviceObject = (PDEVICE_OBJECT)ExAllocatePoolWithTag(
NonPagedPool,
sizeof(DEVICE_OBJECT),
INDEXER_POOL_TAG
);
if(pDeviceObject == NULL)
{
status = STATUS_INSUFFICIENT_RESOURCES;
ExFreePoolWithTag(pobjObjectNameInfo, INDEXER_POOL_TAG);
ExFreePoolWithTag(pFileObject, INDEXER_POOL_TAG);
IndexerDebugPrint(DBG_INIT, DBG_ERR, FUNCTION": Failed to allocate
memory for pDeviceObject");
return status;
}
//
// Getting file object
//
status = ObReferenceObjectByHandle(Handle,
GENERIC_READ,
NULL,
KernelMode,
&pFileObject,
NULL);
if(status != STATUS_SUCCESS)
{
IndexerDebugPrint(DBG_INIT, DBG_ERR, FUNCTION"–. STATUS %x",
status);
ExFreePoolWithTag(pobjObjectNameInfo, INDEXER_POOL_TAG);
ExFreePoolWithTag(pFileObject, INDEXER_POOL_TAG);
ExFreePoolWithTag(pDeviceObject, INDEXER_POOL_TAG);
return status;
}
//
// getting file object name
//
status = ObQueryNameString(pFileObject,
pobjObjectNameInfo,
sizeof(OBJECT_NAME_INFORMATION) + (MAX_PATH*2),
&ulLength);
if(status != STATUS_SUCCESS)
{
IndexerDebugPrint(DBG_INIT, DBG_ERR, FUNCTION"–. STATUS %x",
status);
ObDereferenceObject(pFileObject);
ExFreePoolWithTag(pobjObjectNameInfo, INDEXER_POOL_TAG);
ExFreePoolWithTag(pFileObject, INDEXER_POOL_TAG);
ExFreePoolWithTag(pDeviceObject, INDEXER_POOL_TAG);
return status;
}
//
// Getting device object pointer
//
IoGetDeviceObjectPointer(/*&pFileObject->FileName*/&pobjObjectNameInfo->
Name, FILE_READ_DATA, &pFileObject, &pDeviceObject);
if(status != STATUS_SUCCESS) { IndexerDebugPrint(DBG_INIT, DBG_ERR,
FUNCTION"–. STATUS %x", status); ObDereferenceObject(pFileObject);
ExFreePoolWithTag(pobjObjectNameInfo, INDEXER_POOL_TAG);
ExFreePoolWithTag(pFileObject, INDEXER_POOL_TAG);
ExFreePoolWithTag(pDeviceObject, INDEXER_POOL_TAG); return status; } //
// converting to C:\ format //
IoVolumeDeviceToDosName(pFileObject->DeviceObject, pUnicodeName);
// // must dereference File Object // ObDereferenceObject(pFileObject);
// // cleaning up // ExFreePoolWithTag(pobjObjectNameInfo,
INDEXER_POOL_TAG); ExFreePoolWithTag(pFileObject, INDEXER_POOL_TAG);
ExFreePoolWithTag(pDeviceObject, INDEXER_POOL_TAG); status =
STATUS_SUCCESS; return status;}“Roddy, Mark”
wrote in message news:xxxxx@ntdev…So your fast index search utility
has to build a map from volume guid tovolume mountpoints.-----Original
Message-----From:
xxxxx@lists.osr.com[mailto:xxxxx@lists.osr.com
] On Behalf Of Gleb SuhatskiSent: Thursday, December 15, 2005 6:00 AMTo:
Windows System Software Devs Interest ListSubject: Re:[ntdev] Retrieving
full pathname from handleI am writing a driver that makes a list of file
creation/deletion eventsfora fast index search utility. I have to pass
the string in a DOS filenameformat to the application.“Maxim S.
Shatskih” wrote in messagenews:xxxxx@ntdev…>
Design your product in a way so it will only use Volume GUIDs>
internally> and not the drive letters. The drive letters will appear in
the UIonly.>> Maxim Shatskih, Windows DDK MVP> StorageCraft Corporation>
xxxxx@storagecraft.com> http://www.storagecraft.com>> ----- Original
Message -----> From: > Newsgroups: ntdev> To:
“Windows System Software Devs Interest List” >
Sent: Monday, December 12, 2005 1:53 AM> Subject: [ntdev] Retrieving
full pathname from handle>>>> Hi.>>>> I am trying to populate the full
pathname of a file or directory by>> supplying only the file/dir handle
to my function.>> It can currently populate only the relative path
without the driveletter>> name (for example C:).>>>> I would like the
output format to be a Unicode string with a syntaxthat>> is>> somewhat
like “C:\SomeDir\SomeOtherDir\file.ext”.>> I’ve included my function’s
source in this message. Any help will be>> appreciated.>>>> Thanks in
advance,>>>> Gleb>>>>
//------------------------------------------------------------------->>>
> NTSTATUS FileHandleToUNICODE_STRING(IN HANDLE FileHandle,>> OUT
PUNICODE_STRING FileName)>> {>> PIO_STATUS_BLOCK pioStatusBlock;>>
PFILE_NAME_INFORMATION fniFileInfo;>> NTSTATUS status;>>>> //>> //
Allocating memory for pioStatusBlock>> //>> pioStatusBlock =
(PIO_STATUS_BLOCK)ExAllocatePoolWithTag(>> NonPagedPool,>>
sizeof(IO_STATUS_BLOCK),>> INDEXER_POOL_TAG>> );>> if(pioStatusBlock ==
NULL)>> {>> status = STATUS_INSUFFICIENT_RESOURCES;>>>>
IndexerDebugPrint(DBG_INIT, DBG_ERR, FUNCTION": Failed toallocate>>
memory for pioStatusBlock");>>>> return status;>> }>>>> //>> //
Allocating memory for fniFileInfo>> //>> fniFileInfo =
(PFILE_NAME_INFORMATION)ExAllocatePoolWithTag(>> NonPagedPool,>>
sizeof(FILE_NAME_INFORMATION) + (MAX_PATH-1),>> INDEXER_POOL_TAG>> );>>
if(fniFileInfo == NULL)>> {>> status =
STATUS_INSUFFICIENT_RESOURCES;>>>> ExFreePoolWithTag(pioStatusBlock,
INDEXER_POOL_TAG);>>>> IndexerDebugPrint(DBG_INIT, DBG_ERR,
FUNCTION": Failed toallocate>> memory for fniFileInfo");>>>> return
status;>> }>>>> //>> // Populating file name>> //>> status =
ZwQueryInformationFile(FileHandle,>> pioStatusBlock,>> fniFileInfo,>>
(ULONG)sizeof(FILE_NAME_INFORMATION) + (MAX_PATH-1),>>
FileNameInformation);>> if(status != STATUS_SUCCESS)>> {>>
ExFreePoolWithTag(pioStatusBlock, INDEXER_POOL_TAG);>>
ExFreePoolWithTag(fniFileInfo, INDEXER_POOL_TAG);>>>>
IndexerDebugPrint(DBG_INIT, DBG_ERR, FUNCTION"–. STATUS %x",>>
status);>>>> return status;>> }>>>> FileName->MaximumLength =
fniFileInfo->FileNameLength;>> FileName->Length =
fniFileInfo->FileNameLength;>> FileName->Buffer =
fniFileInfo->FileName;>> RtlCopyMemory(FileName->Buffer,>>
&fniFileInfo->FileName,>> fniFileInfo->FileNameLength);>>>>>>
ExFreePoolWithTag(pioStatusBlock, INDEXER_POOL_TAG);>>
ExFreePoolWithTag(fniFileInfo, INDEXER_POOL_TAG);>>>> status =
STATUS_SUCCESS;>>>> return status;>> }>>>> —>> Questions? First check
the Kernel Driver FAQ at>
http://www.osronline.com/article.cfm?id=256>>>> You are currently
subscribed to ntdev as: xxxxx@storagecraft.com>> To unsubscribe send a
blank email xxxxx@lists.osr.com>>>—Questions? First
check the Kernel Driver FAQ
athttp://www.osronline.com/article.cfm?id=256You are currently
subscribed to ntdev as: xxxxx@stratus.comTo unsubscribe send a
blank email to xxxxx@lists.osr.com
—
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as: xxxxx@microsoft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com