Usin ZwReadFile in winxp

B

breeze.joy

Hi all,
I'm writing a driver code in winxp.Here i want to access the file
using ZwCreateFile,ZwReadFile,etc.I'm able to create a file using
ZwCreateFile.But i'm not able to use ZwReadFile.When i call this
function,my system is always getting hanged(phycial memory
dump).Herewith i'v attached my code.Please go through this code and
feedback to me.Help me with suggestions.
Thanks in advance.

/////////////////my code////////////////////////

//global variables

HANDLE hFile1;

UNICODE_STRING us_ip;

PUCHAR pfile_buff;

ULONG LengthOfFile;

FILE_STANDARD_INFORMATION StandardInfo;

OBJECT_ATTRIBUTES oa;

IO_STATUS_BLOCK iosb;

NTSTATUS ntStatus;

//inside Wdm2DeviceIoControl

RtlInitUnicodeString(&us_ip, L"\\??\\C:\\Windows\\Temp\\sample_ip.txt"
);

case IOCTL_FILE_HANDLE :
KdPrint(("\n ++ IOCTL_PCI_CREATE_FILE"));

OpenAFile_Read(&hFile1, &us_ip, FALSE);

ntStatus = ZwQueryInformationFile(hFile1,
&iosb,
&StandardInfo,
sizeof(FILE_STANDARD_INFORMATION),
FileStandardInformation);

if (!NT_SUCCESS(ntStatus))
{
KdPrint(("\n ZwQueryInformationFile failed"));

}

LengthOfFile = StandardInfo.EndOfFile.LowPart;

KdPrint(("\n LengthOfFile is %d \n",LengthOfFile));

if (LengthOfFile < 1)
{
KdPrint(("\n LengthOfFile < 1 \n"));
}

pfile_buff = (PUCHAR)ExAllocatePool(NonPagedPool, LengthOfFile);

if (pfile_buff == NULL)
{
KdPrint(("\n Input buffer allocate failed\n"));

}
KdPrint(("\n Input buffer allocate passed\n"));

if(hFile1 != NULL)
{
KdPrint(("\n hFile1 success"));

ntStatus = ZwReadFile(hFile1,
NULL,
NULL,
NULL,
&iosb,
pfile_buff,
LengthOfFile,
NULL,
NULL
);

if( !NT_SUCCESS(ntStatus))
{
KdPrint(("\n ZwReadFile failed"));
ExFreePool(pfile_buff);
//return ntStatus;
}
else
{
KdPrint(("\n ZwReadFile success"));

}

ZwClose(hFile1);
}
else
{
KdPrint(("\n hFile1 failed"));
}

KdPrint(("\n -- IOCTL_PCI_CREATE_FILE"));
break;

// function for ZwCreateFile a file

NTSTATUS OpenAFile_Read( PHANDLE pFileHandle, PUNICODE_STRING
wszName,BOOLEAN bCreate )
{

KdPrint(("\n OpenAFile"));

RtlZeroMemory( &oa, sizeof(oa) );
RtlZeroMemory( &iosb, sizeof(iosb) );

oa.Length = sizeof(OBJECT_ATTRIBUTES);
oa.RootDirectory = NULL;
oa.ObjectName = wszName;
oa.Attributes = OBJ_CASE_INSENSITIVE;
oa.SecurityDescriptor = NULL;
oa.SecurityQualityOfService = NULL;

ntStatus = ZwCreateFile( pFileHandle,
FILE_GENERIC_READ | FILE_GENERIC_WRITE |
SYNCHRONIZE,
&oa,
&iosb,
0,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
bCreate ? FILE_OPEN_IF : FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0 );


if( !NT_SUCCESS(ntStatus))
{
KdPrint(("\n OpenAFile_Read failed"));
}
else
KdPrint(("\n OpenAFile_Read success"));

return ntStatus;
}

////////////////////////////////////////////////////////////////////////////////
 
A

Ayush

Next time please cross-post..

--
Ayush [ Be ''?'' Happy ]

For any query, search > www.Google.com
Want to know about a term > http://en.wikipedia.org


