DeviceIoControl: Access device driver via API and get window File/folder ID

Source: Internet
Author: User

1. The device handle can be obtained using the API function CreateFile. It is a prototype of

HANDLE CreateFile (

LPCTSTR lpFileName,//File name/device path

DWORD dwdesiredaccess,//access mode

DWORD dwShareMode,//Shared mode

Lpsecurity_attributes lpsecurityattributes,//Security descriptor pointer

DWORD dwcreationdisposition,//Creation mode

DWORD dwflagsandattributes,//file attributes and Flags

HANDLE handle to hTemplateFile//template file

);

The function prototype for 2.DeviceIoControl is

BOOL DeviceIoControl (

HANDLE hdevice,//device handle

DWORD Dwiocontrolcode,//control code

LPVOID Lpinbuffer,//input data buffer pointer

DWORD ninbuffersize,//input data buffer length

LPVOID lpOutBuffer,//output data buffer pointer

DWORD noutbuffersize,//output data buffer length

Lpdword lpbytesreturned,//output data actual length unit length

lpoverlapped lpoverlapped//overlapping operation structure pointer

);

3. Example of basic parameters for obtaining a hard drive via DeviceIoControl
#include <windows.h>#include<winioctl.h>BOOL getdrivegeometry (disk_geometry*PDG)               {HANDLE hdevice; //handle to examinedBOOL Bresult;//Results FlagDWORD junk;//Discard ResultsHdevice = CreateFile ("////.//physicaldrive0",//Drive to Open                    0,//No access to the driveFile_share_read |//Share modeFile_share_write, NULL,//Default security AttributesOpen_existing,//Disposition                    0,//file AttributesNULL);//Do not copy file attributes    if(Hdevice = = INVALID_HANDLE_VALUE)//cannot open the drive    {        return(FALSE); } bresult= DeviceIoControl (Hdevice,//device to be queriedIoctl_disk_get_drive_geometry,//operation to performNull0,//no input bufferPdgsizeof(*PDG),//Output Buffer&junk,//# bytes returned(lpoverlapped) NULL);//synchronous I/OCloseHandle (Hdevice); return(bresult);}intMainintargcChar*argv[])            {disk_geometry PDG; //disk drive Geometry structureBOOL Bresult;//generic Results FlagULONGLONG DiskSize;//size of the drive, in bytesBresult = Getdrivegeometry (&PDG); if(bresult) {printf ("cylinders =%i64d/n", PDG.        Cylinders); printf ("Tracks per cylinder =%ld/n", (ULONG) PDG.        Trackspercylinder); printf ("Sectors per track =%ld/n", (ULONG) PDG.        Sectorspertrack); printf ("Bytes per sector =%ld/n", (ULONG) PDG.          BytesPerSector); DiskSize= PDG. Cylinders.quadpart * (ULONG) PDG. Trackspercylinder *(ULONG) PDG. Sectorspertrack*(ULONG) PDG.        BytesPerSector; printf ("Disk size =%i64d (Bytes) =%I64D (Mb)/n", DiskSize, DiskSize/ (1024x768*1024x768)); }    Else{printf ("Getdrivegeometry failed. Error%ld./n", GetLastError ()); }    return((int) bresult);}

Note: The above is from other places to obtain the reference, as to the address has been forgotten, forgive ~

4. Get window file/folder ID, such as open doc file can also get

Under window, each file/folder has a unique ID, even if it is a copy of the same file, the ID under window is not the same. However, Office files will be different, if you use Office files to make changes and save, in fact, it is the actual operation of the file you open when you create a cache file, and then when you modify the completion and save, it will copy the current file to the cache file, delete the original file, rename the cache file , it's actually the equivalent of creating a new file. So, to modify the Office file, its window's ID will change.

But the other files are the only ones. Here's how to get it:

HANDLE hfile =Invalid_handle_value;dword cbout; File_objectid_buffer buf= {0}; Qfileinfo file (filePath);if(File.isdir ()) {hfile= CreateFile ((wchar_t*) Filepath.utf16 (), File_list_directory, File_share_read|File_share_write, NULL,//Security Attributesopen_existing, File_flag_backup_semantics|//<-The required priviliges for this flag are:se_backup_name and Se_restore_name. Cprivilegeenabler takes care of.null, NULL); }    Else{hfile= CreateFile ((wchar_t*) filepath.utf16 (),0, File_share_read, NULL, open_existing,0, NULL); }    if(hfile = =Invalid_handle_value) {Q_debug ()<< QString ("CreateFile (% 1) failed with:%2"). Arg (FilePath). Arg (GetLastError ()); FileID=""; returnret; }    if(DeviceIoControl (hfile, fsctl_create_or_get_object_id, NULL,0, &buf,sizeof(BUF), &cbout, NULL))//DeviceIoControl only supports NTFS{GUID guid; CopyMemory (&guid, &buf. ObjectId,sizeof(GUID)); WCHAR szguid[ the]; StringFromGUID2 (GUID, Szguid, the); FileID=qstring::fromstdwstring (SZGUID); RET=true; } closehandle (hfile);

DeviceIoControl: Access device driver via API and get window File/folder ID

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.