读书人

lpvoid 在 win32 api 中的应用

发布时间: 2012-09-11 10:49:03 作者: rapoo

lpvoid 在 win32 api 中的使用


lpvoid在msdn中的描述为:

LPVOID Generic pointer type, equivalent to (void *). Should be used instead of LPSTR.


可以理解为 void *

但是在使用的时候经常感到疑惑。

比如创建线程 传值的时候 会碰到这个类型

HANDLE CreateThread (  SEC_ATTRS SecurityAttributes,  ULONG StackSize,  SEC_THREAD_START StartFunction,  PVOID ThreadParameter,  ULONG CreationFlags,  PULONG ThreadId);



还有 读取内存的时候

BOOL ReadProcessMemory(  HANDLE hProcess,              // handle to the process  LPCVOID lpBaseAddress,        // base of memory area  LPVOID lpBuffer,              // data buffer  SIZE_T nSize,                 // number of bytes to read  SIZE_T * lpNumberOfBytesRead  // number of bytes read);


有时候需要将类型转换成


int m = 10;

(lpvoid)&m;
但是有时候 不需要取地址符号

int m = 10;

(lpvoid)m;


这也是一种情况到底什么时候使用什么情况呢 ? 这也许每个人都思考过我是这样想的(不知道对不对,要是不对,请大家指点一下,感激不尽) 一个函数有传进参数的时候就需要 使用 不取地址的样式就像上面的这个 读取进程内存的函数


BOOL ReadProcessMemory(  HANDLE hProcess,              // handle to the process  LPCVOID lpBaseAddress,        // base of memory area  LPVOID lpBuffer,              // data buffer  SIZE_T nSize,                 // number of bytes to read  SIZE_T * lpNumberOfBytesRead  // number of bytes read);


ParametershProcess [in] Handle to the process whose memory is being read. The handle must have PROCESS_VM_READ access to the process.lpBaseAddress [in] Pointer to the base address in the specified process from which to read. Before any data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for read access. If this is the case, the function proceeds; otherwise, the function fails. lpBuffer [out] Pointer to a buffer that receives the contents from the address space of the specified process.nSize [in] Specifies the requested number of bytes to read from the specified process. lpNumberOfBytesRead [out] Pointer to a variable that receives the number of bytes transferred into the specified buffer. If lpNumberOfBytesRead is NULL, the parameter is ignored


.

传进参数不需要去地址 , 是因为 有时候传进去的时候是局部变量,传进去之后,内存释放了。

加入是传地址,那么就出问题了 ,这时候只适合传 值!

相反,另一种情况,还是这个读取进程内存的函数。

我想将东西从 函数里面读取出来,相信只要基础过关的人都基本上只这时候用什么了 ,

没错 这时候就需要用 取地址的这种形式!

int m = 10;

(lpvoid)&m;

读书人网 >编程

热点推荐