[toc]

C语言遍历

1.使用while循环

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

int main(int argv, char* argc[])
{
int a = 10;
while (a < 20)
{
printf("a 的值: %d\n", a);
a++;
}
return 0;
}

2.使用do{}while()循环

do{}while()为先执行后判断

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

int main(int argv, char* argc[])
{
int a = 10;
do
{
printf("a 的值: %d\n", a);
a++;
} while (a < 20);
return 0;
}

3.使用while循环给数组赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main(int argv, char* argc[])
{
int a = 0;
int i[10];
while (a < 20)
{
i[a]=a;
printf("a 的值: %d\n", i[a]);
a++;
}
return 0;
}

4.使用while循环打印数组的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int main(int argv, char* argc[])
{
int a = 0;
int i[20] = { 0 };
while (a < 20)
{

i[a] = a;
a++;
}
a = 0;
while (a < 20)
{
printf("a 的值: %d\n", i[a]);
a++;
}
return 0;
}

5.使用while循环打印指针指向地址的值(数组的方式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <malloc.h>


int main(int argv, char* argc[])
{
int a = 0;
int* i = (int*)malloc(sizeof(int) * 20);
size_t size = _msize(i );
printf("size 的值: %d\n", size);
while (a < 20)
{

i[a] = a;
a++;
}
a = 0;
while (a < 20)
{
printf("a 的值: %d\n", i[a]);
a++;
}
free(i);
return 0;
}

6.使用while循环打印指针指向地址的值(指针的方式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <malloc.h>


int main(int argv, char* argc[])
{
int a = 0;
int* i = (int*)malloc(sizeof(int) * 20);
size_t size = _msize(i);
printf("size 的值: %d\n", size);
while (a < 20)
{

*(i+a) = a;
a++;
}
a = 0;
while (a < 20)
{
printf("a 的值: %d\n", *(i+a));
a++;
}
free(i);
return 0;
}

Windows程序遍历进程

1. Windows程序中通过遍历PE结构体打印进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <Windows.h>
#include <stdio.h>
#include <TlHelp32.h>
int TraversalProcess()
{
PROCESSENTRY32 pe32;

pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hProcessSanp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSanp == INVALID_HANDLE_VALUE)
{
printf("Error Get the Process SnapShot\n");
return -1;
}
BOOL bMore = Process32First(hProcessSanp, &pe32);
while (bMore)
{
printf("Process Name: %ls\t\tProcess ID: %d\n", pe32.szExeFile, pe32.th32ProcessID);
bMore = Process32Next(hProcessSanp, &pe32);
}
CloseHandle(hProcessSanp);
return;
}
int main(int argv, char* argc[])
{
TraversalProcess();
return 0;
}

2.c语言程序使用二维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>

int main(int argv, char* argc[])
{
char arr[2][256] = { 0 };
char arr1[256] = "00000000000000000000000000";
char arr2[256] = "11111111111111111111111111";
memcpy(arr[0], arr1, sizeof(arr1));
memcpy(arr[1], arr2, sizeof(arr2));
printf("%s\n", arr[0]);
printf("%s\n", arr[1]);
return 0;
}

3.c语言程序使用指针分配二维数组地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(int argv, char* argc[])
{
int m = 2, n = 256;
char** arr = (char**)malloc(sizeof(char*)*m);
memset(arr, 0, m);
for (int i = 0; i < m; i++)
{

arr[i] = (char*)malloc(sizeof(char) * n); //分配每个指针所指向的数组
memset(arr[i],0,n);

}

char* arr1 = (char*)"012345678901234567890123456789";
char* arr2 = (char*)"qwertyuiopasdfghjklzxcvbnmqwer";
memcpy(arr[0], arr1, strlen(arr1));
memcpy(arr[1], arr2, strlen(arr2));
printf("%s\n", arr[0]);
printf("%s\n", arr[1]);
for (int i = 0; i < m; i++)
{

free(arr[i]);
arr[i] = NULL;

}
free(arr);
arr = NULL;
return 0;
}

4.Windows程序中将遍历后的进程信息传到数组中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <Windows.h>
#include <stdio.h>
#include <TlHelp32.h>
#include <tchar.h>

typedef struct Process
{
unsigned long* pid;
unsigned long* ppid;
wchar_t name[1024][256];
wchar_t path[1024][256];
int num;
}_Process, * PtrProcess;


int TraversalProcess(void* addr)
{
PROCESSENTRY32 pe32;

pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hProcessSanp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSanp == INVALID_HANDLE_VALUE)
{
printf("Error Get the Process SnapShot\n");
return -1;
}
PtrProcess pInfo = (PtrProcess)addr;
pInfo->num = 0;
BOOL bMore = Process32First(hProcessSanp, &pe32);
while (bMore)
{
//printf("Process Name: %ls\t\tProcess ID: %d\n", pe32.szExeFile, pe32.th32ProcessID);
pInfo->pid[pInfo->num] = (DWORD)pe32.th32ProcessID;
pInfo->ppid[pInfo->num] = (DWORD)pe32.th32ParentProcessID;
memcpy(pInfo->name[pInfo->num], pe32.szExeFile, wcslen(pe32.szExeFile) * 2);
pInfo->num++;
bMore = Process32Next(hProcessSanp, &pe32);
}
CloseHandle(hProcessSanp);
return 0;
}
int main(int argv, char* argc[])
{

PtrProcess PidInfo = (PtrProcess)malloc(sizeof(_Process));
PidInfo->pid = (unsigned long*)malloc(sizeof(unsigned long) * 1024);
PidInfo->ppid = (unsigned long*)malloc(sizeof(unsigned long) * 1024);
int m = 1024, n = 256;
memset(PidInfo->name, 0, sizeof(PidInfo->name));
TraversalProcess(PidInfo);
wprintf(L"PidInfo->num is %d\n", PidInfo->num);
int i = 0;
while (PidInfo->num)
{
printf("%d\t",i);
wprintf(L"Process Name is %ls\t", PidInfo->name[i]);
wprintf(L"Pid is %d\t", PidInfo->pid[i]);
wprintf(L"PPid is %d\t", PidInfo->ppid[i]);
printf("\n");
PidInfo->num--;
i++;
}
return 0;
}


