找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 11499|回复: 6

C++使用NtQueryDirectoryFile枚举文件

[复制链接]

4

主题

28

回帖

0

精华

铜牌会员

积分
199
QQ
发表于 2010-7-31 16:15:28 | 显示全部楼层 |阅读模式
话说这个东西支持Unicode和多字节的环境……
其实这东西是翻译以前不知在哪看到过的vb代码来的

封装了一个类:CNtQuery

话说好久没来了……

代码有点长,就发附件吧,对了,话说为什么不能上传7z格式的文件?

枚举文件和文件夹.rar

4.44 KB, 下载次数: 7707

上帝一样掷骰子!

4

主题

28

回帖

0

精华

铜牌会员

积分
199
QQ
 楼主| 发表于 2010-7-31 16:16:37 | 显示全部楼层
补上个代码吧:

  1. //NtQuery.h By 腾袭 2010-7-25

  2. #ifndef _NT_QUERY_H_INCLUDE_
  3. #define _NT_QUERY_H_INCLUDE_ 1

  4. #pragma once

  5. typedef struct _LSA_STRING {
  6.     USHORT Length;
  7.     USHORT MaximumLength;
  8.     PCHAR Buffer;
  9. } LSA_STRING, *PLSA_STRING, ANSI_STRING, *PANSI_STRING;

  10. typedef struct _LSA_UNICODE_STRING {
  11.         USHORT Length;
  12.         USHORT MaximumLength;
  13.         PWSTR  Buffer;
  14. } LSA_UNICODE_STRING, *PLSA_UNICODE_STRING, UNICODE_STRING, *PUNICODE_STRING;

  15. typedef struct _IO_STATUS_BLOCK {
  16.         NTSTATUS Status;
  17.         ULONG Information;
  18. }IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;

  19. typedef struct _OBJECT_ATTRIBUTES
  20. {
  21.         ULONG Length;
  22.         HANDLE RootDirectory;
  23.         PUNICODE_STRING ObjectName;
  24.         ULONG Attributes;
  25.         PVOID SecurityDescriptor;
  26.         PVOID SecurityQualityOfService;
  27. } OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;

  28. typedef struct _FILE_DIRECTORY_INFORMATION {  
  29.         ULONG NextEntryOffset;
  30.         ULONG Unknown;
  31.         LARGE_INTEGER CreationTime;
  32.         LARGE_INTEGER LastAccessTime;
  33.         LARGE_INTEGER LastWriteTime;
  34.         LARGE_INTEGER ChangeTime;
  35.         LARGE_INTEGER EndOfFile;
  36.         LARGE_INTEGER AllocationSize;  
  37.         ULONG FileAttributes;
  38.         ULONG FileNameLength;
  39.         WCHAR FileName[1];
  40. } FILE_DIRECTORY_INFORMATION, *PFILE_DIRECTORY_INFORMATION;

  41. //FileFullDirectoryInformation:
  42. typedef struct _FILE_FULL_DIRECTORY_INFORMATION {
  43.         ULONG NextEntryOffset;
  44.         ULONG Unknown;
  45.         LARGE_INTEGER CreationTime;
  46.         LARGE_INTEGER LastAccessTime;
  47.         LARGE_INTEGER LastWriteTime;
  48.         LARGE_INTEGER ChangeTime;
  49.         LARGE_INTEGER EndOfFile;
  50.         LARGE_INTEGER AllocationSize;
  51.         ULONG FileAttributes;
  52.         ULONG FileNameLength;
  53.         ULONG EaInformationLength;
  54.         WCHAR FileName[1];
  55. } FILE_FULL_DIRECTORY_INFORMATION, *PFILE_FULL_DIRECTORY_INFORMATION;

  56. //FileBothDirectoryInformation:
  57. typedef struct _FILE_BOTH_DIRECTORY_INFORMATION {  
  58.         ULONG NextEntryOffset;
  59.         ULONG Unknown;
  60.         LARGE_INTEGER CreationTime;
  61.         LARGE_INTEGER LastAccessTime;
  62.         LARGE_INTEGER LastWriteTime;
  63.         LARGE_INTEGER ChangeTime;
  64.         LARGE_INTEGER EndOfFile;
  65.         LARGE_INTEGER AllocationSize;
  66.         ULONG FileAttributes;
  67.         ULONG FileNameLength;
  68.         ULONG EaInformationLength;
  69.         UCHAR AlternateNameLength;
  70.         WCHAR AlternateName[12];
  71.         WCHAR FileName[1];
  72. } FILE_BOTH_DIRECTORY_INFORMATION, *PFILE_BOTH_DIRECTORY_INFORMATION;  

  73. //FileNamesInformation:
  74. typedef struct _FILE_NAMES_INFORMATION {
  75.         ULONG NextEntryOffset;
  76.         ULONG Unknown;
  77.         ULONG FileNameLength;
  78.         WCHAR FileName[1];
  79. } FILE_NAMES_INFORMATION, *PFILE_NAMES_INFORMATION;

  80. typedef enum _FILE_INFORMATION_CLASS {
  81.         FileDirectoryInformation = 1,
  82.         FileFullDirectoryInformation, // 2
  83.         FileBothDirectoryInformation, // 3
  84.         FileBasicInformation, // 4 wdm
  85.         FileStandardInformation, // 5 wdm
  86.         FileInternalInformation, // 6
  87.         FileEaInformation, // 7
  88.         FileAccessInformation, // 8
  89.         FileNameInformation, // 9
  90.         FileRenameInformation, // 10
  91.         FileLinkInformation, // 11
  92.         FileNamesInformation, // 12
  93.         FileDispositionInformation, // 13
  94.         FilePositionInformation, // 14 wdm
  95.         FileFullEaInformation, // 15
  96.         FileModeInformation, // 16
  97.         FileAlignmentInformation, // 17
  98.         FileAllInformation, // 18
  99.         FileAllocationInformation, // 19
  100.         FileEndOfFileInformation, // 20 wdm
  101.         FileAlternateNameInformation, // 21
  102.         FileStreamInformation, // 22
  103.         FilePipeInformation, // 23
  104.         FilePipeLocalInformation, // 24
  105.         FilePipeRemoteInformation, // 25
  106.         FileMailslotQueryInformation, // 26
  107.         FileMailslotSetInformation, // 27
  108.         FileCompressionInformation, // 28
  109.         FileObjectIdInformation, // 29
  110.         FileCompletionInformation, // 30
  111.         FileMoveClusterInformation, // 31
  112.         FileQuotaInformation, // 32
  113.         FileReparsePointInformation, // 33
  114.         FileNetworkOpenInformation, // 34
  115.         FileAttributeTagInformation, // 35
  116.         FileTrackingInformation, // 36
  117.         FileIdBothDirectoryInformation, // 37
  118.         FileIdFullDirectoryInformation, // 38
  119.         FileValidDataLengthInformation, // 39
  120.         FileShortNameInformation, // 40
  121.         FileMaximumInformation
  122. } FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS;

  123. #ifndef SYNCHRONIZE
  124. #define SYNCHRONIZE 0x100000
  125. #endif

  126. #ifndef FILE_ANY_ACCESS
  127. #define FILE_ANY_ACCESS 0
  128. #endif

  129. #ifndef FILE_LIST_DIRECTORY
  130. #define FILE_LIST_DIRECTORY 1
  131. #endif

  132. #ifndef FILE_DIRECTORY_FILE
  133. #define FILE_DIRECTORY_FILE 1
  134. #endif

  135. #ifndef FILE_SYNCHRONOUS_IO_NONALERT
  136. #define FILE_SYNCHRONOUS_IO_NONALERT 0x20
  137. #endif

  138. #ifndef FILE_OPEN_FOR_BACKUP_INTENT
  139. #define FILE_OPEN_FOR_BACKUP_INTENT 0x4000
  140. #endif

  141. #ifndef OBJ_CASE_INSENSITIVE
  142. #define OBJ_CASE_INSENSITIVE 0x40
  143. #endif

  144. typedef NTSTATUS (NTAPI *FN_NtQueryDirectoryFile)( IN HANDLE FileHandle,
  145.         IN HANDLE Event OPTIONAL,
  146.         IN PVOID ApcRoutine OPTIONAL, /*PIO_APC_ROUTINE*/
  147.         IN PVOID ApcContext OPTIONAL,
  148.         OUT PIO_STATUS_BLOCK IoStatusBlock,
  149.         OUT PVOID FileInformation,
  150.         IN ULONG FileInformationLength,
  151.         IN FILE_INFORMATION_CLASS FileInformationClass,
  152.         IN BOOLEAN ReturnSingleEntry,
  153.         IN PUNICODE_STRING FileName OPTIONAL,
  154.         IN BOOLEAN RestartScan );

  155. typedef NTSTATUS (NTAPI *FN_NtOpenFile)(
  156.         __out  PHANDLE FileHandle,
  157.         __in   ACCESS_MASK DesiredAccess,
  158.         __in   POBJECT_ATTRIBUTES ObjectAttributes,
  159.         __out  PIO_STATUS_BLOCK IoStatusBlock,
  160.         __in   ULONG ShareAccess,
  161.         __in   ULONG OpenOptions
  162. );

  163. typedef NTSTATUS (NTAPI *FN_NtClose)(
  164.         __in  HANDLE Handle
  165. );

  166. typedef void (NTAPI *FN_RtlInitUnicodeString)(
  167.         __inout   PUNICODE_STRING DestinationString,
  168.         __in_opt  PCWSTR SourceString
  169. );

  170. typedef void (NTAPI *FN_RtlInitAnsiString)(
  171.         __inout PANSI_STRING DestinationString,
  172.         __inout PCSTR SourceString
  173. );

  174. typedef NTSTATUS (NTAPI *FN_RtlAnsiStringToUnicodeString)(
  175.         PUNICODE_STRING DestinationString,
  176.         PANSI_STRING SourceString,
  177.         BOOLEAN AllocateDestinationString
  178. );

  179. typedef NTSTATUS (NTAPI *FN_RtlUnicodeStringToAnsiString)(
  180.         PANSI_STRING DestinationString,
  181.         PUNICODE_STRING SourceString,
  182.         BOOLEAN AllocateDestinationString
  183. );

  184. typedef void (NTAPI *FN_RtlFreeUnicodeString)(
  185.         PUNICODE_STRING UnicodeString
  186. );

  187. typedef void (NTAPI *FN_RtlFreeAnsiString)(
  188.         PANSI_STRING AnsiString
  189. );

  190. class CNtQuery
  191. {
  192. public:
  193.         CNtQuery(void);
  194.         virtual ~CNtQuery(void);
  195.         static bool CNtQuery::GetAddr();
  196.         static bool m_bInit;

  197.         HANDLE FindFirstFile(LPCTSTR strDirectory,PBYTE bytBuffer,ULONG size);
  198.         bool FindNextFile(HANDLE hFind,PBYTE bytBuffer,ULONG size);
  199.         void Enum(LPCTSTR lpszPath);

  200.         NTSTATUS CNtQuery::ANSI2UNICODE(LPCSTR lpBuf, PUNICODE_STRING pUnicodeString, BOOLEAN AllocateDestinationString = TRUE);
  201.         NTSTATUS CNtQuery::UNICODE2ANSI(LPCWSTR lpBuf, PANSI_STRING pAnsiString, BOOLEAN AllocateDestinationString = TRUE);
  202. private:
  203.         static HMODULE m_hNtdll;
  204. protected:
  205.         static FN_NtQueryDirectoryFile fn_NtQueryDirectoryFile;
  206.         static FN_NtOpenFile fn_NtOpenFile;
  207.         static FN_NtClose fn_NtClose;
  208.         static FN_RtlInitUnicodeString fn_RtlInitUnicodeString;
  209.         static FN_RtlInitAnsiString fn_RtlInitAnsiString;
  210.         static FN_RtlAnsiStringToUnicodeString fn_RtlAnsiStringToUnicodeString;
  211.         static FN_RtlUnicodeStringToAnsiString fn_RtlUnicodeStringToAnsiString;
  212.         static FN_RtlFreeUnicodeString fn_RtlFreeUnicodeString;
  213.         static FN_RtlFreeAnsiString fn_RtlFreeAnsiString;
  214. };

  215. #endif
