Win32 进程操作

2021/4/19 7:28:56

本文主要是介绍Win32 进程操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

创建线程

#include<iostream>
#include<windows.h>

using namespace std;

DWORD WINAPI PROCESST1(LPVOID param);
DWORD WINAPI PROCESST2(LPVOID param);
DWORD WINAPI PROCESST3(LPVOID param);

int main3()
{
    HANDLE Thread1, Thread2, Thread3;
    Thread1 = CreateThread(NULL, 0, PROCESST1, NULL, 0, NULL);
    Thread2 = CreateThread(NULL, 0, PROCESST2, NULL, 0, NULL);
    Thread3 = CreateThread(NULL, 0, PROCESST3, NULL, 0, NULL);
    WaitForSingleObject(Thread1, INFINITE);
    WaitForSingleObject(Thread2, INFINITE);
    WaitForSingleObject(Thread3, INFINITE);
    CloseHandle(Thread1);
    CloseHandle(Thread2);
    CloseHandle(Thread3);
    return 0;
}

DWORD WINAPI PROCESST1(LPVOID param)
{
    while (true)
    {
        cout << "THE Thread1 IS working." << endl;
        Sleep(500);

    }
    return 0;
}

DWORD WINAPI PROCESST2(LPVOID param)
{
    while (true)
    {
        cout << "THE Thread2 IS working.." << endl;
        Sleep(500);
    }
    return 0;
}

DWORD WINAPI PROCESST3(LPVOID param)
{
    while (true)
    {
        cout << "THE Thread3 IS working..." << endl;
        Sleep(500);
    }
    return 0;
}

进程操作:

 

#include<iostream>
#include<windows.h>
#include<TlHelp32.h>

using namespace std;

//DWORD WINAPI PROCESST1(LPVOID param);
//HANDLE mythred = ::CreateThread(NULL, 0, PROCESST1, NULL, 0, NULL);

//建立进程
void Cpro()
{
    STARTUPINFO sw = { 0 };
    PROCESS_INFORMATION pinfo = { 0 };
    CreateProcess(
        L"G:\\CPro\\Project1\\Debug\\Project1.exe",
        NULL,
        NULL,
        NULL,
        FALSE,
        0,
        NULL,
        NULL,
        &sw,
        &pinfo
    );
    CloseHandle(pinfo.hThread);
    CloseHandle(pinfo.hProcess);

};

//打开与关闭进程
void Opro()
{
    HANDLE hproc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, 8096);
    TerminateProcess(hproc, 0);
};

//遍历进程
void Tpro()
{
    HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    PROCESSENTRY32 p32 = { sizeof(PROCESSENTRY32) };

    BOOL flg = Process32First(hsnap,&p32);
    if (flg)
    {
        do
        {
            printf("id:%d name:%ls\n",p32.th32ProcessID,p32.szExeFile);   
        }while (Process32Next(hsnap, &p32));
    }

};

//遍历DLL模块
void Spro() 
{
   
    HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 10808);
    MODULEENTRY32 p32 = { sizeof(MODULEENTRY32) };

    BOOL flg = Module32First(hsnap, &p32);
    if (flg)
    {
        do
        {
            printf("Name:%ls\n", p32.szExePath);
        } while (Module32Next(hsnap, &p32));
    }

};

int main()
{
    Spro();
    return 0;
}

 



这篇关于Win32 进程操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程