5.Windows程序中将遍历后的进程信息传到数组指针中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <Windows.h>
#include <stdio.h>
#include <TlHelp32.h>
#include <tchar.h>

typedef struct Process
{
unsigned long* pid;
unsigned long* ppid;
wchar_t** name;
wchar_t** path;
int num;
}_Process, * PtrProcess;


int TraversalProcess(void* addr)
{
PROCESSENTRY32 pe32;

pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hProcessSanp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSanp == INVALID_HANDLE_VALUE)
{
printf("Error Get the Process SnapShot\n");
return -1;
}
PtrProcess pInfo = (PtrProcess)addr;
pInfo->num = 0;
BOOL bMore = Process32First(hProcessSanp, &pe32);
while (bMore)
{
//printf("Process Name: %ls\t\tProcess ID: %d\n", pe32.szExeFile, pe32.th32ProcessID);
pInfo->pid[pInfo->num] = (DWORD)pe32.th32ProcessID;
pInfo->ppid[pInfo->num] = (DWORD)pe32.th32ParentProcessID;
memcpy(pInfo->name[pInfo->num], pe32.szExeFile, wcslen(pe32.szExeFile)*2);
pInfo->num++;
bMore = Process32Next(hProcessSanp, &pe32);
}
CloseHandle(hProcessSanp);
return 0;
}
int main(int argv, char* argc[])
{

PtrProcess PidInfo = (PtrProcess)malloc(sizeof(_Process));
PidInfo->pid = (unsigned long*)malloc(sizeof(unsigned long) * 1024);
PidInfo->ppid = (unsigned long*)malloc(sizeof(unsigned long) * 1024);
int m = 1024, n = 256;
PidInfo->name = (wchar_t**)malloc(sizeof(wchar_t*) * m);
memset(PidInfo->name, 0, m);
for (int i = 0; i < m; i++)
{
PidInfo->name[i] = (wchar_t*)malloc(sizeof(wchar_t) * n); //分配每个指针所指向的数组
memset(PidInfo->name[i], 0, n);
}
TraversalProcess(PidInfo);
wprintf(L"PidInfo->num is %d\n", PidInfo->num);
int i = 0;
while (PidInfo->num)
{
printf("%d\t",i);
wprintf(L"Process Name is %ls\t", PidInfo->name[i]);
wprintf(L"Pid is %d\t", PidInfo->pid[i]);
wprintf(L"PPid is %d\t", PidInfo->ppid[i]);
printf("\n");
PidInfo->num--;
i++;
}
for (int i = 0; i < m; i++)
{
free(PidInfo->name[i]);
PidInfo->name[i] = NULL;
}
free(PidInfo->name);
PidInfo->name = NULL;
return 0;
}

