Process and Thread Functions--进程线程函数

来源:互联网 发布:人工智能的成功案例 编辑:程序博客网 时间:2024/06/05 21:12

TlsAlloc

The TlsAlloc function allocates(分配) a thread local storage (TLS) index.
原型:
DWORD TlsAlloc(VOID);
DWORD index=TlsAlloc(VOID);

TlsSetValue

The TlsSetValue function stores a value in the calling thread’s thread local storage (TLS) slot for a specified TLS index(这个函数存储一个值到调用它的线程的一个指定的索引的TLS槽中). Each thread of a process has its own slot for each TLS index(进程里面的每个线程都有一个对应索引的TLS槽).
原型:
BOOL TlsSetValue(
DWORD dwTlsIndex, // 由TlsAlloc分配的一个索引
LPVOID lpTlsValue // 需要存储的值
);

TlsGetValue

The TlsGetValue function retrieves(检索) the value in the calling thread’s thread local storage (TLS) slot for a specified TLS index. Each thread of a process has its own slot for each TLS index.

LPVOID TlsGetValue(
DWORD dwTlsIndex // 由TlsAlloc分配的一个索引
);

TlsFree

The TlsFree function releases(释放) a thread local storage (TLS) index, making it available for reuse(使它可以重复使用).
原型:
BOOL TlsFree(
DWORD dwTlsIndex // TLS index to free
);
实例:

#define  MAX_THREAD_COUNT 4DWORD hTlsIndex, dwThreadID;DWORD hThreadID[4] = {0};DWORD dwCount;WCHAR szBuffer[500] = { 0 };PCWSTR szOut1 = TEXT("线程%d终止,用时:%d毫秒。\n");PCWSTR szErr1 = TEXT("读取TLS槽数据时失败!\n");PCWSTR szErr2 = TEXT("写入TLS槽数据时失败!\n");//获取当前时间void _initTime(){    DWORD dwStart;    dwStart = GetTickCount();//从SO启动到现在的毫秒数    if (TlsSetValue(hTlsIndex, &dwStart)==0) //存一个指定的数到指定的TLS索引    {        MessageBox(NULL, szErr2, NULL, MB_OK);    }    return;}//获取用时DWORD _getLostTime(){    DWORD dwTemp;    PVOID pTemp = NULL;    dwTemp = GetTickCount();    pTemp = TlsGetValue(hTlsIndex);    if (pTemp==NULL)    {        MessageBox(NULL, szErr2, NULL, MB_OK);    }    dwTemp = dwTemp - *(PDWORD)pTemp;    return dwTemp;}//线程函数void _tFun(){    DWORD count;    DWORD tID;    _initTime();    //模拟耗时操作    count = 1000 * 10000;    while (count)    {        count--;    }    tID = GetCurrentThreadId();    wsprintf(szBuffer, szOut1, tID, _getLostTime());    MessageBox(NULL, szBuffer, NULL, MB_OK);    return;}int _tmain(int argc, _TCHAR* argv[]){    //通过在进程位数组中申请一个索引    //初始化线程运行时间记录系统    hTlsIndex=TlsAlloc();    dwCount = MAX_THREAD_COUNT;    while (dwCount>0)    {        hThreadID[MAX_THREAD_COUNT-dwCount] = (DWORD)CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)_tFun, NULL, NULL, &dwThreadID);        dwCount--;    }    //等待线程结束    dwCount = MAX_THREAD_COUNT;    while (dwCount--)    {        dwThreadID = hThreadID[MAX_THREAD_COUNT-dwCount];        WaitForSingleObject((HANDLE)dwThreadID, INFINITE);        CloseHandle((HANDLE)dwThreadID);    }    //通过释放线程局部存储索引    //释放时间记录系统占用的资源    return 0;}

OpenProcess

The OpenProcess function returns a handle to an existing process object.
//返回正在运行的进程对象的句柄

原型:
HANDLE OpenProcess(
DWORD dwDesiredAccess, // 渴望得到的访问权限(标志)具体值参看MSDN
BOOL bInheritHandle, // 是否继承句柄
DWORD dwProcessId //进程标示符
);
这里写图片描述

ReadProcessMemory

The ReadProcessMemory function reads memory in a specified process. The entire area to be read must be accessible, or the operation fails.
//读取指定进程的内存,被读取的内存必须是可访问的,否者操作失败

原型:
BOOL ReadProcessMemory(
HANDLE hProcess, //为远程进程的句柄
LPCVOID lpBaseAddress, //用于指明远程进程中读的首地址
LPVOID lpBuffer, //是本地进程中的内存地址 指向要读的数据指针
DWORD nSize, //是需要传送的字节数
LPDWORD lpNumberOfBytesRead
//用于指明实际传送的字节数.当函数返回时,可以查看这个参数的值
);

WriteProcessMemory

The WriteProcessMemory function writes memory in a specified process. The entire area to be written to must be accessible, or the operation fails.
为指定进程的指定内存写入数据,整个写区域必须是可以访问的,否者操作失败。

原型:
BOOL WriteProcessMemory(
HANDLE hProcess, //为远程进程的句柄
LPVOID lpBaseAddress,
// 用于指明远程进程中写的首地址
LPVOID lpBuffer, //是本地进程中的内存地址 指向要写的数据指针
DWORD nSize, // 要写入的字节数
LPDWORD lpNumberOfBytesWritten
// 用于指明实际传送的字节数.当函数返回时,可以查看这个参数的值
);

0 0
原创粉丝点击