复制代码
上帝一样掷骰子!

4

主题

28

回帖

0

精华

铜牌会员

积分
199
QQ
 楼主| 发表于 2010-7-31 16:16:55 | 显示全部楼层

  1. //NtQuery.cpp By 腾袭 2010-7-25

  2. #include "StdAfx.h"
  3. #include "NtQuery.h"

  4. HMODULE CNtQuery::m_hNtdll = NULL;
  5. FN_NtQueryDirectoryFile CNtQuery::fn_NtQueryDirectoryFile;
  6. FN_NtOpenFile CNtQuery::fn_NtOpenFile;
  7. FN_NtClose CNtQuery::fn_NtClose;
  8. FN_RtlInitUnicodeString CNtQuery::fn_RtlInitUnicodeString;
  9. FN_RtlInitAnsiString CNtQuery::fn_RtlInitAnsiString;
  10. FN_RtlAnsiStringToUnicodeString CNtQuery::fn_RtlAnsiStringToUnicodeString;
  11. FN_RtlUnicodeStringToAnsiString CNtQuery::fn_RtlUnicodeStringToAnsiString;
  12. FN_RtlFreeUnicodeString CNtQuery::fn_RtlFreeUnicodeString;
  13. FN_RtlFreeAnsiString CNtQuery::fn_RtlFreeAnsiString;

  14. bool CNtQuery::m_bInit = false;

  15. bool CNtQuery::GetAddr()
  16. {
  17.         m_hNtdll = GetModuleHandle(_T("ntdll.dll"));
  18.         if (m_hNtdll == NULL)
  19.                 return false;

  20.         fn_NtQueryDirectoryFile = (FN_NtQueryDirectoryFile)GetProcAddress(m_hNtdll,"NtQueryDirectoryFile");
  21.         fn_NtOpenFile = (FN_NtOpenFile)GetProcAddress(m_hNtdll,"NtOpenFile");
  22.         fn_NtClose = (FN_NtClose)GetProcAddress(m_hNtdll,"NtClose");
  23.         fn_RtlInitUnicodeString = (FN_RtlInitUnicodeString)GetProcAddress(m_hNtdll,"RtlInitUnicodeString");
  24.         fn_RtlInitAnsiString = (FN_RtlInitAnsiString)GetProcAddress(m_hNtdll,"RtlInitAnsiString");
  25.         fn_RtlAnsiStringToUnicodeString = (FN_RtlAnsiStringToUnicodeString)GetProcAddress(m_hNtdll,"RtlAnsiStringToUnicodeString");
  26.         fn_RtlUnicodeStringToAnsiString = (FN_RtlUnicodeStringToAnsiString)GetProcAddress(m_hNtdll,"RtlUnicodeStringToAnsiString");
  27.         fn_RtlFreeUnicodeString = (FN_RtlFreeUnicodeString)GetProcAddress(m_hNtdll,"RtlFreeUnicodeString");
  28.         fn_RtlFreeAnsiString = (FN_RtlFreeAnsiString)GetProcAddress(m_hNtdll,"RtlFreeAnsiString");

  29.         if (!(fn_NtQueryDirectoryFile && fn_NtOpenFile && fn_NtClose && fn_RtlInitUnicodeString))
  30.         {
  31.                 return false;
  32.         }
  33.         return true;
  34. }

  35. CNtQuery::CNtQuery(void)
  36. {
  37.         if (!m_bInit)
  38.                 m_bInit = GetAddr();
  39. }


  40. CNtQuery::~CNtQuery(void)
  41. {
  42. }

  43. HANDLE CNtQuery::FindFirstFile(LPCTSTR strDirectory,PBYTE bytBuffer,ULONG size)
  44. {
  45.         if (!m_bInit)
  46.                 return INVALID_HANDLE_VALUE;

  47.     TCHAR strFolder[MAX_PATH];
  48.         OBJECT_ATTRIBUTES obAttr;
  49.     IO_STATUS_BLOCK objIoStatus;
  50.     NTSTATUS ntStatus;
  51.     HANDLE hFind = NULL;
  52.     UNICODE_STRING strUnicode;
  53.    
  54.         wsprintf(strFolder,_T("\\??\\%s"),strDirectory);

  55.         //'初始化Unicode字符串
  56. #ifdef UNICODE
  57.     fn_RtlInitUnicodeString(&strUnicode, strFolder);
  58. #else
  59.         ANSI2UNICODE(strFolder,&strUnicode);
  60. #endif

  61.         memset(&objIoStatus,0,sizeof(IO_STATUS_BLOCK));
  62.         memset(&obAttr,0,sizeof(OBJECT_ATTRIBUTES));

  63.     obAttr.Length = sizeof(OBJECT_ATTRIBUTES); //'初始化OBJECT_ATTRIBUTES结构
  64.     obAttr.Attributes = OBJ_CASE_INSENSITIVE;
  65.     obAttr.ObjectName = &strUnicode; //'需要打开的文件夹路径
  66.         obAttr.RootDirectory = NULL;
  67.     obAttr.SecurityDescriptor = NULL;
  68.     obAttr.SecurityQualityOfService = NULL;
  69.     //'获取文件夹句柄
  70.     ntStatus = fn_NtOpenFile(&hFind,
  71.                         FILE_LIST_DIRECTORY | SYNCHRONIZE | FILE_ANY_ACCESS,
  72.                         &obAttr,
  73.                         &objIoStatus,
  74.                         3,
  75.                         FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT);
  76. #ifndef UNICODE
  77.         //释放内存
  78.         fn_RtlFreeUnicodeString(&strUnicode);
  79. #endif
  80.     if (ntStatus == 0 && hFind != INVALID_HANDLE_VALUE)
  81.         {
  82.                
  83.         //'获取文件夹文件/目录信息,其实这个函数是Kernel32里的FindFirstFile的封装
  84.         ntStatus = fn_NtQueryDirectoryFile(hFind,
  85.                                      NULL,
  86.                                      NULL,
  87.                                      NULL,
  88.                                      &objIoStatus,
  89.                                      bytBuffer,
  90.                                      size,
  91.                                      FileBothDirectoryInformation,
  92.                                      TRUE,
  93.                                      NULL,
  94.                                      NULL);
  95.         if (ntStatus == 0)        //'Nt系列函数一般返回大于零表示成功,而NtQueryDirectoryFile返回零表示成功
  96.                 {
  97.                         //_tprintf(_T("Succeed!\n"));
  98.             return hFind;
  99.                 }else{
  100.             fn_NtClose(hFind);
  101.                 }
  102.         }
  103.         return INVALID_HANDLE_VALUE;
  104. }

  105. bool CNtQuery::FindNextFile(HANDLE hFind,PBYTE bytBuffer,ULONG size)
  106. {
  107.         if (!m_bInit)
  108.                 return false;
  109.     NTSTATUS ntStatus;
  110.     IO_STATUS_BLOCK objIoStatus;
  111.     //'这是Kernel32 FindNextFile的封装,只是是一次把所有文件/目录都取出来了
  112.     ntStatus = fn_NtQueryDirectoryFile(hFind,
  113.                                  NULL,
  114.                                  NULL,
  115.                                  NULL,
  116.                                  &objIoStatus,
  117.                                  bytBuffer,
  118.                                  size,
  119.                                  FileBothDirectoryInformation,
  120.                                  FALSE,
  121.                                  NULL,
  122.                                  NULL);
  123.         //_tprintf(_T("ntStatus\n"));
  124.     return (ntStatus == 0);
  125. }

  126. void CNtQuery::Enum(LPCTSTR lpszPath)
  127. {
  128.         if (!m_bInit)
  129.                 return;
  130. #ifndef UNICODE
  131.         ANSI_STRING asfn,asan;
  132. #endif

  133.     PFILE_BOTH_DIRECTORY_INFORMATION pDir;
  134.     HANDLE hFind;
  135.     LPBYTE bytBuffer;
  136.     wchar_t fileName[_MAX_FNAME];
  137.         wchar_t AlternateName[13];
  138.     TCHAR strPath[MAX_PATH];
  139.     TCHAR strFileName[MAX_PATH];

  140.     ULONG dwDirOffset;
  141.         ULONG iCount = 0;
  142.         ULONG usize = sizeof(FILE_BOTH_DIRECTORY_INFORMATION) + MAX_PATH * 2 - 3;
  143.     bytBuffer = new BYTE[usize];
  144.         //memset(bytBuffer,0,usize);

  145.         lstrcpy(strPath,lpszPath);

  146.     hFind = FindFirstFile(strPath,bytBuffer,usize);        //'获取第一个文件/目录对象
  147.         if (hFind == INVALID_HANDLE_VALUE)
  148.                 return ;
  149.         pDir = (PFILE_BOTH_DIRECTORY_INFORMATION)bytBuffer;
  150.         //获取FILE_BOTH_DIRECTORY_INFORMATION结构,目的是获取FileNameLength和NextEntryOffset数据

  151.     memcpy(fileName, pDir->FileName, pDir->FileNameLength);
  152.         memcpy(AlternateName,pDir->AlternateName,pDir->AlternateNameLength);        //8.3短文件名
  153.         fileName[pDir->FileNameLength / sizeof(wchar_t)] = 0;
  154.         AlternateName[pDir->AlternateNameLength / sizeof(wchar_t)] = 0;                        //这里的长度都是字节数

  155. #ifndef UNICODE
  156.         UNICODE2ANSI(fileName, &asfn);
  157.         UNICODE2ANSI(AlternateName, &asan);
  158.         wsprintf(strFileName,_T("%s%s"),strPath, asfn.Buffer);
  159.         _tprintf(_T("%02d %8s\t%s\n"),pDir->AlternateNameLength, asan.Buffer, strFileName);
  160.         fn_RtlFreeAnsiString(&asfn);
  161.         fn_RtlFreeAnsiString(&asan);
  162. #else
  163.         wsprintf(strFileName,_T("%s%s"),strPath,fileName);
  164.         _tprintf(_T("%02d %8s\t%s\n"),pDir->AlternateNameLength, AlternateName, strFileName);
  165. #endif
  166.        
  167.     delete[] bytBuffer;
  168.        
  169.         usize = (sizeof(FILE_BOTH_DIRECTORY_INFORMATION) + (260 * 2 - 3)) * 0x2000;
  170.         bytBuffer = new BYTE[usize];

  171.         iCount = 1;
  172.     while (FindNextFile(hFind,bytBuffer,usize))        //虽然分配了许多空间了,但是仍可能不能把全部文件列举出来,因此,这里需要进一步测试
  173.         {
  174.         dwDirOffset = 0;
  175.         //遍历缓存
  176.         while(true)
  177.                 {
  178.                         //移动指针
  179.             pDir = (PFILE_BOTH_DIRECTORY_INFORMATION)(bytBuffer + dwDirOffset);
  180.                         //得到FILE_BOTH_DIRECTORY_INFORMATION结构
  181.                        
  182.                         memcpy(fileName, pDir->FileName, pDir->FileNameLength);                                        //文件名
  183.                         memcpy(AlternateName,pDir->AlternateName,pDir->AlternateNameLength);        //8.3短文件名
  184.                         AlternateName[pDir->AlternateNameLength / sizeof(wchar_t)] = 0;                        //这里的长度都是字节数
  185.                         fileName[pDir->FileNameLength / sizeof(wchar_t)] = 0;

  186. #ifndef UNICODE
  187.                         UNICODE2ANSI(fileName, &asfn);
  188.                         UNICODE2ANSI(AlternateName, &asan);
  189.                         wsprintf(strFileName,_T("%s%s"),strPath, asfn.Buffer);
  190.                         _tprintf(_T("%02d %8s\t%s\n"),pDir->AlternateNameLength, asan.Buffer, strFileName);
  191.                         fn_RtlFreeAnsiString(&asfn);
  192.                         fn_RtlFreeAnsiString(&asan);
  193. #else
  194.                         wsprintf(strFileName,_T("%s%s"),strPath,fileName);
  195.                         _tprintf(_T("%02d %8s\t%s\n"),pDir->AlternateNameLength, AlternateName, strFileName);
  196. #endif
  197.                         //这里如果是目录可以递归遍历目录下所有文件/目录,可以自己封装一个递归函数吧

  198.                         iCount++;
  199.             if (pDir->NextEntryOffset == 0)
  200.                                 break;
  201.             dwDirOffset += pDir->NextEntryOffset; //这里指向下一个FILE_BOTH_DIRECTORY_INFORMATION结构在内存中的位置
  202.                 }
  203.         }

  204.         delete[] bytBuffer;
  205.     fn_NtClose(hFind);
  206.         _tprintf(_T("\nfilecount: %d\n"),iCount);
  207. }


  208. //RtlAnsiStringToUnicodeString()
  209. //RtlUnicodeStringToAnsiString()
  210. //当第3个参数为TRUE时,系统将新分配内存,所以要记得调用RtlFreeUnicodeString()和RtlFreeAnsiString()释放掉。

  211. NTSTATUS CNtQuery::ANSI2UNICODE(LPCSTR lpBuf, PUNICODE_STRING pUnicodeString, BOOLEAN AllocateDestinationString)
  212. {
  213.         ANSI_STRING as;
  214.         fn_RtlInitAnsiString( &as, lpBuf );
  215.         return fn_RtlAnsiStringToUnicodeString( pUnicodeString, &as, AllocateDestinationString );
  216. }

  217. NTSTATUS CNtQuery::UNICODE2ANSI(LPCWSTR lpBuf, PANSI_STRING pAnsiString, BOOLEAN AllocateDestinationString)
  218. {
  219.         UNICODE_STRING ns;
  220.         fn_RtlInitUnicodeString( &ns, lpBuf );
  221.         return fn_RtlUnicodeStringToAnsiString( pAnsiString, &ns, AllocateDestinationString );
  222. }
复制代码
上帝一样掷骰子!

4

主题

28

回帖

0

精华

铜牌会员

积分
199
QQ
 楼主| 发表于 2010-7-31 16:17:35 | 显示全部楼层
没有用ntddk.h,所以结构申明有点长
上帝一样掷骰子!

0

主题

34

回帖

0

精华

铜牌会员

积分
44
发表于 2010-8-3 20:48:57 | 显示全部楼层
哇,一句话,小孩没娘,说来话长!~·····

0

主题

25

回帖

0

精华

铜牌会员

积分
61
发表于 2013-2-2 17:51:09 | 显示全部楼层
WIN64上這個還有用嗎

7

主题

414

回帖

1

精华

铂金会员

积分
2173
发表于 2013-3-30 17:08:48 | 显示全部楼层
TengAttack 发表于 2010-7-31 16:16

收下先
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表