Processes that Use the Shared Memory Program Example
The following processes use the shared memory provided by the DLL defined above. The first process calls SetSharedMem() to write a string while the second process calls GetSharedMem() to retrieve this string. This process uses the SetSharedMem() function implemented by the DLL to write the string "This is a test string" to the shared memory. It also starts a child process that will read the string from the shared memory.
Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.
Then, add the source file and give it a suitable name.
Next, add the following source code.
// Parent process
extern "C" void SetSharedMem(LPWSTR lpszBuf);
HANDLE CreateChildProcess(LPTSTR szCmdline)
BOOL bFuncRetn = FALSE;
// Set up members of the PROCESS_INFORMATION structure.
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
// Set up members of the STARTUPINFO structure.
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
// Create the child process.
bFuncRetn = CreateProcess(NULL,
szCmdline, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
if (bFuncRetn == 0)
wprintf(L"CreateProcess() failed, error %d\n", GetLastError());
void wmain(int argc, WCHAR *argv)
if (argc == 1)
wprintf(L"Please specify an input file\n");
// Call the DLL function
wprintf(L"\nProcess is writing to shared memory...\n\n");
SetSharedMem(L"This is a test string");
// Start the child process that will read the memory
hProcess = CreateChildProcess(argv);
// Ensure this process is around until the child process terminates
if (INVALID_HANDLE_VALUE != hProcess)
Before building this program we need to tell the compiler where to find the SharedMemDll.lib file and a copy of the previously created DLL, SharedMemDll.dll must be available to this project. Set the library location path and the name as done previously through the projects' properties page.
Take note that your library path should be different and if you copy this project file, the path should be different. So, change accordingly. Then, copy the DLL to the project's Debug folder so that the file can be found physically.
Finally, run the project. The following sample outputs show the application run without and with an argument.