Click here to Skip to main content
15,877,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I want to copy a string data to shared memory multiple times using CopyMemory(). But this call only keeps the last string data and clears the existing one. Ofcourse this is as per the design. But will some one let me know how to copy the string data to shared memory using any other alternate api which should not override the existing data.

I have a std::string which has almost 5Lakh characters in it. When i try to copy that string to shared memory it is crashing

HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, BUF_SIZE, wstring{ L"Local\\StringData_" + to_wstring(GetParentID()) }.data());
				m_pStringBuf = (LPCTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
				CopyMemory((PVOID)m_pStringBuf, st.data(), (st.size()+1) * sizeof(TCHAR));


Instead of copying the st.data() at a time, i want to copy the data when ever i get it so that the string holds less data and no crash occurs.

Any suggestions please.

What I have tried:

Tried self and googled but dint not find right answer.

What is wrong here which is crashing at CopyMemory()

std::string m_strData;
	std::string ch = "s";
	for (long long i = 0; i < 500000; i++)
	{
		m_strData.append(ch);
	}
	long BUF_SIZE = (long)(sizeof(TCHAR) * (m_strData.size() + 1));

	HANDLE hMapFilesize = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(long), { L"Local\\AECData_BufferSize_" });
	long *m_pAECBufferSize = reinterpret_cast<long*>(MapViewOfFile(hMapFilesize, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(long)));
	CopyMemory((PVOID)m_pAECBufferSize, &BUF_SIZE, sizeof(long));

	HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, BUF_SIZE, { L"Local\\AECPropertiesAndLinkages_" });
	LPTSTR m_pAECQuantityBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
	CopyMemory((PVOID)m_pAECQuantityBuf, m_strData.data(), (m_strData.size() + 1) * sizeof(TCHAR));
Posted
Updated 24-May-20 18:59pm
v3
Comments
Sampath579 24-May-20 11:32am    
I could figure out the issue on self finally. :)
KarstenK 24-May-20 13:24pm    
So write it as answer to solve the q&a.

1 solution

Below piece of code works

long BUF_SIZE = (long)((m_strData.size() + 1));

CopyMemory((PVOID)m_pAECQuantityBuf, m_strData.data(), (m_strData.size() + 1));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900