[英]How could I initialize LPVOID
I have a function which should return me text.我有一个函数应该返回我的文本。 This is the declaration:这是声明:
typedef bool(WINAPI* GetMenuHelpName)(intptr_t dll, LPVOID lpBuffer, int size);
I suppose my text is in LpBuffer
:我想我的文字在LpBuffer
:
GetMenuHelpName func2 = (GetMenuHelpName)GetProcAddress(hGetProcIDDLL, "GetMenuHelpName");
LPVOID lpBuffer;
func2(instance, lpBuffer, 2048);
I got this error:我收到此错误:
Error C4700 : uninitialized local variable 'lpBuffer' used
How could I initialize lpBuffer
and put my result in a std:string
?我如何初始化lpBuffer
并将我的结果放入std:string
?
An LPVOID
type is exactly the same as void*
, which means a pointer to something which doesn't have a specific type. LPVOID
类型与void*
完全相同,这意味着指向没有特定类型的东西的指针。 Also, as it stands right now, lpBuffer isn't pointing at anything because it has not been initialized.此外,就目前而言, lpBuffer 没有指向任何内容,因为它尚未初始化。 There are two main ways to initialize this pointer: on the heap or on the stack.初始化这个指针有两种主要方式:在堆上或在栈上。 To initalize on the stack, initialize it like a char
array:要在堆栈上初始化,请像char
数组一样初始化它:
char lpb[1000]; // Or however much space
LPVOID lpBuffer = (LPVOID) lpb;
To initialize on the heap, use the malloc
(memory allocation) function.要在堆上初始化,请使用malloc
(内存分配)函数。 This allocates some space somewhere and returns a pointer to it.这会在某处分配一些空间并返回指向它的指针。 Just remember to call free
on it when you're done to give that space back to the OS:只记得打电话free
就可以了,当你做给那个空间回OS:
#include <stdlib.h> // malloc, free
// ...
LPVOID lpBuffer = malloc(1000); // pick your space
// ...
free(lpBuffer); // release the space
As for converting to a std::string
, see this page .至于转换为std::string
,请参阅此页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.