Windows Dynamic-Link Libraries 10

 

 

 

 

 

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.

 

Processes that Use the Shared Memory Program Example: Creating new Win32 C++ console application project in Visual C++ .NET

 

 

Processes that Use the Shared Memory Program Example: Seleting the empty and console application

 

Then, add the source file and give it a suitable name.

 

Processes that Use the Shared Memory Program Example: Adding new C++ source file for C++ source code to the existing C++ project

 

Next, add the following source code.

 

// Parent process

#include <windows.h>

#include <stdio.h>

 

extern "C" void SetSharedMem(LPWSTR lpszBuf);

 

HANDLE CreateChildProcess(LPTSTR szCmdline)

{

   PROCESS_INFORMATION piProcInfo;

   STARTUPINFO siStartInfo;

   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());

      return INVALID_HANDLE_VALUE;

   }

   else

   {

      CloseHandle(piProcInfo.hThread);

      return piProcInfo.hProcess;

   }

}

 

void wmain(int argc, WCHAR *argv[])

{

   HANDLE hProcess;

 

   if (argc == 1)

   {

      wprintf(L"Please specify an input file\n");

      ExitProcess(0);

   }

 

   // 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[1]);

 

   // Ensure this process is around until the child process terminates

   if (INVALID_HANDLE_VALUE != hProcess)

   {

      WaitForSingleObject(hProcess, INFINITE);

      CloseHandle(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.

 

Processes that Use the Shared Memory Program Example: Adding additional libraries to the existing Visual C+= project

 

Processes that Use the Shared Memory Program Example: Adding the library path

 

Processes that Use the Shared Memory Program Example: Browsing the additional library path

 

 

Processes that Use the Shared Memory Program Example: Selecting the library path

 

Processes that Use the Shared Memory Program Example: The additional library path has been set

 

Processes that Use the Shared Memory Program Example: The added additional library path seen in the project property's page

 

Processes that Use the Shared Memory Program Example: Adding additional library, telling the linker the library file that need to be searched

 

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.

 

Processes that Use the Shared Memory Program Example: Copying the DLL file to the current project folder

 

Finally, run the project. The following sample outputs show the application run without and with an argument.

 

Processes that Use the Shared Memory Program Example: A sample console program output in action without any argument

 

Processes that Use the Shared Memory Program Example: A sample console program output in action with cmd.exe as the argument

 

 

 

 

< Windows Dynamic Link Library 9 | Windows Dynamic Link Library | Win32 Programming | Windows Dynamic Link Library 11 >