Transferred from: http://blog.csdn.net/cooblily/archive/2007/10/27/1848037.aspx
Have not been in a long time to write articles, do not know what to do, the result or to learn to write a bit with the native API program, these API prototypes of course long in DDK inside find, but because NTDLL.DLL have export ah, so can LoadLibrary call into this dynamic connection file, Then GetProcAddress find the corresponding API address, then of course you can call it.
The most troublesome thing in the whole process is to turn the DDK over to find the function prototype to use, the structure used by the function, and some macros. Copy into the program, OK, here are the results of my study.
The following code creates a ForZwFileTest.txt file in C: and writes the content, and then deletes it. In fact, there is no use, anyway, there is a soft public API is not used, and the use of these open APIs to achieve this function is entirely because of boredom.
[CPP]View PlainCopyprint?
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- typedef unsigned long NTSTATUS;
- typedef unsigned short USHORT;
- typedef unsigned long ULONG;
- typedef unsigned long DWORD;
- typedef long long;
- typedef __INT64 Longlong;
- typedef struct unicode_string{
- USHORT Length;
- USHORT MaxLen;
- USHORT *buffer;
- } unicode_string,*punicode_string;
- #define Obj_inherit 0x00000002l
- #define Obj_permanent 0x00000010l
- #define Obj_exclusive 0x00000020l
- #define Obj_case_insensitive 0x00000040l
- #define OBJ_OPENIF 0x00000080l
- #define Obj_openlink 0x00000100l
- #define Obj_kernel_handle 0x00000200l
- #define Obj_force_access_check 0x00000400l
- #define Obj_valid_attributes 0X000007F2L
- #define FILE_ATTRIBUTE_NORMAL 0x00000080
- #define FILE_SHARE_DELETE 0x00000004
- #define FILE_OPEN_IF 0x00000003
- #define File_synchronous_io_nonalert 0x00000020
- #define GENERIC_WRITE (0x40000000l)
- #define SYNCHRONIZE (0x00100000l)
- #define GENERIC_READ (0x80000000l)
- typedef struct _object_attributes{
- ULONG Length;
- HANDLE rootdirectory;
- Punicode_string ObjectName;
- ULONG Attributes;
- PVOID SecurityDescriptor;
- PVOID Securityqualityofservice;
- } object_attributes, *pobject_attributes;
- typedef CONST Object_attributes *pcobject_attributes;
- typedef NTSTATUS (__stdcall *zwdeletefile) (
- In Pobject_attributes objectattributes);
- typedef VOID (__stdcall *rtlinitunicodestring) (
- In Out Punicode_string destinationstring,
- In pcwstr sourcestring);
- typedef struct _io_status_block{
- DWORD Status;
- ULONG information;
- } Io_status_block, *pio_status_block;
- typedef NTSTATUS (__stdcall *zwcreatefile) (
- Out phandle filehandle,
- In Access_mask desiredaccess,
- In Pobject_attributes Objectattributes,
- Out Pio_status_block Iostatusblock,
- In Plarge_integer allocationsize OPTIONAL,
- in ULONG fileattributes,
- in ULONG shareaccess,
- in ULONG createdisposition,
- in ULONG createoptions,
- in PVOID eabuffer OPTIONAL,
- In ULONG ealength);
- typedef VOID (Ntapi *pio_apc_routine) (
- in PVOID Apccontext,
- In Pio_status_block Iostatusblock,
- In ULONG Reserved);
- typedef NTSTATUS (__stdcall *zwwritefile) (
- in HANDLE filehandle,
- in HANDLE Event OPTIONAL,
- In Pio_apc_routine Apcroutine OPTIONAL,
- in PVOID apccontext OPTIONAL,
- Out Pio_status_block Iostatusblock,
- in PVOID Buffer,
- in ULONG Length,
- In Plarge_integer Byteoffset OPTIONAL,
- In pulong Key OPTIONAL);
- typedef NTSTATUS (__stdcall *zwclose) (
- In HANDLE HANDLE);
- int main ()
- {
- hinstance Hntdll;
- Zwdeletefile Zwdeletefile;
- Rtlinitunicodestring rtlinitunicodestring;
- ZwCreateFile ZwCreateFile;
- Zwwritefile Zwwritefile;
- Zwclose Zwclose;
- Hntdll = LoadLibrary ("NTDLL");
- if (!hntdll)
- return 0;
- Zwdeletefile = (zwdeletefile) GetProcAddress (Hntdll,"Zwdeletefile");
- rtlinitunicodestring = (rtlinitunicodestring) GetProcAddress (Hntdll,"rtlinitunicodestring");
- ZwCreateFile = (zwcreatefile) GetProcAddress (Hntdll,"ZwCreateFile");
- Zwwritefile = (zwwritefile) GetProcAddress (Hntdll,"Zwwritefile");
- Zwclose = (zwclose) GetProcAddress (Hntdll,"Zwclose");
- Unicode_string ObjectName;
- Rtlinitunicodestring (&objectname,l"//?? C://forzwfiletest.txt ");//Remember this is to have//?? In front of, DDK said.
- Object_attributes objectattributes = {
- sizeof (object_attributes), //Length
- NULL, //RootDirectory
- &objectname, //ObjectName
- Obj_case_insensitive, //Attributes
- 0, //SecurityDescriptor
- NULL, //Securityqualityofservice
- };
- HANDLE hfile;
- PVOID content = "Forzwfiletest";
- Io_status_block Iostatusblock;
- ZwCreateFile (&hfile,
- generic_write| synchronize| Generic_read,
- &objectattributes,
- &iostatusblock,
- 0,
- File_attribute_normal,
- File_share_delete,
- File_open_if,
- File_synchronous_io_nonalert,
- Null
- 0);
- Zwwritefile (hfile, 0, 0, 0, &iostatusblock, content,, null, NULL);
- Zwclose (hfile);
- Zwdeletefile (&objectattributes);
- FreeLibrary (Hntdll);
- return 0;
- }
Transferred from: http://blog.csdn.net/cooblily/archive/2007/10/27/1848037.aspx
About calling some ZW series file manipulation functions in WIN32