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
// 用于指明实际传送的字节数.当函数返回时,可以查看这个参数的值
);
- Process and Thread Functions--进程线程函数
- Process and Thread Functions (进程和线程相关API函数)
- Symbian Thread and Process 进程和线程
- Process and Thread 进程和线程
- Android Process and Thread 进程和线程
- Process进程、Thread线程
- process and thread android中的进程与线程概念
- process and thread android中的进程与线程概念
- 进程(Process)和线程(Thread)
- Process & Thread 进程和线程
- 理解process(进程)、thread(线程)
- Debug Support from Process, Thread, and Exception Functions
- 进程(process)和线程(thread).
- Android中的进程(Process)和线程(Thread)
- Android中的进程(Process)和线程(Thread)
- Android中的进程(Process)和线程(Thread)
- 计算机必知必会:进程process与线程thread
- Android task process thread 进程与线程
- Android开发步步为营之55:google广告平台admob接入总结
- HDU 2955 Robberies
- Spring 系列: Spring 框架简介
- C++学习笔记(四)
- 手机适配----资源适配篇(values)
- Process and Thread Functions--进程线程函数
- 通过二维码传输少量文本文件
- C语言的类型转化
- U盘安装win7+CentOS6.3双系统
- 《java入门第一季》之泛型引入
- 客户端JavaScript
- common.css中面向属性的类定义
- KOG注释[Ubuntu 15.10系统]
- 蓝桥杯 九宫重排