 |
|
 |
Below is Read/Write source and I don't understand the bold-typed if statement's meaning.
For Read, shouldn't it be checking for dw == 0?
For Write, shouldn't it be checking for the max var count?
Download the demo and run it. After the initial window appears, click Read. An error message saying "Cannot read memory:..." will show up even though there is one variable called "Test" exists. Again click Write and enter some text and click OK. Again an error message saying "Cannot write memory:..." will pop up.
It seems no one ever have tested the demo program at all.
Someone explain please.
HR
BOOL CSharedMemory::Write(BYTE *pbData, DWORD dwLength, DWORD dwOffset)
{
BOOL rc = FALSE;
do {
if (!m_bCreated) {
SetLastError(ERROR_OBJECT_NOT_FOUND);
break;
}
m_pLock->Lock();
DWORD dw;
CopyMemory(&dw,m_lpView,sizeof(DWORD));
//test - no variables
if (dw != 0) { // WHY CHECKING FOR NON-ZERO?
SetLastError(ERROR_INVALID_DATA);
break;
} CopyMemory(&dw,m_lpView+sizeof(DWORD),sizeof(DWORD));
if ((dwLength + dwOffset) > (dw-2*sizeof(DWORD))) {
SetLastError(ERROR_BAD_LENGTH);
break;
}
CopyMemory(m_lpView+((2*sizeof(DWORD))+dwOffset),pbData,dwLength);
//at the end set event
CEvent event_m(FALSE,TRUE,m_CsName+CString(_T("_Event")),m_bSecPres ? &m_SecAtr : NULL);
event_m.SetEvent();
rc = TRUE;
} while (FALSE);
if (m_pLock) m_pLock->Unlock();
return rc;
}
/**************************************************************************************/
BOOL CSharedMemory::Read(BYTE *pbData, DWORD dwLength, DWORD dwOffset)
{
BOOL rc = FALSE;
do {
if (!m_bCreated) {
SetLastError(ERROR_OBJECT_NOT_FOUND);
break;
}
m_pLock->Lock();
DWORD dw;
CopyMemory(&dw,m_lpView,sizeof(DWORD));
//test - no variables
if (dw != 0) { // WHY CHECKING FOR NON-ZERO?
SetLastError(ERROR_INVALID_DATA);
break;
}
CopyMemory(&dw,m_lpView+sizeof(DWORD),sizeof(DWORD));
if ((dwLength + dwOffset) > (dw-2*sizeof(DWORD))) {
SetLastError(ERROR_BAD_LENGTH);
break;
}
CopyMemory(pbData,m_lpView+((2*sizeof(DWORD))+dwOffset),dwLength);
rc = TRUE;
} while (FALSE);
if (m_pLock) m_pLock->Unlock();
return rc;
}
|
|
|
|
 |
|
 |
yeah,when i read about this.i don‘t understand too.why??mayber when "if(dw==0){}"to check ERROR_INVALID_DATA.
|
|
|
|
 |
|
 |
Can anyone think of a good example for showing advantages over Naughter's CMemMapFile class(http://www.codeproject.com/win32/cmemmap.asp)?
Naughter continues updating, supporting and helping for using his class. This is a really big point for users of his class.
However Martin's class has its own features so that there must be some good examples that would be harder or much more complicated when using Naughter's class.
HR
|
|
|
|
 |
|
 |
i can't bulid the ptogramm i have error(s)
No thing
|
|
|
|
 |
|
 |
Hello,
First, excellet code
I use your shared memory in a program with 2 threads. At one moment, I stop the thread and I try to free the shared memory so :
if (_SHM != NULL)
{
if ( _SHM->ExistValue ( _SHMName ) )
_SHM->DeleteValue (_SHMName);
delete _SHM;
_SHM = NULL;
}
I must delete the value because when I want to run the thread for a second time I use the same name but with a different size, so If I try to add the value, it give an error (of course )
The problezm is, when I tried to delete the valur, I have an excception error on the line
MoveMemory(moveTo,moveFrom,dwMoveLength);
Can you help me please ???
Fred, idea is the begining
|
|
|
|
 |
|
 |
Hi all
Im familiar with the IPC method of sharing memory
with CreateFileMapping, but how do you share memory
that was already allocated and filled?
for example a driver allocated it,passed the ptr to one process
and another needs to access that buffer.
What if one of these processes is 32bit and the other is 64bit?
thanks, Tom.
|
|
|
|
 |
|
 |
Hi, can anyone tell me something about the mm.h library of C for work with shared memory in Unix?
please
help me
thanks
|
|
|
|
 |
|
 |
This[^] is probably a good place to start.
Steve
|
|
|
|
 |
|
 |
thanks Steve, but i'm seeking for any application, any person than worked with the mm.h library, i need make a proyect about client/server wich the server mounts a shared memory segment (a random numbers linked list ) to several clients can read de list and i need think in the concurency (with semaphores).
Any idea???
i'm taking a course of advanced linux, and is very dificult...i'm programming in C
|
|
|
|
 |
|
 |
sorry, can't help you. It has been 25 years since I did any Unix programming.
Steve
|
|
|
|
 |
