Source code for parent.cpp
1: #define WIN32_LEAN_AND_MEAN
2: #include <windows.h>
3: #include <stdio.h>
4: #include <stdlib.h>
5: #include "sharedmem.h"
6:
7: #define MEM_NAME "ParentChildSharedMemory"
8:
9: typedef struct _DATA {
10: int value;
11: char sText[ 50 ];
12: } DATA, *PDATA;
13:
14: HANDLE Run( char* psProgram );
15:
16:
17: void main( void )
18: {
19: SharedMem sm;
20:
21: if ( !sm.Create( sizeof( DATA ), MEM_NAME ) ) {
22: printf( "Unable to create shared memory buffer! Error#: %d\n",
23: sm.ErrorNumber() );
24: exit( EXIT_FAILURE );
25: }
26:
27: PDATA pdata = (PDATA) sm.GetMem();
28:
29: // note that we'll need to be in the same directory...
30: HANDLE hChild = Run( "child.exe" );
31:
32: if ( NULL != hChild ) {
33: // wait for the child process to finish
34: WaitForSingleObject( hChild, INFINITE );
35:
36: // clean up resources
37: CloseHandle( hChild );
38:
39: // print out what was found in the buffer
40: printf( "Parent found the following in shared memory:\n"
41: "Value: %d\nText: %s\n", pdata->value,
42: pdata->sText );
43: }
44: else
45: printf( "Unable to run child: %d\n", GetLastError() );
46: }
47:
48: HANDLE Run( char* psProgram )
49: {
50: STARTUPINFO si;
51: PROCESS_INFORMATION pi;
52: BOOL bStarted;
53:
54: GetStartupInfo( &si );
55:
56: // create the consumer
57: bStarted = CreateProcess( psProgram,
58: NULL, NULL, NULL, FALSE,
59: NORMAL_PRIORITY_CLASS, NULL, NULL,
60: &si, &pi );
61:
62: if ( bStarted )
63: return pi.hProcess;
64: else
65: return NULL;
66: }
67: