C Language Enumeration process and the coding problems encountered

Source: Internet
Author: User

First, understand the associated api:createtoolhelp32snapshot that the enumeration process needs to get a snapshot of the process
CPlain Text view copy code ?
123456 函数原型:HANDLEWINAPI CreateToolhelp32Snapshot(DWORD dwFlags, //用来指定“快照”中需要返回的对象,可以是TH32CS_SNAPPROCESS等DWORDth32ProcessID //一个进程ID号,用来指定要获取哪一个进程的快照,当获取系统进程列表或获取 当前进程快照时可以设为0);//调用成功,返回快照的句柄,调用失败,返回INVALID_HANDLE_VALUE


Process32First getting the first process information
CPlain Text view copy code ?
1234 Process32First(    HANDLEhSnapshot,//CreateToolhelp32Snapshot 返回的句柄    LPPROCESSENTRY32 lppe//保存进程信息的结构);

The PROCESSENTRY32 structure is as follows:
CPlain Text view copy code ?
010203040506070809101112 typedef struct tagPROCESSENTRY32 {    DWORD dwSize; // 结构大小;    DWORD cntUsage; // 此进程的引用计数;    DWORD th32ProcessID; // 进程ID;    DWORD th32DefaultHeapID; // 进程默认堆ID;    DWORD th32ModuleID; // 进程模块ID;    DWORD cntThreads; // 此进程开启的线程计数;    DWORD th32ParentProcessID;// 父进程ID;    LONG pcPriClassBase; // 线程优先权;    DWORD dwFlags; // 保留;    WCHAR szExeFile[MAX_PATH]; // 进程全名;} PROCESSENTRY32;


Process32Nextto get the next process information
Parameters withProcess32First.


Pure C Language Implementation code:
CPlain Text view copy code ?
010203040506070809101112131415161718192021 #include <Windows.h>#include <TlHelp32.h>#include <stdio.h>int main(){        PROCESSENTRY32 processEntry = { 0 };        processEntry.dwSize = sizeof(PROCESSENTRY32);        HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);        if (hProcessSnap == INVALID_HANDLE_VALUE) return -1;                 BOOL bRet = Process32First(hProcessSnap,&processEntry);        while (bRet)        {                printf("ProcessID:%d  %s\n",processEntry.th32ProcessID,processEntry.szExeFile);                bRet = Process32Next(hProcessSnap,&processEntry);        }        CloseHandle(hProcessSnap);        system("pause");        return 0;}


vs2013 Execution Results:
<ignore_js_op>
At this time found a very important problem, and the expected difference Ah, the process name shows garbled! The code does not have any pointers to leaks, nor is it a security insecure issue for printf, habitually dropped into the VC6, and changed the code to a standard compatible with VC6 (that is, the variable definition is placed in front):
CPlain Text view copy code ?
01020304050607080910111213141516171819202122232425 #include <Windows.h>#include <TlHelp32.h>#include <stdio.h>int main(){        HANDLE hProcessSnap;        PROCESSENTRY32 processEntry = { 0 };        BOOL bRet ;        processEntry.dwSize = sizeof(PROCESSENTRY32);        hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);        if (hProcessSnap == INVALID_HANDLE_VALUE) return -1;                bRet= Process32First(hProcessSnap,&processEntry);        while (bRet)        {                printf("ProcessID:%d  %s\n",processEntry.th32ProcessID,processEntry.szExeFile);                bRet = Process32Next(hProcessSnap,&processEntry);        }        CloseHandle(hProcessSnap);        system("pause");        return 0;}


Operation Result:
<ignore_js_op>
Unexpectedly did not appear garbled!!
Wit me at this time consider the coding problem, VC6 encoding is ansi,vs2013 default Unicode,
Then vs Modify the project file properties to change the character set Unicode to multibyte character set (ANSI-based character set):
<ignore_js_op>
Run again under vs2013:
<ignore_js_op>
This is normal and the problem is solved.

The cause of this problem is that Processentry.szexefile is a wchar (wide byte) type array under Unicode encoding
In the multibyte character set is an array of char (narrow byte) types, and if you want to solve this problem without changing the character set of the project, you can convert the Processentry.szexefile obtained under Unicode encoding to ANSI encoding and then output it by encoding the conversion.
Code that successfully enumerates the process without changing the project character set:
CPlain Text view copy code ?
010203040506070809101112131415161718192021222324 #include <Windows.h>#include <TlHelp32.h>#include <stdio.h>int main(){        PROCESSENTRY32 processEntry = { 0 };        processEntry.dwSize = sizeof(PROCESSENTRY32);        HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);        if (hProcessSnap == INVALID_HANDLE_VALUE) return -1;                BOOL bRet = Process32First(hProcessSnap,&processEntry);        while (bRet)        {                intnLength = WideCharToMultiByte(CP_ACP, 0, processEntry.szExeFile, -1, NULL, 0, NULL, NULL);//获取字符长度                char *str = (char *)malloc(sizeof(char)*nLength);                WideCharToMultiByte(CP_ACP, 0, processEntry.szExeFile, -1, str, nLength, NULL, NULL);//编码转换-unicode转ansi                printf("ProcessID:%d  %s\n",processEntry.th32ProcessID,str);                bRet = Process32Next(hProcessSnap,&processEntry);        }        CloseHandle(hProcessSnap);        system("pause");        return 0;}

The results of the operation are as above.

Original address: http://www.c0ks.com/thread-1291-1-1.html

C Language Enumeration process and the coding problems encountered

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.