gpt4 book ai didi

C:如何使 ArrayList 实现与 UNICODE_STRING 数据兼容?

转载 作者:太空宇宙 更新时间:2023-11-04 03:17:48 25 4
gpt4 key购买 nike

以下代码必须能够将每个进程名称添加到 ArrayList,因为当前名称仍未存储在列表中。 ArrayList 实现的代码来自 this reference,但有一个问题,当将 int 更改为 UNICODE_STRING 数据(在 Element 结构中,导致在线语法错误:

if (e.data == list->elements[index].data) return index;

Error 1 error C2088: '==' : illegal for struct

indexOf() 例程。

那么,如何解决?

代码:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <Winternl.h>

#pragma comment(lib,"ntdll.lib")

typedef struct _SYSTEM_PROCESS_INFO
{
ULONG NextEntryOffset;
ULONG NumberOfThreads;
LARGE_INTEGER Reserved[3];
LARGE_INTEGER CreateTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER KernelTime;
UNICODE_STRING ImageName;
ULONG BasePriority;
HANDLE ProcessId;
HANDLE InheritedFromProcessId;
}SYSTEM_PROCESS_INFO, *PSYSTEM_PROCESS_INFO;

typedef struct
{
UNICODE_STRING data;
}Element;

typedef struct
{
int current;
int size;
int increment_rate;
Element *elements;
}ArrayList;

void initWithSizeAndIncRate(ArrayList *const list, int size, int rate)
{
list->size = size;
list->increment_rate = rate;
list->elements = (Element*)calloc(sizeof(Element), list->size);
list->current = -1;
}

void initWithSize(ArrayList *const list, int size)
{
initWithSizeAndIncRate(list, size, 50);
}

void init(ArrayList *const list)
{
initWithSize(list, 100);
}

void arraryCopy(void *dest, int dIndex, const void* src, int sIndex, int len, int destLen, size_t size)
{
uint8_t *udest = (uint8_t*)dest;
uint8_t *usrc = (uint8_t*)src;
dIndex *= size;
sIndex *= size;
len *= size;
destLen *= size;

if (src != dest)
{
memcpy(&udest[dIndex], &usrc[sIndex], len);
}
else
{
if (dIndex > sIndex)
{
uint8_t *tmp = (uint8_t*)calloc(destLen, size);
memcpy(tmp, &udest[dIndex], (destLen - dIndex));
memcpy(&udest[dIndex], &usrc[sIndex], len);
memcpy(&udest[dIndex + len], tmp, (destLen - dIndex));
free(tmp);
}
else if (sIndex > dIndex)
{
memcpy(&udest[dIndex], &usrc[sIndex], (destLen - sIndex) + 1);
}
else
return;
}
}

void clear(ArrayList *const list)
{
while (list->current >= 0)
{
list->elements[list->current] = (Element){ 0 };
list->current--;
}
}

void wide(ArrayList* const list)
{
list->size += list->increment_rate;
Element *newArr = (Element*)calloc(sizeof(Element), list->size);
arraryCopy(newArr, 0, list->elements, 0, list->current, list->size, sizeof(Element));
free(list->elements);
list->elements = newArr;
}

int add(ArrayList *const list, Element e)
{
if (++list->current < list->size)
{
list->elements[list->current] = e;
return 1;
}
else
{
wide(list);
list->elements[list->current] = e;
return 1;
}
return 0;
}

int indexOf(const ArrayList *const list, Element e)
{
int index = 0;
while (index <= list->current)
{
if (e.data == list->elements[index].data) return index;
index++;
}
return 0;
}

void printElement(const Element *const e)
{
printf("%i ", e->data);
}

void print(const ArrayList *const list)
{
int i;
for (i = 0; i <= list->current; i++)
{
Element e = list->elements[i];
printElement(&e);
}
printf("\n");
}

void clean(ArrayList *list)
{
free(list->elements);
}

int _tmain(int argc, _TCHAR* argv[])
{
NTSTATUS status;
PVOID buffer;
PSYSTEM_PROCESS_INFO spi;
ArrayList list;

init(&list);

buffer = VirtualAlloc(NULL, 1024 * 1024, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

if (!buffer)
{
printf("\nError: Unable to allocate memory for process list (%d)\n", GetLastError());
return -1;
}

printf("\nProcess list allocated at address %#x\n", buffer);
spi = (PSYSTEM_PROCESS_INFO)buffer;

if (!NT_SUCCESS(status = NtQuerySystemInformation(SystemProcessInformation, spi, 1024 * 1024, NULL)))
{
printf("\nError: Unable to query process list (%#x)\n", status);

VirtualFree(buffer, 0, MEM_RELEASE);
return -1;
}

while (spi->NextEntryOffset)
{
printf("\nProcess name: %wZ | Process ID: %d\n", &spi->ImageName, spi->ProcessId);

int i = indexOf(&list, (Element){ spi->ImageName });

if (i > 0)
print("process already in list \n");
else
add(&list, (Element){ spi->ImageName });

spi = (PSYSTEM_PROCESS_INFO)((LPBYTE)spi + spi->NextEntryOffset);
}
VirtualFree(buffer, 0, MEM_RELEASE);

print(&list);
clean(&list);

_getch();

return 0;
}

编辑:

在下面@Johnny Mopp 的回答之后,现在如何正确调用 indexof 例程?我试过这样:

int i = indexOf(&list, (Element){ &spi->ImageName.Buffer });

if (i >= 0)
printf("process already in list \n");
else
add(&list, (Element){ &spi->ImageName.Buffer });

但是 indexof 说 all 已经存在于列表中,这是错误的!


编辑 2:

第一版答案后的错误: IMAGE

最佳答案

UNICODE_STRING 是一个结构。您必须在整个结构上执行 memcmp() 或在 e.data.Buffer 上执行 wcscmp()

int indexOf(const ArrayList *const list, Element e)
{
int index = 0;
while (index <= list->current)
{
// Check same lengths and then do string compare
if (e.data.Length == list->elements[index].data.Length &&
0 == wcsncmp(e.data.Buffer,
list->elements[index].data.Buffer,
list->elements[index].data.Length))
return index;
index++;
}
return 0;
}

//评论后更新

这是对 UNICODE_STRING 进行适当内存管理的全部内容

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <Winternl.h>

#pragma comment(lib,"ntdll.lib")

typedef struct _SYSTEM_PROCESS_INFO
{
ULONG NextEntryOffset;
ULONG NumberOfThreads;
LARGE_INTEGER Reserved[3];
LARGE_INTEGER CreateTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER KernelTime;
UNICODE_STRING ImageName;
ULONG BasePriority;
HANDLE ProcessId;
HANDLE InheritedFromProcessId;
}SYSTEM_PROCESS_INFO, *PSYSTEM_PROCESS_INFO;

typedef struct
{
// Changed to be a pointer
UNICODE_STRING *data;
}Element;

typedef struct
{
int current;
int size;
int increment_rate;
Element *elements;
}ArrayList;

// Duplicate a UNICODE_STRING
UNICODE_STRING * CopyUString(UNICODE_STRING *src)
{
UNICODE_STRING *dest = (UNICODE_STRING *) malloc(sizeof UNICODE_STRING);
dest->Length = src->Length;
dest->MaximumLength = src->MaximumLength;
dest->Buffer = (PWSTR) malloc(sizeof WCHAR * dest->MaximumLength);
memcpy(dest->Buffer, src->Buffer, sizeof WCHAR * dest->MaximumLength);
return dest;
}
// Free a duplicated UNICODE_STRING
void FreeUString(UNICODE_STRING *src)
{
free(src->Buffer);
free(src);
}

void initWithSizeAndIncRate(ArrayList *const list, int size, int rate)
{
list->size = size;
list->increment_rate = rate;
list->elements = (Element*) calloc(sizeof(Element), list->size);
list->current = -1;
}

void initWithSize(ArrayList *const list, int size)
{
initWithSizeAndIncRate(list, size, 50);
}

void init(ArrayList *const list)
{
initWithSize(list, 100);
}

void arraryCopy(void *dest, int dIndex, const void* src, int sIndex, int len, int destLen, size_t size)
{
uint8_t *udest = (uint8_t*) dest;
uint8_t *usrc = (uint8_t*) src;
dIndex *= size;
sIndex *= size;
len *= size;
destLen *= size;

if (src != dest)
{
memcpy(&udest[dIndex], &usrc[sIndex], len);
}
else
{
if (dIndex > sIndex)
{
uint8_t *tmp = (uint8_t*) calloc(destLen, size);
memcpy(tmp, &udest[dIndex], (destLen - dIndex));
memcpy(&udest[dIndex], &usrc[sIndex], len);
memcpy(&udest[dIndex + len], tmp, (destLen - dIndex));
free(tmp);
}
else if (sIndex > dIndex)
{
memcpy(&udest[dIndex], &usrc[sIndex], (destLen - sIndex) + 1);
}
else
return;
}
}

void clear(ArrayList *const list)
{
while (list->current >= 0)
{
FreeUString(list->elements[list->current].data);
list->current--;
}
}

void wide(ArrayList* const list)
{
list->size += list->increment_rate;
Element *newArr = (Element*) calloc(sizeof(Element), list->size);
arraryCopy(newArr, 0, list->elements, 0, list->current, list->size, sizeof(Element));
free(list->elements);
list->elements = newArr;
}

int add(ArrayList *const list, Element *e)
{
if (++list->current < list->size)
{
list->elements[list->current].data = CopyUString(e->data);
return 1;
}
else
{
wide(list);
list->elements[list->current].data = CopyUString(e->data);
return 1;
}
return 0;
}

int indexOf(const ArrayList *const list, Element *e)
{
int index = 0;
while (index <= list->current)
{
// Check same lengths and then do string compare
if (e->data->Length == list->elements[index].data->Length &&
0 == wcsncmp(e->data->Buffer,
list->elements[index].data->Buffer,
list->elements[index].data->Length))
return index;
index++;
}
return 0;
}

void printElement(const Element *const e)
{
wprintf(L"%s ", e->data->Buffer);
}

void print(const ArrayList *const list)
{
int i;
for (i = 0; i <= list->current; i++)
{
Element e = list->elements[i];
printElement(&e);
}
printf("\n");
}

void clean(ArrayList *list)
{
free(list->elements);
}

int _tmain(int argc, _TCHAR* argv [])
{
NTSTATUS status;
PVOID buffer;
PSYSTEM_PROCESS_INFO spi;
ArrayList list;

init(&list);

buffer = VirtualAlloc(NULL, 1024 * 1024, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

if (!buffer)
{
printf("\nError: Unable to allocate memory for process list (%d)\n", GetLastError());
return -1;
}

printf("\nProcess list allocated at address %#x\n", buffer);
spi = (PSYSTEM_PROCESS_INFO) buffer;

if (!NT_SUCCESS(status = NtQuerySystemInformation(SystemProcessInformation, spi, 1024 * 1024, NULL)))
{
printf("\nError: Unable to query process list (%#x)\n", status);

VirtualFree(buffer, 0, MEM_RELEASE);
return -1;
}

Element e;
while (spi->NextEntryOffset)
{
printf("\nProcess name: %wZ | Process ID: %d\n", &spi->ImageName, spi->ProcessId);

e.data = &(spi->ImageName);
int i = indexOf(&list, &e);

if (i > 0)
printf("process already in list \n");
else
add(&list, &e);

spi = (PSYSTEM_PROCESS_INFO) ((LPBYTE) spi + spi->NextEntryOffset);
}
VirtualFree(buffer, 0, MEM_RELEASE);

print(&list);
clean(&list);

_getch();

return 0;
}

关于C:如何使 ArrayList 实现与 UNICODE_STRING 数据兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49765716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com