About calling some ZW series file manipulation functions in WIN32

Source: Internet
Author: User

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?
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. typedef unsigned long NTSTATUS;
  5. typedef unsigned short USHORT;
  6. typedef unsigned long ULONG;
  7. typedef unsigned long DWORD;
  8. typedef long long;
  9. typedef __INT64 Longlong;
  10. typedef struct unicode_string{
  11. USHORT Length;
  12. USHORT MaxLen;
  13. USHORT *buffer;
  14. } unicode_string,*punicode_string;
  15. #define Obj_inherit 0x00000002l
  16. #define Obj_permanent 0x00000010l
  17. #define Obj_exclusive 0x00000020l
  18. #define Obj_case_insensitive 0x00000040l
  19. #define OBJ_OPENIF 0x00000080l
  20. #define Obj_openlink 0x00000100l
  21. #define Obj_kernel_handle 0x00000200l
  22. #define Obj_force_access_check 0x00000400l
  23. #define Obj_valid_attributes 0X000007F2L
  24. #define FILE_ATTRIBUTE_NORMAL 0x00000080
  25. #define FILE_SHARE_DELETE 0x00000004
  26. #define FILE_OPEN_IF 0x00000003
  27. #define File_synchronous_io_nonalert 0x00000020
  28. #define GENERIC_WRITE (0x40000000l)
  29. #define SYNCHRONIZE (0x00100000l)
  30. #define GENERIC_READ (0x80000000l)
  31. typedef struct _object_attributes{
  32. ULONG Length;
  33. HANDLE rootdirectory;
  34. Punicode_string ObjectName;
  35. ULONG Attributes;
  36. PVOID SecurityDescriptor;
  37. PVOID Securityqualityofservice;
  38. } object_attributes, *pobject_attributes;
  39. typedef CONST Object_attributes *pcobject_attributes;
  40. typedef NTSTATUS (__stdcall *zwdeletefile) (
  41. In Pobject_attributes objectattributes);
  42. typedef VOID (__stdcall *rtlinitunicodestring) (
  43. In Out Punicode_string destinationstring,
  44. In pcwstr sourcestring);
  45. typedef struct _io_status_block{
  46. DWORD Status;
  47. ULONG information;
  48. } Io_status_block, *pio_status_block;
  49. typedef NTSTATUS (__stdcall *zwcreatefile) (
  50. Out phandle filehandle,
  51. In Access_mask desiredaccess,
  52. In Pobject_attributes Objectattributes,
  53. Out Pio_status_block Iostatusblock,
  54. In Plarge_integer allocationsize OPTIONAL,
  55. in ULONG fileattributes,
  56. in ULONG shareaccess,
  57. in ULONG createdisposition,
  58. in ULONG createoptions,
  59. in PVOID eabuffer OPTIONAL,
  60. In ULONG ealength);
  61. typedef VOID (Ntapi *pio_apc_routine) (
  62. in PVOID Apccontext,
  63. In Pio_status_block Iostatusblock,
  64. In ULONG Reserved);
  65. typedef NTSTATUS (__stdcall *zwwritefile) (
  66. in HANDLE filehandle,
  67. in HANDLE Event OPTIONAL,
  68. In Pio_apc_routine Apcroutine OPTIONAL,
  69. in PVOID apccontext OPTIONAL,
  70. Out Pio_status_block Iostatusblock,
  71. in PVOID Buffer,
  72. in ULONG Length,
  73. In Plarge_integer Byteoffset OPTIONAL,
  74. In pulong Key OPTIONAL);
  75. typedef NTSTATUS (__stdcall *zwclose) (
  76. In HANDLE HANDLE);
  77. int main ()
  78. {
  79. hinstance Hntdll;
  80. Zwdeletefile Zwdeletefile;
  81. Rtlinitunicodestring rtlinitunicodestring;
  82. ZwCreateFile ZwCreateFile;
  83. Zwwritefile Zwwritefile;
  84. Zwclose Zwclose;
  85. Hntdll = LoadLibrary ("NTDLL");
  86. if (!hntdll)
  87. return 0;
  88. Zwdeletefile = (zwdeletefile) GetProcAddress (Hntdll,"Zwdeletefile");
  89. rtlinitunicodestring = (rtlinitunicodestring) GetProcAddress (Hntdll,"rtlinitunicodestring");
  90. ZwCreateFile = (zwcreatefile) GetProcAddress (Hntdll,"ZwCreateFile");
  91. Zwwritefile = (zwwritefile) GetProcAddress (Hntdll,"Zwwritefile");
  92. Zwclose = (zwclose) GetProcAddress (Hntdll,"Zwclose");
  93. Unicode_string ObjectName;
  94. Rtlinitunicodestring (&objectname,l"//?? C://forzwfiletest.txt ");//Remember this is to have//??  In front of, DDK said.
  95. Object_attributes objectattributes = {
  96. sizeof (object_attributes), //Length
  97. NULL, //RootDirectory
  98. &objectname, //ObjectName
  99. Obj_case_insensitive, //Attributes
  100. 0, //SecurityDescriptor
  101. NULL, //Securityqualityofservice
  102. };
  103. HANDLE hfile;
  104. PVOID content = "Forzwfiletest";
  105. Io_status_block Iostatusblock;
  106. ZwCreateFile (&hfile,
  107. generic_write| synchronize| Generic_read,
  108. &objectattributes,
  109. &iostatusblock,
  110. 0,
  111. File_attribute_normal,
  112. File_share_delete,
  113. File_open_if,
  114. File_synchronous_io_nonalert,
  115. Null
  116. 0);
  117. Zwwritefile (hfile, 0, 0, 0, &iostatusblock, content,, null, NULL);
  118. Zwclose (hfile);
  119. Zwdeletefile (&objectattributes);
  120. FreeLibrary (Hntdll);
  121. return 0;
  122. }

Transferred from: http://blog.csdn.net/cooblily/archive/2007/10/27/1848037.aspx

About calling some ZW series file manipulation functions in WIN32

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.