Windows Dynamic-Link Libraries 11

 

 

 

 

 

Another Example Using Shared Memory

 

The following is another program example that supposed to use the shared memory. The process uses the GetSharedMem() function implemented by the DLL to read a string from the shared memory. It is started by the parent process above.

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 2: Creating new Win32 C++ console application project in Visual C++ .NET

 

Processes that Use the Shared Memory Program Example 2: Selecting the console and empty Win32 project template

 

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

 

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

 

Next, add the following source code.

 

// Child process

#include <windows.h>

#include <stdio.h>

 

extern "C" void GetSharedMem(LPWSTR lpszBuf, DWORD cchSize);

 

void wmain()

{

    WCHAR cBuf[MAX_PATH];

 

    GetSharedMem(cBuf, MAX_PATH);

 

    wprintf(L"Child process read from shared memory: %s\n", cBuf);

}

 

Next add the path to the DLL/library and the library file name.

 

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

 

Processes that Use the Shared Memory Program Example 2: Telling the linker the library file to be searched for

 

Build the project. Then we need to copy the DLL into the project’s Debug folder so that it can be found during the run-time else the following error screen will be displayed.

 

Processes that Use the Shared Memory Program Example 2: The DLL file cannot be found hence cannot resolve the functions in the program

 

For the sake of demonstration, copy the DLL into the application Debug’s project folder and re-run the project.

 

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

 

The following is the sample output.

 

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

 

 

 

 

< Windows Dynamic Link Library 10 | Windows Dynamic Link Library | Win32 Programming | Windows Dynamic Link Library 12 >