64,651
社区成员
发帖
与我相关
我的任务
分享
HRESULT ThreadA::CaptureImage(para1,para2)
{
...
// memory size
int MemorySize = ImageWidth * ImageHeight;
// share memory name
_bstr_t csShareMemoryName = _T("CaptureImage");
//create share memory
HANDLE hShareMemoryHandle = NULL;
hShareMemoryHandle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, csShareMemoryName);
if (NULL == hShareMemoryHandle)
{
hShareMemoryHandle = ::CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
nInit,
MemorySize,
csShareMemoryName);
DWORD dwCurrentThreadID = GetCurrentThreadId();
CString test_CaptureImage;
test_CaptureImage.Format(_T("### [TID]:%d, memorysize=%d"), long(dwCurrentThreadID), MemorySize);
OutputDebugString(test_CaptureImage);
// Check if create share memory successfully
if (NULL == hShareMemoryHandle)
{
if (pImageData != NULL)
{
delete[] pImageData;
pImageData = NULL;
}
return E_FAIL;
}
}
// Map the sharing memory.
LPVOID pShareMemoryAddress = MapViewOfFile(
hShareMemoryHandle,
FILE_MAP_ALL_ACCESS,
nInit,
nInit,
nInit);
BYTE* pBuffer = reinterpret_cast<BYTE*>(pShareMemoryAddress);
// Fill the sharing memory.
memcpy(pBuffer, pImageData, MemorySize);
// Unmaps a mapped view of a file from the address space of the calling process.
UnmapViewOfFile(pShareMemoryAddress);
if (pImageData != NULL)
{
delete[] pImageData;
pImageData = NULL;
}
...
return S_OK;
}
HRESULT ThreadB::CaptureImage( para1,... )
{
...
// memory size
int MemorySize=GetMemorySize();
// share memory name
_bstr_t csShareMemoryName = _T("CaptureImage");
//open file mapping
HANDLE hShareMemoryHandle = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
FALSE,
csShareMemoryName);
if (NULL == hShareMemoryHandle)
{
return E_FAIL;
}
// Map the sharing memory.
LPVOID pShareMemoryAddress = MapViewOfFile(
hShareMemoryHandle,
FILE_MAP_READ,
nZero,
nZero,
nZero);
if (NULL == pShareMemoryAddress)
{
return E_FAIL;
}
BYTE* pBuffer = new BYTE[MemorySize];
memcpy(pBuffer, pShareMemoryAddress, MemorySize);
UnmapViewOfFile(pShareMemoryAddress);
CloseHandle(hShareMemoryHandle);
...
return S_OK;
}
// Fill the sharing memory.
memcpy(pBuffer, pImageData, MemorySize);