ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / Process functions / Shared memory area functions
  • Overview
  • How to?
  • Creating a shared memory area
  • Finding out whether a shared memory area already exists
  • Handling the content of a shared memory area through programming
  • Dialog between several applications
  • Automatic notification of modifications
  • Manual synchronization
  • Naming the shared memory areas
  • Managing the share mode
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Overview
WINDEV, WEBDEV and WINDEV Mobile allow you to handle shared memory areas.
The shared memory areas are a communication mechanism between several applications on a given computer. They are used to easily exchange data between a service and the setting application for example.
The functions for managing shared memory areas use the standard APIs of the different operating systems (Windows or Linux) and allow for better interoperability not only between WINDEV, WEBDEV and WINDEV Mobile applications, but also between external applications.
How to?

Creating a shared memory area

The shared memory areas are created and opened by fMemOpen.
The first application that accesses the zone will provoke its creation while the other applications will perform a simple opening.
The application that creates the zone defines its size by supplying a minimum size. The operating system will create a zone whose size is a multiple of the size of the memory pages (4 KB sous in Windows 32 bits for example). The actual size of the memory area is returned by fSize.
// Open the shared memory area
ZoneID is int
ZoneID = fMemOpen("MySharedZone", 50, shareGlobal)
IF ZoneID > 0 THEN
ZoneSize is int
ZoneSize = fSize(ZoneID)
Info(StringBuild("The memory area %1 was opened. Its size is equal to %2 bytes.", ...
ZoneID, ZoneSize))
END
...
// Close the shared memory area
fClose(ZoneID)

Finding out whether a shared memory area already exists

In some cases, it may be useful to find out whether a shared memory area already exists.
For example, an application used to configure a service can check whether the service is properly started: all you have to do is check whether a specific shared memory area was created by the service. If the zone does not exist, an error message can be displayed by the application.
fMemExist is used to find out whether a shared memory area was already created by another application.
// Check the existence of the shared memory area
IF fMemExist("MySharedZone", shareGlobal) = False THEN
Error("Check whether the XXX server was started.")
RETURN
END

Handling the content of a shared memory area through programming

To handle the content of a shared memory area, you can:
  • use the functions for reading and writing in the external files. This solution is recommended for the simple cases (transmission of a string for example).
  • use the automatic serialization of WLanguage (Serialize and Deserialize), then use the functions for reading and writing to the external files in order to transmit the elements. This solution is recommended when transmitting classes whose members correspond to the elements to transmit.
The following functions for managing external files can be used with the shared memory areas:
fReadReads:
  • a block of bytes (characters) in an external file (ANSI or Unicode),
  • the content of an external file (ANSI or Unicode) and assigns it to a memory area.
fReadLineReads a line from an external file (ANSI or UNICODE).
fSeekReturns and modifies the current position in an external file.
fWriteWrites:
  • a character string into an external file.
  • a memory section.
fWriteLineWrites a line into a text file (ANSI or UNICODE).
Dialog between several applications
Two applications can share data using a shared memory area.
Two synchronization mechanisms are available:
  • the automatic notification (by using the callback functions)
  • the manual synchronization via a semaphore.

Automatic notification of modifications

The automatic notification of modifications can be implemented as soon as a shared memory area is opened.
To do so, each application that accesses the memory area must pass the name of a WLanguage procedure as the last parameter to fMemOpen. This procedure will be automatically called whenever the content of the shared memory area is modified.
To wait for the notification to be processed by the other applications (before performing a new modification of the memory area for example), you must use fMemWait.
// Open the memory area in the application 1
ZoneID is int
ZoneID = fMemOpen("SharedZone", 200, shareGlobal, "ModifMemory")
...
// -- Procedure used for callback (application 1)
PROCEDURE ModifMemory(ModifZoneName is string, ModifZoneID is int)
Info(StringBuild("The memory area %1 was modified.", ModifZoneName))
// Open the memory area in the application 2
ZoneID is int
ZoneID = fMemOpen("SharedZone", 200, shareGlobal, "ModifMemory")
// Write into the zone
fWrite(ZoneID, "Hello, I am the application 2.")
// Wait for the validation of "Info" of the "ModifMemory" procedure in the application 1
fMemWait(ZoneID)
// Second write operation into the zone
fWrite(ZoneID, "New information.")
...

Manual synchronization

The manual synchronization can be performed according to the following principle:
  • The applications open the shared memory area and the dialog semaphore.
  • The first application "takes" the semaphore (SemaphoreStart).
  • The first application writes its data to the memory area.
  • The first application frees the semaphore (SemaphoreEnd).
  • The second application "takes" the semaphore.
  • The second application reads the data written by the first application.
  • The second application frees the semaphore.
Remark: If the two applications want to exchange data, two semaphores must be used to insure the regulation.
Tip: The manual synchronization will be preferably used when:
  • one of the two applications is not written in WLanguage.
  • the mechanism for notification of modifications is not available.
Naming the shared memory areas
Shared memory areas are identified by their name.
Remark: The names of the shared memory areas are case sensitive in Windows and in Linux.

Managing the share mode

The share mode differs according to the versions of the operating systems:
  • Linux, Windows 2000 and earlier: there is a single space for creating the share memory area. The <Share> parameter of fMemOpen is ignored.
  • Windows XP: the <parameter> of fMemOpen has effect if the fast user switching feature is enabled, otherwise it is ignored.
  • Windows Vista and later: the <Share> parameter of fMemOpen is supported. The services and the users are located in a different space. To share a memory area between a service and an application in a user session, the shareGlobal constant must be used.
  • In Terminal Server: the <Share> parameter of fMemOpen is supported. Each session opened on the Terminal Server has a different memory space. Applications running in the same TSE session can share a memory area created with the shareUser constant. Several applications located in different sessions can share a memory area created by the shareGlobal constant.
Related Examples:
WD SharingMemory Training (WINDEV): WD SharingMemory
[ + ] This example explains how to use the function for sharing memory with WINDEV.

Several applications may share data when they are started at the same time. In this case, you may have to use a shared memory zone. This memory zone will allow all the started applications to share some information.
Minimum version required
  • Version 15
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 06/22/2023

Send a report | Local help