Hiyah from Sydney, Australia
this looks great, but Im having trouble compiling with it - I get C2501 'missing storage class or type specifiers' for each of the 'CSemaphore *m_pSync' and 'CSingleLock *m_pLock' declarations in SharedMemory.h (and it goes downhill from there)
My guess would be that I need to tell MFC to include Multithreaded support (I would likely do the same if I was on my other dev platform, Sun Solaris) .. but Im not sure what this means on MFC ...
does it mean that I have to :-
1) #include <afxmt.h>
2) (where ??) StdAfx.h ???
sorry, thats probably a real basic question, maybe by the time Ive spent some time tomorrow (Saturday) on it, I'll have the answer myself ... (just need some backup/reassurance )
ps ... (1) #include <afxmt.h> was gleaned from a quick web-scan .. I'll post back here when Ive tried it - one of the problems with where I live is, its GMT +10. This is well ahead of most of you in the USA, so I like to try and get questions out there early, so as maybe by the next day I'll have answers ...
Thanks for a great job in the classes, Im learning a lot here ..
Well, looks like I did manage to answer my own question - sorry if anyone was inconvenienced by this ..
the solution was as per :-
add a "#include <afxmt>" in StdAfx.h (with all the other 'Afx' includes)
I wonder if there's a way of indicating 'Multithreading support required' when one is using the wizard to start building an MFC program, but its a moot point now ..
It's great that you have a callback in your constructor to initialize the memory at allocation time. You might want to make this callback more C++ friendly either by adding a "user parameter" (an opaque void *) that is passed to the callback. That way, if my CFoo class is creating the shared memory, I can do this:
CSharedMemory* pNewBlock = new CSharedMemory("name", 1024, CFoo::Initializer, this);
/*static*/void CFoo::Initializeer(CSharedMemory* pMem, void* p)
CFoo* This = (CFoo*) p;
// "This" now has access to my CFoo object
Alternatively, if you want to go the "real" OO C++ route, you can make the callback a functor.
Thank you very much for your comment, I thought my CSharedMemory was enough C++ enabled , but probably I was wrong .
It is a lovely idea to pass one more parameter to callback function, however it has nothing to do with C++ at all. I have not done it in my code just because I have never needed to do so.
Nevertheless I can imagine that you could be happy with this functionality, therefore I sent to Chris updated article with your suggestion.
But there is no functor in the whole article, frankly, I see no reason to use "real C++" for such a simple thing as couple of variables somewhere in the memory .
Martin Ziacek wrote:
It is a lovely idea to pass one more parameter to callback function, however it has nothing to do with C++ at all.
Actually, it is a C++ issue, because it's the only way to get a C-style callback to call an object method (as opposed to a standalong function or a static method).
It seems like a minor thing, until you run across one of the few callbacks in the Windows API (SetTimer) that doesn't let you pass a user parameter, and you have to resort to hackery to get an object method called in response to it.
Last Visit: 31-Dec-99 18:00 Last Update: 4-Aug-15 7:15