| Hi all,
| I'm writing a driver code in winxp.Here i want to access the file
| using ZwCreateFile,ZwReadFile,etc.I'm able to create a file using
| ZwCreateFile.But i'm not able to use ZwReadFile.When i call this
| function,my system is always getting hanged(phycial memory
| dump).Herewith i'v attached my code.Please go through this code and
| feedback to me.Help me with suggestions.
| Thanks in advance.
|
| /////////////////my code////////////////////////
|
| //global variables
|
| HANDLE hFile1;
|
| UNICODE_STRING us_ip;
|
| PUCHAR pfile_buff;
|
| ULONG LengthOfFile;
|
| FILE_STANDARD_INFORMATION StandardInfo;
|
| OBJECT_ATTRIBUTES oa;
|
| IO_STATUS_BLOCK iosb;
|
| NTSTATUS ntStatus;
|
| //inside Wdm2DeviceIoControl
|
| RtlInitUnicodeString(&us_ip, L"\\??\\C:\\Windows\\Temp\\sample_ip.txt"
| );
|
| case IOCTL_FILE_HANDLE :
| KdPrint(("\n ++ IOCTL_PCI_CREATE_FILE"));
|
| OpenAFile_Read(&hFile1, &us_ip, FALSE);
|
| ntStatus = ZwQueryInformationFile(hFile1,
| &iosb,
| &StandardInfo,
| sizeof(FILE_STANDARD_INFORMATION),
| FileStandardInformation);
|
| if (!NT_SUCCESS(ntStatus))
| {
| KdPrint(("\n ZwQueryInformationFile failed"));
|
| }
|
| LengthOfFile = StandardInfo.EndOfFile.LowPart;
|
| KdPrint(("\n LengthOfFile is %d \n",LengthOfFile));
|
| if (LengthOfFile < 1)
| {
| KdPrint(("\n LengthOfFile < 1 \n"));
| }
|
| pfile_buff = (PUCHAR)ExAllocatePool(NonPagedPool, LengthOfFile);
|
| if (pfile_buff == NULL)
| {
| KdPrint(("\n Input buffer allocate failed\n"));
|
| }
| KdPrint(("\n Input buffer allocate passed\n"));
|
| if(hFile1 != NULL)
| {
| KdPrint(("\n hFile1 success"));
|
| ntStatus = ZwReadFile(hFile1,
| NULL,
| NULL,
| NULL,
| &iosb,
| pfile_buff,
| LengthOfFile,
| NULL,
| NULL
| );
|
| if( !NT_SUCCESS(ntStatus))
| {
| KdPrint(("\n ZwReadFile failed"));
| ExFreePool(pfile_buff);
| //return ntStatus;
| }
| else
| {
| KdPrint(("\n ZwReadFile success"));
|
| }
|
| ZwClose(hFile1);
| }
| else
| {
| KdPrint(("\n hFile1 failed"));
| }
|
| KdPrint(("\n -- IOCTL_PCI_CREATE_FILE"));
| break;
|
| // function for ZwCreateFile a file
|
| NTSTATUS OpenAFile_Read( PHANDLE pFileHandle, PUNICODE_STRING
| wszName,BOOLEAN bCreate )
| {
|
| KdPrint(("\n OpenAFile"));
|
| RtlZeroMemory( &oa, sizeof(oa) );
| RtlZeroMemory( &iosb, sizeof(iosb) );
|
| oa.Length = sizeof(OBJECT_ATTRIBUTES);
| oa.RootDirectory = NULL;
| oa.ObjectName = wszName;
| oa.Attributes = OBJ_CASE_INSENSITIVE;
| oa.SecurityDescriptor = NULL;
| oa.SecurityQualityOfService = NULL;
|
| ntStatus = ZwCreateFile( pFileHandle,
| FILE_GENERIC_READ | FILE_GENERIC_WRITE |
| SYNCHRONIZE,
| &oa,
| &iosb,
| 0,
| FILE_ATTRIBUTE_NORMAL,
| FILE_SHARE_READ,
| bCreate ? FILE_OPEN_IF : FILE_OPEN,
| FILE_SYNCHRONOUS_IO_NONALERT,
| NULL,
| 0 );
|
|
| if( !NT_SUCCESS(ntStatus))
| {
| KdPrint(("\n OpenAFile_Read failed"));
| }
| else
| KdPrint(("\n OpenAFile_Read success"));
|
| return ntStatus;
| }
|
|
////////////////////////////////////////////////////////////////////////////////
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top