本文整理汇总了C++中NtCreateFile函数的典型用法代码示例。如果您正苦于以下问题:C++ NtCreateFile函数的具体用法?C++ NtCreateFile怎么用?C++ NtCreateFile使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NtCreateFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetDriveGeometry
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Reads or writes specified number of sectors from/to specified buffer.
//
static BOOL GetDriveGeometry(
PWCHAR Drive, // drive name to read/write sectors from/to
PDISK_GEOMETRY DiskGeometry
)
{
BOOL Ret = FALSE;
HANDLE hDrive;
NTSTATUS ntStatus;
OBJECT_ATTRIBUTES oa = {0};
UNICODE_STRING us;
IO_STATUS_BLOCK IoStatus = {0};
HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
RtlInitUnicodeString(&us, Drive);
InitializeObjectAttributes(&oa, &us, OBJ_CASE_INSENSITIVE, NULL, NULL);
ntStatus = NtCreateFile(&hDrive, GENERIC_WRITE | GENERIC_READ, &oa, &IoStatus, NULL, FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OPEN, 0, NULL, 0);
if (NT_SUCCESS(ntStatus))
{
ntStatus = NtDeviceIoControlFile(hDrive, hEvent, NULL, NULL, &IoStatus, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, DiskGeometry, sizeof(DISK_GEOMETRY));
if (ntStatus == STATUS_PENDING)
WaitForSingleObject(hEvent, INFINITE);
NtClose(hDrive);
if (NT_SUCCESS(IoStatus.Status))
Ret = TRUE;
} // if (NT_SUCCESS(ntStatus))
return(Ret);
}
示例2: SfuIsDirectoryExists
//@@unimplemented
BOOL SfuIsDirectoryExists(
_In_ PWSTR DirectoryName
)
{
UNICODE_STRING usDirectoryName;
NTSTATUS status;
HANDLE DirectoryHandle;
IO_STATUS_BLOCK IoStatusBlock;
OBJECT_ATTRIBUTES ObjectAttributes;
RtlInitUnicodeString(&usDirectoryName, DirectoryName);
InitializeObjectAttributes(&ObjectAttributes, &usDirectoryName, OBJ_CASE_INSENSITIVE, 0, NULL);
status = NtCreateFile(&DirectoryHandle,
SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_READONLY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
FILE_OPEN_IF,
FILE_DIRECTORY_FILE,
NULL,
0
);
if (NT_SUCCESS(status)) {
NtClose(DirectoryHandle);
return TRUE;
}
return FALSE;
}
示例3: SafeNtCreateFile
NTSTATUS WINAPI SafeNtCreateFile(
PHANDLE FileHandle,
ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes,
PIO_STATUS_BLOCK IoStatusBlock,
PLARGE_INTEGER AllocationSize,
ULONG FileAttributes,
ULONG ShareAccess,
ULONG CreateDisposition,
ULONG CreateOptions,
PVOID EaBuffer,
ULONG EaLength
)
{
NTSTATUS rc;
if (CheckOldFunction(&OldNtCreateFile))
rc=OldNtCreateFile(FileHandle,DesiredAccess,ObjectAttributes,IoStatusBlock,AllocationSize,FileAttributes,
ShareAccess,CreateDisposition,CreateOptions,EaBuffer,EaLength);
else
rc=NtCreateFile(FileHandle,DesiredAccess,ObjectAttributes,IoStatusBlock,AllocationSize,FileAttributes,
ShareAccess,CreateDisposition,CreateOptions,EaBuffer,EaLength);
return rc;
}
示例4: MyCreateDirectory
static NTSTATUS MyCreateDirectory(TFileTestData * pData, POBJECT_ATTRIBUTES pObjAttr, PIO_STATUS_BLOCK pIoStatus)
{
NTSTATUS Status;
HANDLE SaveTransactionHandle = NULL;
HANDLE DirectoryHandle = NULL;
if(pData->bUseTransaction)
{
SaveTransactionHandle = pfnRtlGetCurrentTransaction();
pfnRtlSetCurrentTransaction(pData->hTransaction);
}
Status = NtCreateFile(&DirectoryHandle,
FILE_ADD_FILE,
pObjAttr,
pIoStatus,
NULL,
FILE_ATTRIBUTE_DIRECTORY,
FILE_SHARE_READ,
FILE_OPEN_IF,
FILE_DIRECTORY_FILE,
NULL,
0);
if(DirectoryHandle != NULL)
NtClose(DirectoryHandle);
if(pData->bUseTransaction)
{
pfnRtlSetCurrentTransaction(SaveTransactionHandle);
}
// If the directory already exists, take it as success
return Status;
}
示例5: SfuCreateDirectory
/*
* SfuCreateDirectory
*
* Purpose:
*
* Native create directory.
*
*/
BOOL SfuCreateDirectory(
_In_ OBJECT_ATTRIBUTES *ObjectAttributes
)
{
NTSTATUS status;
HANDLE DirectoryHandle;
IO_STATUS_BLOCK IoStatusBlock;
status = NtCreateFile(&DirectoryHandle,
FILE_GENERIC_WRITE,
ObjectAttributes,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,//za use hidden+system
0,
FILE_OPEN_IF,
FILE_DIRECTORY_FILE,
NULL,
0
);
if (!NT_SUCCESS(status)) {
return FALSE;
}
NtClose(DirectoryHandle);
return TRUE;
}
示例6: CreateRegistryFile
/*
* Should be called under privileges
*/
static NTSTATUS
CreateRegistryFile(
IN HANDLE RootDirectory OPTIONAL,
IN PUNICODE_STRING RootPath OPTIONAL,
IN PCWSTR RegistryKey,
IN HANDLE ProtoKeyHandle)
{
NTSTATUS Status;
HANDLE RootPathHandle, FileHandle;
UNICODE_STRING FileName;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
/* Open the root directory */
Status = OpenDirectoryByHandleOrPath(&RootPathHandle, RootDirectory, RootPath);
if (!NT_SUCCESS(Status))
{
DPRINT1("OpenDirectoryByHandleOrPath failed, Status 0x%08lx\n", Status);
return Status;
}
/* Create the file */
RtlInitUnicodeString(&FileName, RegistryKey);
InitializeObjectAttributes(&ObjectAttributes,
&FileName,
OBJ_CASE_INSENSITIVE,
(HANDLE)((ULONG_PTR)RootPathHandle & ~1), // Remove the opened-locally flag
NULL);
Status = NtCreateFile(&FileHandle,
FILE_GENERIC_WRITE /* | DELETE */,
&ObjectAttributes,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL /* | FILE_FLAG_DELETE_ON_CLOSE */,
0,
FILE_OVERWRITE_IF,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE,
NULL,
0);
if (!NT_SUCCESS(Status))
{
DPRINT1("NtCreateFile(%wZ) failed, Status 0x%08lx\n", &FileName, Status);
goto Cleanup;
}
/* Save the selected hive into the file */
Status = NtSaveKeyEx(ProtoKeyHandle, FileHandle, REG_LATEST_FORMAT);
if (!NT_SUCCESS(Status))
{
DPRINT1("NtSaveKeyEx(%wZ) failed, Status 0x%08lx\n", &FileName, Status);
}
/* Close the file, the root directory (if opened locally), and return */
NtClose(FileHandle);
Cleanup:
if ((ULONG_PTR)RootPathHandle & 1) NtClose((HANDLE)((ULONG_PTR)RootPathHandle & ~1));
return Status;
}
示例7: NtDeleteFile2
NTSTATUS NtDeleteFile2(POBJECT_ATTRIBUTES ObjectAttributes)
{
HANDLE hFile;
IO_STATUS_BLOCK IoStatusBlock;
auto Status = NtOpenFile(&hFile, SYNCHRONIZE | DELETE, ObjectAttributes, &IoStatusBlock, FILE_SHARE_DELETE | FILE_SHARE_READ, FILE_OPEN_REPARSE_POINT | FILE_OPEN_FOR_BACKUP_INTENT);
if (Status)
{
return Status;
}
FILE_DISPOSITION_INFORMATION Dispostion = { TRUE };
Status = NtSetInformationFile(hFile, &IoStatusBlock, &Dispostion, sizeof(Dispostion), FileDispositionInformation);
NtClose(hFile);
if (Status == 0)
return 0;
//使用文件取代法删除
auto cbData = ObjectAttributes->ObjectName->Length + sizeof(L"~$") - 2;
UNICODE_STRING TempFileName = { cbData,cbData, (wchar_t*)new byte[cbData] };
memcpy(TempFileName.Buffer, ObjectAttributes->ObjectName->Buffer, ObjectAttributes->ObjectName->Length);
*(wchar_t*)(((byte*)TempFileName.Buffer) + ObjectAttributes->ObjectName->Length) = L'~';
*(wchar_t*)(((byte*)TempFileName.Buffer) + ObjectAttributes->ObjectName->Length + 2) = L'$';
OBJECT_ATTRIBUTES ObjectAttributes2 = { sizeof(OBJECT_ATTRIBUTES),ObjectAttributes->RootDirectory, &TempFileName, OBJ_CASE_INSENSITIVE };
auto Status2 = NtCreateFile(&hFile, SYNCHRONIZE | DELETE, &ObjectAttributes2, &IoStatusBlock, 0, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_OPEN_FOR_BACKUP_INTENT | FILE_DELETE_ON_CLOSE, NULL, 0);
delete[](byte*)TempFileName.Buffer;
cbData = sizeof(FILE_RENAME_INFORMATION) + ObjectAttributes->ObjectName->Length;
FILE_RENAME_INFORMATION* pRenameInfo = (FILE_RENAME_INFORMATION*)new byte[cbData];
memcpy(pRenameInfo->FileName, ObjectAttributes->ObjectName->Buffer, ObjectAttributes->ObjectName->Length);
pRenameInfo->ReplaceIfExists = TRUE;
pRenameInfo->RootDirectory = ObjectAttributes->RootDirectory;
pRenameInfo->FileNameLength = ObjectAttributes->ObjectName->Length;
//FILE_RENAME_INFORMATION RenameInfo = {TRUE,hRootDir ,FileName.};
Status2 = NtSetInformationFile(hFile, &IoStatusBlock, pRenameInfo, cbData, FileRenameInformation);
delete[](byte*)pRenameInfo;
NtClose(hFile);
return Status2 == 0 ? 0 : Status;
}
示例8: Mkdir
VOID Mkdir(
IN PCHAR String
)
{
NTSTATUS Status;
HANDLE FileHandle;
OBJECT_ATTRIBUTES ObjectAttributes;
STRING NameString;
IO_STATUS_BLOCK IoStatus;
LARGE_INTEGER AllocationSize;
//
// Get the filename
//
simprintf("Mkdir ", 0);
simprintf(String, 0);
simprintf("\n", 0);
//
// Create the new directory
//
AllocationSize = LiFromLong( 4 );
RtlInitString( &NameString, String );
InitializeObjectAttributes( &ObjectAttributes, &NameString, 0, NULL, NULL );
if (!NT_SUCCESS(Status = NtCreateFile( &FileHandle,
SYNCHRONIZE,
&ObjectAttributes,
&IoStatus,
&AllocationSize,
0L,
0L,
FILE_CREATE,
WriteThrough | FILE_DIRECTORY_FILE,
(PVOID)NULL,
0L ))) {
CreateFileError( Status , String );
return;
}
//
// Now close the directory
//
if (!NT_SUCCESS(Status = NtClose( FileHandle ))) {
CloseError( Status );
}
//
// And return to our caller
//
return;
}
示例9: XDeleteDirectory
int XDeleteDirectory(char *directoryName)
{
ANSI_STRING xboxFilename;
IO_STATUS_BLOCK ioStatusBlock;
OBJECT_ATTRIBUTES objectAttributes;
FILE_DISPOSITION_INFORMATION deleteInfo;
NTSTATUS status;
int handle;
#ifdef DEBUG
debugPrint("XDeleteDirectory directoryName=%s\n", directoryName);
#endif
char tmp[200];
int rc = XConvertDOSFilenameToXBOX(directoryName, tmp);
if (rc != STATUS_SUCCESS)
return rc;
RtlInitAnsiString(&xboxFilename, tmp);
objectAttributes.RootDirectory = NULL;
objectAttributes.Attributes = OBJ_CASE_INSENSITIVE;
objectAttributes.ObjectName = &xboxFilename;
// Open the file for deletion
status = NtCreateFile(
(PHANDLE)&handle,
DELETE | SYNCHRONIZE,
&objectAttributes,
&ioStatusBlock,
NULL,
0,
FILE_SHARE_DELETE,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
if (!NT_SUCCESS(status))
return RtlNtStatusToDosError(status);
// Delete the file
deleteInfo.DeleteFile = TRUE;
status = NtSetInformationFile(
(HANDLE)handle,
&ioStatusBlock,
&deleteInfo,
sizeof(deleteInfo),
FileDispositionInformation);
if (!NT_SUCCESS(status))
return RtlNtStatusToDosError(status);
else
{
// Close the dead handle and return
NtClose((HANDLE)handle);
return STATUS_SUCCESS;
}
}
示例10: RtlCliOpenInputDevice
/*++
* @name RtlCliOpenInputDevice
*
* The RtlCliOpenInputDevice routine opens an input device.
*
* @param Handle
* Pointer where the handle for the input device will be returned.
*
* @param Type
* Type of the input device to use.
*
* @return STATUS_SUCCESS or error code when attemping to open the device.
*
* @remarks This routine supports both mouse and keyboard input devices.
*
*--*/
NTSTATUS
RtlCliOpenInputDevice(OUT PHANDLE Handle,
IN CON_DEVICE_TYPE Type)
{
UNICODE_STRING Driver;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK Iosb;
HANDLE hDriver;
NTSTATUS Status;
//
// Chose the driver to use
// FIXME: Support MouseType later
// FIXME: Don't hardcode keyboard path
//
if (Type == KeyboardType)
{
RtlInitUnicodeString(&Driver, L"\\Device\\KeyboardClass0");
}
//
// Initialize the object attributes
//
InitializeObjectAttributes(&ObjectAttributes,
&Driver,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
//
// Open a handle to it
//
Status = NtCreateFile(&hDriver,
SYNCHRONIZE | GENERIC_READ | FILE_READ_ATTRIBUTES,
&ObjectAttributes,
&Iosb,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OPEN,
FILE_DIRECTORY_FILE,
NULL,
0);
//
// Now create an event that will be used to wait on the device
//
InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
Status = NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, &ObjectAttributes, 1, 0);
//
// Return the handle
//
*Handle = hDriver;
return Status;
}
示例11: SetupCreateSingleDirectory
static
NTSTATUS
SetupCreateSingleDirectory(
PWCHAR DirectoryName)
{
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
UNICODE_STRING PathName;
HANDLE DirectoryHandle;
NTSTATUS Status;
if(!RtlCreateUnicodeString(&PathName, DirectoryName))
return STATUS_NO_MEMORY;
if (PathName.Length > sizeof(WCHAR) &&
PathName.Buffer[PathName.Length / sizeof(WCHAR) - 2] == L'\\' &&
PathName.Buffer[PathName.Length / sizeof(WCHAR) - 1] == L'.')
{
PathName.Length -= sizeof(WCHAR);
PathName.Buffer[PathName.Length / sizeof(WCHAR)] = 0;
}
if (PathName.Length > sizeof(WCHAR) &&
PathName.Buffer[PathName.Length / sizeof(WCHAR) - 1] == L'\\')
{
PathName.Length -= sizeof(WCHAR);
PathName.Buffer[PathName.Length / sizeof(WCHAR)] = 0;
}
InitializeObjectAttributes(&ObjectAttributes,
&PathName,
OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
NULL,
NULL);
Status = NtCreateFile(&DirectoryHandle,
DIRECTORY_ALL_ACCESS,
&ObjectAttributes,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_OPEN_IF,
FILE_DIRECTORY_FILE,
NULL,
0);
if (NT_SUCCESS(Status))
{
NtClose(DirectoryHandle);
}
RtlFreeUnicodeString(&PathName);
return Status;
}
示例12: SmpDeleteFile
/*++
* @name SmpDeleteFile
*
* The SmpDeleteFile function deletes a specify file.
*
* @param lpFileName
* the name of a file which should be deleted
*
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
* othwerwise.
*
* @remarks
* This function is called by SmpMoveFilesQueryRoutine().
*
*
*--*/
NTSTATUS
SmpDeleteFile( IN LPCWSTR lpFileName )
{
FILE_DISPOSITION_INFORMATION FileDispInfo;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
UNICODE_STRING FileNameU;
HANDLE FileHandle;
NTSTATUS Status;
DPRINT("SmpDeleteFile ( %S )\n", lpFileName);
if( !lpFileName )
return (STATUS_INVALID_PARAMETER);
RtlInitUnicodeString(&FileNameU, lpFileName);
InitializeObjectAttributes(&ObjectAttributes,
&FileNameU,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
Status = NtCreateFile (&FileHandle,
DELETE,
&ObjectAttributes,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if( !NT_SUCCESS(Status) ) {
DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
return (Status);
}
FileDispInfo.DeleteFile = TRUE;
Status = NtSetInformationFile(
FileHandle,
&IoStatusBlock,
&FileDispInfo,
sizeof(FILE_DISPOSITION_INFORMATION),
FileDispositionInformation );
NtClose(FileHandle);
return (Status);
}
示例13: IthOpenPipe
HANDLE IthOpenPipe(LPWSTR name, ACCESS_MASK direction)
{
UNICODE_STRING us;
RtlInitUnicodeString(&us,name);
SECURITY_DESCRIPTOR sd={1};
OBJECT_ATTRIBUTES oa={sizeof(oa),0,&us,OBJ_CASE_INSENSITIVE,&sd,0};
HANDLE hFile;
IO_STATUS_BLOCK isb;
if (NT_SUCCESS(NtCreateFile(&hFile,direction,&oa,&isb,0,0,FILE_SHARE_READ,FILE_OPEN,0,0,0)))
return hFile;
else return INVALID_HANDLE_VALUE;
}
示例14: LogfBackupFile
NTSTATUS
LogfBackupFile(PLOGFILE LogFile,
PUNICODE_STRING BackupFileName)
{
NTSTATUS Status;
LOGFILE BackupLogFile;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
DPRINT1("LogfBackupFile(%p, %wZ)\n", LogFile, BackupFileName);
/* Lock the log file shared */
RtlAcquireResourceShared(&LogFile->Lock, TRUE);
InitializeObjectAttributes(&ObjectAttributes,
BackupFileName,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
Status = NtCreateFile(&BackupLogFile.FileHandle,
GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
FILE_CREATE,
FILE_WRITE_THROUGH | FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if (!NT_SUCCESS(Status))
{
DPRINT("Cannot create backup file `%wZ' (Status 0x%08lx)\n", BackupFileName, Status);
goto Quit;
}
Status = ElfBackupFile(&LogFile->LogFile,
&BackupLogFile.LogFile);
Quit:
/* Close the backup file */
if (BackupLogFile.FileHandle != NULL)
NtClose(BackupLogFile.FileHandle);
/* Unlock the log file */
RtlReleaseResource(&LogFile->Lock);
return Status;
}
示例15: winfs_symlink
static int winfs_symlink(struct mount_point *mp, const char *target, const char *linkpath)
{
WCHAR wlinkpath[PATH_MAX];
int len = filename_to_nt_pathname(mp, linkpath, wlinkpath, PATH_MAX);
if (len <= 0)
return -L_ENOENT;
UNICODE_STRING pathname;
RtlInitCountedUnicodeString(&pathname, wlinkpath, len * sizeof(WCHAR));
IO_STATUS_BLOCK status_block;
OBJECT_ATTRIBUTES attr;
attr.Length = sizeof(OBJECT_ATTRIBUTES);
attr.RootDirectory = NULL;
attr.ObjectName = &pathname;
attr.Attributes = 0;
attr.SecurityDescriptor = NULL;
attr.SecurityQualityOfService = NULL;
HANDLE handle;
NTSTATUS status = NtCreateFile(&handle, SYNCHRONIZE | FILE_WRITE_DATA, &attr, &status_block, NULL,
FILE_ATTRIBUTE_SYSTEM, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE,
FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
if (!NT_SUCCESS(status))
{
if (status == STATUS_OBJECT_NAME_EXISTS || status == STATUS_OBJECT_NAME_COLLISION)
{
log_warning("File already exists.");
return -L_EEXIST;
}
log_warning("NtCreateFile() failed, status: %x", status);
return -L_ENOENT;
}
DWORD num_written;
if (!WriteFile(handle, WINFS_SYMLINK_HEADER, WINFS_SYMLINK_HEADER_LEN, &num_written, NULL) || num_written < WINFS_SYMLINK_HEADER_LEN)
{
log_warning("WriteFile() failed, error code: %d.", GetLastError());
NtClose(handle);
return -L_EIO;
}
DWORD targetlen = strlen(target);
if (!WriteFile(handle, target, targetlen, &num_written, NULL) || num_written < targetlen)
{
log_warning("WriteFile() failed, error code: %d.", GetLastError());
NtClose(handle);
return -L_EIO;
}
NtClose(handle);
return 0;
}