Can't get VirtualAllocEx to work.

Hi There, I've been trying to program something that chats in another program online using the program's chat function(which I have found). The function has one parameter - the chat string. I can call createremotethread using data within the program, but I want to use my own string, so I allocate empty memory, write to it, supply the memory location in createremotethread's ipparam. And once all that is finished I free the memory. However, all it helps me do is crash the program with lots of access violations.

1
2
3
4
5
6
7
8
9
10
cout <<"Trying to call chat\n";

LPVOID AllocMem = VirtualAllocEx(hProcess, NULL, 1024, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_WRITECOPY);

string Text = "Test";

WriteProcessMemory(hProcess, AllocMem, &Text, sizeof(Text), NULL);
CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)*Chat function Addess*, AllocMem, 0, NULL);
        
VirtualFreeEx(hProcess, AllocMem, 0, MEM_RELEASE);


Any help would be greatly appreciated. ~ 64Bit
I thought you needed to ask for a minimum of 4K. It's not meant to be used directly unless you have special memory needs. If you want just want routine use, there's HealAlloc. But why use a non-portable call when there's new char[1024]?
kbw, the user is trying to allocate memory in an external process, I would say. This means that the new operator is no good in this case.

bit64, I think there might be a problem with your call to VirtualAllocEx(). You are requesting execute and copy-on-write access. As per http://msdn.microsoft.com/en-us/library/aa366786(VS.85).aspx, that seems to be used only with file mapping objects. See http://msdn.microsoft.com/en-us/library/aa366556(VS.85).aspx. I think you need to use PAGE_READWRITE, assuming you are not writing executable code into the reserved memory.

Then in your call to WriteProcessMemory() you are passing the address of the std::string object, and that is not correct. You need to pass Text.c_str(). The next argument is also incorrect. It has to be Text.size() + 1. If you are wanting to copy the entire std::string object, then remember that internally, the object has the string allocated in the heap in non-contiguous memory, and therefore the approach is bound to fail.

Finally, you are not checking return values. This is bad practice. For example, you must verify if WriteProcessMemory() failed, and if VirtualAllocEx() fails.
Topic archived. No new replies allowed.