小弟要在驱动中递归的创建目录,下面是我写的一个函数。可在调试过程中,总是少创建一级。比如我要创建"\\??\\C:\\Virtual\\E\\avm\\bin"的一个目录,最后只能创建到avm这一级。最后的bin这个目录不知道到哪里去了。小弟才疏学浅,刚接触驱动开发,希望哪位好心人给指点指点,下面是程序的源代码。谢谢各位了
=============================================
#include <ntddk.h>
NTSTATUS
CreateDir(
UNICODE_STRING ANewPath
)
{
HANDLE DirHandle;
NTSTATUS Status=STATUS_SUCCESS;
OBJECT_ATTRIBUTES DirOA;
IO_STATUS_BLOCK IoSB;
UNICODE_STRING BNewPath;
PWSTR plast;
USHORT i;
USHORT j;
USHORT k;
InitializeObjectAttributes(
&DirOA,
&ANewPath,
OBJ_CASE_INSENSITIVE,
NULL,
NULL
);
Status=ZwCreateFile(
&DirHandle,
SYNCHRONIZE|FILE_ALL_ACCESS,
&DirOA,
&IoSB,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_VALID_FLAGS|FILE_SHARE_READ|FILE_SHARE_WRITE,
FILE_OPEN_IF,
FILE_DIRECTORY_FILE,
NULL,
0);
KdPrint( ("[-]CreateDir create dir:%ws\n", ANewPath.Buffer));
if (NT_SUCCESS(Status)){
KdPrint(("[-]create dir ok.%ws\n", ANewPath.Buffer));
ZwClose(DirHandle);
return STATUS_SUCCESS;
}else{
if (Status==STATUS_OBJECT_NAME_EXISTS)
{
KdPrint(("[+]STATUS_OBJECT_NAME_EXISTS\n"));
return STATUS_SUCCESS;
}else
{
//Get Parent Path
k=(ANewPath.Length/2);
for (i=0;i<k;i++)
{
if (ANewPath.Buffer[i]==L'\\')
{
plast=&ANewPath.Buffer[i];
}
}
*plast= '\0';
RtlInitUnicodeString(&BNewPath,ANewPath.Buffer);
KdPrint(("[+]CreateDir the BNewPath is :%ws\n",BNewPath.Buffer));
Status=CreateDir(BNewPath);
if (!NT_SUCCESS(Status))
{
KdPrint(("Call CreateDir failed with status:%08x\n",Status));
return Status;
}else
{
KdPrint(("[-] create father dir ok. %ws \n", BNewPath.Buffer));
Status=ZwCreateFile(
&DirHandle,
SYNCHRONIZE|FILE_ALL_ACCESS,
&DirOA,
&IoSB,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_VALID_FLAGS|FILE_SHARE_READ|FILE_SHARE_WRITE,
FILE_OPEN_IF,
FILE_DIRECTORY_FILE,
NULL,
0);
if (!NT_SUCCESS(Status))
{
KdPrint((" call ZwCreateFile :status is %08x\n",Status));
return Status;
}else
ZwClose(DirHandle);
return STATUS_SUCCESS;
}
}
KdPrint(("@CreateDir:create directory failed with status %08X\n",Status));
}
return Status;
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!