|
 |
thanks for the link!
do you know any site or anyone who can know anything about this?
regeards
Mariana
|
|
|
|
 |
|
 |
Hi,
not sure if this is the right forum to ask this, but perhaps someone can calrify this one for me.
I have a shared memory implementation similar to the one outlined here. I'm sharing data between instances of a DLL mapped into different processes.
So I create/open a named shared memory segment, get a handle, and then use the handle to MapViewOfFile and get a pointer to the beginning of the memory segment.
Now suppose I need to reallocate the amount of space that's shared in one of the instances of this DLL.
Would calling realloc on the pointer to the beginning of shared memory segment suffice? Since the value of this pointer could be different in every process, it seems to me that I would need to re-MapViewOfFile in all other instances, or does the Windows HANDLE abstraction take care of this for me.
Any insight would be awesome!
Thanks,
Timur
|
|
|
|
 |
|
 |
would this be easy to do?
thanks
|
|
|
|
 |
|
 |
I examine your function "SetValue" and "GetValue", and you use a CSingleLock to lock the value before read and write, a better way is to support multiple readers and single writers, viz. Readers/Writers problem.
|
|
|
|
 |
|
 |
Hi,
Im wondering does, this also work with pictures and other files?
greetz
|
|
|
|
 |
|
 |
when I use the class in two process A,B,and I use two shared variable dwA and dwB in the shared memory,the blocking happend.
the pseudo codes just like the following
first I run process A
process A:
while(true)
{
waitforvaluechage("dwA");
if(dwA==0xff)
{
setvalue("dwB",0xFF);
break;
}
setvalue("dwB",0x12);
}
then I run process B
process B:
int count=0;
while(true)
{
setvalue("dwA",count);
waitforvaluechange("dwB")
if(dwB==0xff)
break;
count ++;
}
when i run about 10 times,the prg blocking.how can i do?
|
|
|
|
 |
|
 |
Hi,
Your assuming the following:
process A Runs..
Process A Waits for change (dwA)
Process B Runs..
Process B changes (dwA)
Process B waits for change (dwB)
Process A resumes..
Process A changes (dwB)
etc..
But what could possibly happen is:
process A Runs..
Process A Waits for change (dwA)
Process B Runs..
Process B changes (dwA)
OS Scheduler switches to process A
Process A resumes..
Process A changes (dwB)
Process A Waits for change (dwA)
Process B waits for change (dwB)
DEADLOCK
|
|
|
|
 |
|
 |
This is a wonderfull code, the only thing I'll change would be the way you detect some versions of win95 (becose not work)
In the constructor:
...
....
TRACE(_T("osvi.dwMajorVersion = %d\n"),osvi.dwMajorVersion);
TRACE(_T("osvi.dwMinorVersion = %d\n"),osvi.dwMinorVersion);
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) //new line
{
if (osvi.dwMajorVersion >= 3) {
if ((osvi.dwMinorVersion == 51) ||
(osvi.dwMinorVersion == 0) ||
(osvi.dwMinorVersion == 1))
{
m_bSecPres = TRUE;
}
}
}
|
|
|
|
 |
|
 |
It works great on WIndows2000/XP, but on Windows98, sometimes, I mean seldom, the shared memory gets trespassed by other process, or say that the memory contents get altered with no valid reason. For example, I have process A and process B using the shared memory, none of them did any change to the shared memory, but after a while some of the memory contents get modified mystically.
This never happened on Windows 2000/XP, but did from time to time on Windows9x. My 2c, do not use system page file, use a private disk file instead, to do so, give a valid file handle value to the first parameter of "CreateFileMapping".
|
|
|
|
 |
|
 |
Friend of mine uses this class on Win95 and has no problems with altered memory contents.
I designed this class only for WinNT like systems and support for different versions has been added later only because of very small and not important project.
I think your suggestion about using file for sharing memory on Win95 may help, but the real problem lies somewhere else - you are running process which damages that memory accidentally (assuming from your description).
Martin
--------------------------------------------
C'mon we all know computers are experimental devices and should only be used for playing games.
Using them for alternative stuff like business, is clearly not using them for what they are intended.
Colin Davies
|
|
|
|
 |
|
 |
Hello,
Is there any way to use CArchive with this class? It would be slick to be able to Serialize data in in one process and Serialize it out in another.
Ted
|
|
|
|
 |
|
 |
There is no direct way to use CArchive with this class, however you can use 'Direct memory access methods' to store any data at any offset in the shared memory.
Martin
--------------------------------------------
C'mon we all know computers are experimental devices and should only be used for playing games.
Using them for alternative stuff like business, is clearly not using them for what they are intended.
Colin Davies
|
|
|
|
 |
|
 |
I have not compile it myself,sorry!
Please pardon my weak English!
|
|
|
|
 |
|
 |
Hi,
it does not support non-MFC, however, you might be able to cut out all referencies to MFC and replace 'MFC parts' with SDK versions.
Regards,
Martin
|
|
|
|
 |
|
 |
Ok,I am trying!
Thanks a lot!
Please pardon my weak English!
|
|
|
|
 |