遍历进程路径

1.根据pid打印进程路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include <Psapi.h>
#pragma comment (lib,"Psapi.lib")

BOOL DosPathToNtPath(LPTSTR pszDosPath, LPTSTR pszNtPath)
{
TCHAR szDriveStr[500];
TCHAR szDrive[3];
TCHAR szDevName[100];
INT cchDevName;
INT i;

//检查参数
if (!pszDosPath || !pszNtPath)
return FALSE;

//获取本地磁盘字符串
if (GetLogicalDriveStrings(sizeof(szDriveStr), szDriveStr))
{
for (i = 0; szDriveStr[i]; i += 4)
{
if (!lstrcmpi(&(szDriveStr[i]), _T("A:\\")) || !lstrcmpi(&(szDriveStr[i]), _T("B:\\")))
continue;

szDrive[0] = szDriveStr[i];
szDrive[1] = szDriveStr[i + 1];
szDrive[2] = '\0';
if (!QueryDosDevice(szDrive, szDevName, 100))//查询 Dos 设备名
return FALSE;

cchDevName = lstrlen(szDevName);
if (_tcsnicmp(pszDosPath, szDevName, cchDevName) == 0)//命中
{
lstrcpy(pszNtPath, szDrive);//复制驱动器
lstrcat(pszNtPath, pszDosPath + cchDevName);//复制路径

return TRUE;
}
}
}

lstrcpy(pszNtPath, pszDosPath);

return FALSE;
}
//获取进程完整路径
BOOL GetProcessFullPath(DWORD dwPID, TCHAR pszFullPath[MAX_PATH])
{
TCHAR szImagePath[MAX_PATH];
HANDLE hProcess;
if (!pszFullPath)
return FALSE;

pszFullPath[0] = '\0';
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, dwPID);
if (!hProcess)
return FALSE;

if (!GetProcessImageFileName(hProcess, szImagePath, MAX_PATH))
{
CloseHandle(hProcess);
return FALSE;
}

if (!DosPathToNtPath(szImagePath, pszFullPath))
{
CloseHandle(hProcess);
return FALSE;
}

CloseHandle(hProcess);

return TRUE;
}

int main(int argc, char* argv[])
{
TCHAR pszFullPath[MAX_PATH] = { 0 };
unsigned long pid = 41368;//Notepad.exe
GetProcessFullPath(pid, pszFullPath);
_tprintf(_T("%d,%s \r\n"), pid, pszFullPath);
return 0;
}

