WMCOPYDATA CLIENT/SERVER IPC CLASS
The CWmCopyDataPeer class is a class I have used in more than one project of mine in the past. It should be enough tested for being used with confidence in other projects as well. If you have suggestions and/or corrections about this implementation, please feel free to write to me.
It is intended expressly for client/server IPC purposes: in fact the server code waits and accepts only one connection from the corresponding peer. Consider that the inherent nature of Windows messages and their handling mechanism does NOT allow the use of this class for inter-machine exchange of data purposes: it is suitable only for inter-process communication on the same machine where authentication isn't strictly a issue. One method that can be implemented in order to provide a basic authentication mechanism in this class could be to derive the Process ID from the provided HWND associated to an incoming message and then to do authorization checks on the user account name and/or on the process privileges associated to that sending process.
TYPE DEFINITION DETAILS
CWmCopyDataPeer ( HINSTANCE hiModuleInstance, BOOL bIsClient, charstring& strChannelName, PFNRECV pfnRecvCallback, DWORD dwRecvCallbackParam );
BOOL Connect ();
void Disconnect ();
int Send ( CONST BYTE* pbBuffer, int nBufferSize, HWND hwnd = NULL, DWORD* pdwRetVal = NULL );
DWORD Broadcast( CONST BYTE* pbBuffer, int nBufferSize );
BOOL FindPeer ( BOOL force = FALSE );
HOW TO USE
Simply instantiate the class and then call the Connect method. Each channel is identified by the class name of its receiving window, which is created and managed from a dedicated thread that is started by the the Connect method.
All my IPC classes use STL under the hood. Be sure to include the following definitions BEFORE the definitions of all my IPC types:
// Stl Stuff.
typedef std::basic_string< CHAR > charstring;
typedef std::vector< charstring > charstring_vector;
typedef std::basic_string< WCHAR > widestring;
typedef std::vector< widestring > widestring_vector;