-1

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:

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?

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
user34224
  • 1
  • 3
  • Consider using `malloc` or initialize it like an array. – Jerfov2 Nov 07 '18 at 03:20
  • You have to allocate some memory for the buffer. You can't pass just a pointer to nothing and expect to get text back; you have to make the memory available, and it should match the size that you're telling it you're providing. Think about it - your current code says *Take this space I've not made available, and I'm telling you you can put 2048 bytes into that space that doesn't exist*. How exactly would you think that would work? Hint: It would work about as well as holding your empty hands out in front of you and telling someone *Shovel that manure into this bag I'm holding*. – Ken White Nov 07 '18 at 03:29
  • Usually, if you plan to copy the data from the buffer, it would be a local array and you pass the address of that to the function then copy the data that is returned out. – Retired Ninja Nov 07 '18 at 04:10

1 Answers1

1

An LPVOID type is exactly the same as void*, which means a pointer to something which doesn't have a specific type. Also, as it stands right now, lpBuffer isn't pointing at anything because it has not been initialized. 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 lpb[1000]; // Or however much space
LPVOID lpBuffer = (LPVOID) lpb;

To initialize on the heap, use the malloc (memory allocation) function. 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:

#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.

Jerfov2
  • 5,264
  • 5
  • 30
  • 52