2.依据Pid循环打印进程路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include <Psapi.h>
#pragma comment (lib,"Psapi.lib")
#include <TlHelp32.h>
BOOL DosPathToNtPath(LPTSTR pszDosPath, LPTSTR pszNtPath)
{
TCHAR szDriveStr[500];
TCHAR szDrive[3];
TCHAR szDevName[100];
INT cchDevName;
INT i;

//检查参数
if (!pszDosPath || !pszNtPath)
return FALSE;

//获取本地磁盘字符串
if (GetLogicalDriveStrings(sizeof(szDriveStr), szDriveStr))
{
for (i = 0; szDriveStr[i]; i += 4)
{
if (!lstrcmpi(&(szDriveStr[i]), _T("A:\\")) || !lstrcmpi(&(szDriveStr[i]), _T("B:\\")))
continue;

szDrive[0] = szDriveStr[i];
szDrive[1] = szDriveStr[i + 1];
szDrive[2] = '\0';
if (!QueryDosDevice(szDrive, szDevName, 100))//查询 Dos 设备名
return FALSE;

cchDevName = lstrlen(szDevName);
if (_tcsnicmp(pszDosPath, szDevName, cchDevName) == 0)//命中
{
lstrcpy(pszNtPath, szDrive);//复制驱动器
lstrcat(pszNtPath, pszDosPath + cchDevName);//复制路径

return TRUE;
}
}
}

lstrcpy(pszNtPath, pszDosPath);

return FALSE;
}
//获取进程完整路径
BOOL GetProcessFullPath(DWORD dwPID, TCHAR pszFullPath[MAX_PATH])
{
TCHAR szImagePath[MAX_PATH];
HANDLE hProcess;
if (!pszFullPath)
return FALSE;

pszFullPath[0] = '\0';
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, dwPID);
if (!hProcess)
return FALSE;

if (!GetProcessImageFileName(hProcess, szImagePath, MAX_PATH))
{
CloseHandle(hProcess);
return FALSE;
}

if (!DosPathToNtPath(szImagePath, pszFullPath))
{
CloseHandle(hProcess);
return FALSE;
}

CloseHandle(hProcess);

return TRUE;
}
typedef struct Process
{
unsigned long* pid;
int size;
}_Process, *PtrProcess;

int TraversalProcess(void* addr)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return NULL;
}
PtrProcess PidInfo = (PtrProcess)addr;
PidInfo->size = 0;
PROCESSENTRY32 pe = { 0 };
pe.dwSize = sizeof(PROCESSENTRY32);
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
{
PidInfo->pid[PidInfo->size] = pe.th32ProcessID;
PidInfo->size++;
}
}
int main(int argc, char* argv[])
{
TCHAR pszFullPath[MAX_PATH] = { 0 };
PtrProcess PidInfo =(PtrProcess)malloc(sizeof(_Process));
PidInfo->pid = (int*)malloc(sizeof(int) * 1024);
TraversalProcess(PidInfo);
int i = 0;
while(i< PidInfo->size)
{
GetProcessFullPath(PidInfo->pid[i], pszFullPath);
_tprintf(_T("%d,%s \r\n"), PidInfo->pid[i], pszFullPath);
i++;
}
free(PidInfo->pid);
free(PidInfo);
PidInfo->pid = NULL;
PidInfo = NULL;
return 0;
}

利用static循环打印进程信息

1 .C语言循环打印static修饰的局部变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <conio.h>

void print()
{
static unsigned long long i = 0;
printf("%llu\n", i);
i++;

}

int main(int argv, char* argc[])
{
while (1)
{
print();
_getch();
}
return 0;
}

2.借助stati修饰局部变量打印进程信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <Windows.h>
#include <stdio.h>
#include <TlHelp32.h>
#include <tchar.h>
#include <conio.h>

int TraversalProcess()
{
PROCESSENTRY32 pe32;

pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hProcessSanp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSanp == INVALID_HANDLE_VALUE)
{
printf("Error Get the Process SnapShot\n");
return -1;
}
BOOL bMore = Process32First(hProcessSanp, &pe32);
int num = 0;
static int numadd = 1;
while (bMore)
{
num++;
if (num == numadd)
{
numadd++;
printf("%d\t", num);
printf("Process Name: %ls\t\tProcess ID: %d\n", pe32.szExeFile, pe32.th32ProcessID);
CloseHandle(hProcessSanp);
return 0;
}
bMore = Process32Next(hProcessSanp, &pe32);
if (bMore == FALSE)return 1;


}
CloseHandle(hProcessSanp);
return 0;
}
int main(int argv, char* argc[])
{
while (1)
{
int i = TraversalProcess();
if (i == 1)return 0;
//_getche();
}
return 0;
}

