PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview
  • How to proceed?
  • Creating a shared memory zone
  • Finding out whether a shared memory zone already exists
  • Handling the content of a shared memory zone by programming
  • Dialog between several applications
  • Automatic notification of modifications
  • Manual synchronization
  • Naming the shared memory zones
  • Managing the share mode
  • Correspondence between the name provided to fMemOpen and the opening in C
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Overview
WINDEV, WEBDEV and WINDEV Mobile allow you to handle the shared memory zones.
The shared memory zones 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 zones are using the standard APIs of different operating systems (Windows or Linux) and they facilitate the interoperability between the WINDEV/WEBDEV/WINDEV Mobile applications and the applications developed in other languages.
How to proceed?

Creating a shared memory zone

The shared memory zones 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 zone is returned by fSize.
// Open the shared memory zone
ZoneID is int
ZoneID = fMemOpen("MySharedZone", 50, shareGlobal)
IF ZoneID > 0 THEN
ZoneSize is int
ZoneSize = fSize(ZoneID)
Info(StringBuild("The memory zone %1 was opened. Its size is equal to %2 bytes.", ...
ZoneID, ZoneSize))
END
...
// Close the shared memory zone
fClose(ZoneID)

Finding out whether a shared memory zone already exists

In some cases, it may be useful to find out whether a shared memory zone 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 zone 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 zone was already created by another application.
// Check the existence of the shared memory zone
IF fMemExist("MySharedZone", shareGlobal) = False THEN
Error("Check whether the XXX server was started.")
RETURN
END

Handling the content of a shared memory zone by programming

To handle the content of a shared memory zone, 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 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 the external files can be used with the shared memory zones:
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 zone.
fReadLineReads a line in an external file (in ANSI or UNICODE format).
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 (in ANSI or UNICODE format).
Dialog between several applications
Two applications can share data by using a shared memory zone.
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 zone is opened.
To do so, each application that accesses the memory zone must give the name of a WLanguage procedure in last parameter of fMemOpen. This procedure will be automatically called whenever the content of the shared memory zone is modified.
To wait for the notification to be processed by the other applications (before performing a new modification of the memory zone for example), you must use fMemWait.
// Open the memory zone in the application 1
ZoneID is int
ZoneID = fMemOpen("SharedZone", 200, shareGlobal, "ModifMemory")
...
// -- Procedure used for callback (application 1)
PROCÉDURE ModifMemory(ModifZoneName is string, ModifZoneID is int)
Info(StringBuild("The memory zone %1 was modified.", ModifZoneName))
// Open the memory zone 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 zone and the dialog semaphore.
  • The first application "takes" the semaphore (SemaphoreStart).
  • The first application writes its data into the memory zone.
  • 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.
Note: 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 zones
The shared memory zones are identified by their name.
Note: The names of the shared memory zones 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 zone. The <Share> parameter of fMemOpen is ignored.
  • Windows XP: the <Share> parameter of fMemOpen is effective if the service for quick change of user 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 zone 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 zone created with the shareUser constant. Several applications located in different sessions can share a memory zone created by the shareGlobal constant.
Linux

Correspondence between the name provided to fMemOpen and the opening in C

The two following code examples present the opening of a memory zone (named "myzone") in WLanguage and inC.
// Code in WLanguage
ZoneID is int
ZoneID = fMemOpen("myzone", 1024, shareGlobal)
// Equivalent code in C
char * szZoneName = "myzone";
int nSize;
int nMem;
key_t nKey;
int nAccess = 0666; // Open in read/write

nSize = 1024;
nKey = ftok(szZoneName+sizeof(char),(int) szZoneName[0]);
nMem = shmget(nKey, nSize , nAccess);
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 required version
  • Version 15
This page is also available for…
Comments
Click [Add] to post a comment