驱动开发与系统原理-设置文件属性
0环代码```
#define CTL_SETARR_PATH IRP_IOCTRL_CODE(11)
...
...
NTSTATUS SetArrIoCall(char *szFilePath){
DbgBreakPoint();
NTSTATUS ntSTATUS = STATUS_SUCCESS;
//文件句柄
HANDLE hFile = NULL;
//文件属性结构体
FILE_STANDARD_INFORMATION fsi = { 0 };
FILE_POSITION_INFORMATION fpi = { 0 };
//完成状态
IO_STATUS_BLOCK Iostatus = { 0 };
//对象属性
OBJECT_ATTRIBUTES ObjectAtt = { 0 };
ANSI_STRING asFilePath = { 0 };
UNICODE_STRING usFilePath = { 0 };
UNICODE_STRING usDriverFilePath = { 0 };
UNICODE_STRING usDrvPath = { 0 };
WCHARwcBuffer;
ULONG wcbufferLen = 256 * sizeof(WCHAR);
RtlInitEmptyUnicodeString(&usDrvPath, &wcBuffer, wcbufferLen);
RtlInitUnicodeString(&usDriverFilePath, L"\\??\\");
RtlInitAnsiString(&asFilePath, szFilePath);
RtlAnsiStringToUnicodeString(&usFilePath, &asFilePath, TRUE);
RtlAppendUnicodeStringToString(&usDrvPath, &usDriverFilePath);
RtlAppendUnicodeStringToString(&usDrvPath, &usFilePath);
RtlFreeUnicodeString(&usFilePath);
InitializeObjectAttributes(&ObjectAtt, &usDrvPath, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
ntSTATUS = ZwCreateFile(&hFile, GENERIC_WRITE, &ObjectAtt, &Iostatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_WRITE, FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
if (!NT_SUCCESS(ntSTATUS))
{
return ntSTATUS;
}
fpi.CurrentByteOffset.QuadPart = 100;//设置文件指针放到距离文件头100的地方
ntSTATUS = ZwSetInformationFile(hFile, &Iostatus, &fpi, sizeof(FILE_POSITION_INFORMATION), FilePositionInformation);
ntSTATUS = ZwQueryInformationFile(hFile, &Iostatus, &fsi, sizeof(FILE_STANDARD_INFORMATION), FileStandardInformation);
if (!NT_SUCCESS(ntSTATUS))
{
return ntSTATUS;
}
memset(g_Buffer, 0, sizeof(g_Buffer));
ntSTATUS = ZwReadFile(hFile, NULL, NULL, NULL, &Iostatus, g_Buffer, fsi.EndOfFile.QuadPart, NULL, NULL);
ZwClose(hFile);
return ntSTATUS;
}
...
...
case CTL_SETARR_PATH: {
ntStatus = SetArrIoCall(pInputBuffer);
if (NT_SUCCESS(ntStatus))
{
RtlZeroMemory(pOutputBuffer, 1024);
ULONG uRetlength = strlen(g_Buffer);
RtlCopyMemory(pOutputBuffer, g_Buffer, uRetlength);
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = uRetlength;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
else {
RtlZeroMemory(pOutputBuffer, 1024);
ULONG uRetlength = strlen("SetArr Failed!");
RtlCopyMemory(pOutputBuffer, "SetArr Failed!", uRetlength);
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = uRetlength;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
```
3环代码
```
case'9': {
RetNumber = 0;
memset(InputBuffer, 0, sizeof(InputBuffer));
memset(OutputBuffer, 0, sizeof(OutputBuffer));
printf("请输入需要设置属性的文件路径:\n");
scanf("%s", InputBuffer);
DeviceIoControl(hDriver, CTL_SETARR_PATH, InputBuffer, sizeof(InputBuffer), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
printf("返回数据: %s\n", OutputBuffer);
system("pause");
break;
}
```

页:
[1]