3 使用static变量与malloc分配内存打印进程信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <Windows.h>
#include <stdio.h>
#include <TlHelp32.h>
#include <tchar.h>

typedef struct Process
{
unsigned long* pid;
unsigned long* ppid;
wchar_t** name;
wchar_t** path;
int num;
}_Process, * PtrProcess;


int TraversalProcess(void* addr)
{
PROCESSENTRY32 pe32;

pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hProcessSanp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSanp == INVALID_HANDLE_VALUE)
{
printf("Error Get the Process SnapShot\n");
return -1;
}
PtrProcess pInfo = (PtrProcess)addr;
int m = pInfo->num;
pInfo->num = 0;
BOOL bMore = Process32First(hProcessSanp, &pe32);
static int numadd = 0;
while (bMore)
{
for (int i = 0; i < numadd; i++)
{
bMore = Process32Next(hProcessSanp, &pe32);
if (bMore == FALSE)
{
CloseHandle(hProcessSanp);
return 1;
}
}
for (int i = 0; i < 10; i++)
{
numadd++;
//printf("Process Name: %ls\t\tProcess ID: %d\n", pe32.szExeFile, pe32.th32ProcessID);
pInfo->pid[pInfo->num] = (DWORD)pe32.th32ProcessID;
pInfo->ppid[pInfo->num] = (DWORD)pe32.th32ParentProcessID;
memcpy(pInfo->name[pInfo->num], pe32.szExeFile, wcslen(pe32.szExeFile)*2);
pInfo->num++;
bMore = Process32Next(hProcessSanp, &pe32);
if (bMore == FALSE)
{
CloseHandle(hProcessSanp);
return 1;
}

}
CloseHandle(hProcessSanp);
return 0;
}
CloseHandle(hProcessSanp);
return 0;
}
int main(int argv, char* argc[])
{

PtrProcess PidInfo = (PtrProcess)malloc(sizeof(_Process));
PidInfo->pid = (unsigned long*)malloc(sizeof(unsigned long) * 10);
PidInfo->ppid = (unsigned long*)malloc(sizeof(unsigned long) * 10);
int m = 10, n = 256;
PidInfo->name = (wchar_t**)malloc(sizeof(wchar_t*) * m);
PidInfo->num = m;
memset(PidInfo->name, 0, sizeof(wchar_t*)*m);
for (int i = 0; i < m; i++)
{
PidInfo->name[i] = (wchar_t*)malloc(sizeof(wchar_t) * n); //分配每个指针所指向的数组
memset(PidInfo->name[i], 0, sizeof(wchar_t)*n);
}
while (1)
{
static count = 0;
int i = TraversalProcess(PidInfo);
//wprintf(L"PidInfo->num is %d\n", PidInfo->num);
for (int i = 0; i < PidInfo->num; i++)
{
printf("%d\t", count++);
wprintf(L"Process Name is %ls\t", PidInfo->name[i]);
wprintf(L"Pid is %d\t", PidInfo->pid[i]);
wprintf(L"PPid is %d\t", PidInfo->ppid[i]);
printf("\n");
memset(PidInfo->name[i], 0, sizeof(wchar_t) * n);
}

if (1 == i)break;
}
for (int i = 0; i < m; i++)
{
free(PidInfo->name[i]);
PidInfo->name[i] = NULL;
}
free(PidInfo->name);
PidInfo->name = NULL;
free(PidInfo->pid); free(PidInfo->ppid); free(PidInfo);
return 0;
}