Page 1 of 1
Create or open a shared memory block
Posted: Fri May 22, 2020 6:48 pm
by Otto
Hello,
Is it possible to create or open a shared memory block like in php.
https://www.php.net/manual/en/function.shmop-open.php
Best regards,
Otto
Re: Create or open a shared memory block
Posted: Sat May 23, 2020 7:03 am
by Antonio Linares
Otto,
For Windows you could use GlobalAlloc()
https://docs.microsoft.com/en-us/window ... lobalalloc
To make it Linux and OSX compatible, then simply use malloc()
Re: Create or open a shared memory block
Posted: Sat May 23, 2020 7:22 am
by Otto
Dear Antonio,
thank you. Would you be so kind to post a little sample.
Best regards,
Otto
Re: Create or open a shared memory block
Posted: Sat May 23, 2020 8:56 am
by Antonio Linares
Code: Select all
#include <windows.h>
#include <hbapi.h>
HB_FUNC( MEMBLOCK )
{
hb_retptr( GlobalAlloc( hb_parnl( 1 ), ( SIZE_T ) hb_parnll( 2 ) ) );
}
Re: Create or open a shared memory block
Posted: Sat May 23, 2020 3:53 pm
by Otto
Dear Antonio,
When I restart the server, I save a password in memory.
The data in the databases are encrypted with this password .
There are no saved passwords on the server hard drive.
I use a Form to insert the PW.
Code: Select all
<?php
$shm_id = shmop_open(0xff3, "c", 0644, 100);
$shm_size = shmop_size($shm_id);
echo "SHM Block mit: ".$shm_size. " Bytes wurde erstellt.\n";
$shm_bytes_written = shmop_write($shm_id, "my key1234567890", 0);
$my_string = shmop_read($shm_id, 0, $shm_size);
echo "Data written: ".$my_string."\n";
shmop_close($shm_id);
?>
The programs read the password like this:
Code: Select all
<?php
$shm_id = shmop_open(0xff3, "c", 0644, 100);
$shm_size = shmop_size($shm_id);
echo "SHM Block mit: ".$shm_size. " Bytes wurde erstellt.\n";
$my_string = shmop_read($shm_id, 0, $shm_size);
echo "Data read(PW): ".$my_string."\n";
shmop_close($shm_id);
?>
Can you please show how to do this.
Sample from WORDPRESS - I would like to use a similar security for my mod harbourPress.
Best regards,
Otto
Code: Select all
function require_wp_db() {
global $wpdb;
// Erstelle einen 100 Byte grossen gemeinsam genutzten Speicherblock
// mit mit der System_ID if 0xff3
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if(!$shm_id) {
echo "Konnte kein gemeinsames Speichersegment erstellen\n";
}
// Hole die Gr�sse des gemeinsamen Speicherblocks
$shm_size = shmop_size($shm_id);
echo "SHM Block mit: ".$shm_size. " Bytes wurde erstellt.\n";
// Den Teststring wieder auslesen
$my_string = shmop_read($shm_id, 0, $shm_size);
if(!$my_string) {
echo "Konnte nicht aus dem gemeinsamen Speicher lesen\n";
}
echo "Die D a t e n im gemeinsamen Speicher waren (PW): |".$my_string."|\n";
// Den Speicherblock l�schen und den gemeinsamen Speicher schliessen
if(!shmop_delete($shm_id)) {
echo "Konnte den gemeinsamen Speicherblock nicht zum L�schen markieren.";
}
shmop_close($shm_id);
require_once( ABSPATH . WPINC . '/wp-db.php' );
if ( file_exists( WP_CONTENT_DIR . '/db.php' ) )
require_once( WP_CONTENT_DIR . '/db.php' );
if ( isset( $wpdb ) ) {
return;
}
$wpdb = new wpdb( DB_USER, $my_string , DB_NAME, DB_HOST );
}
Re: Create or open a shared memory block
Posted: Sun May 24, 2020 6:00 am
by Antonio Linares
Dear Otto,
https://stackoverflow.com/questions/167 ... y-segments
Ok, here we have the full explanation and a working example:
https://nullprogram.com/blog/2016/04/10/
Thinking how to port this into mod_harbour
Re: Create or open a shared memory block
Posted: Sun May 24, 2020 10:13 am
by Otto
Dear Antonio,
thank you so much.
Best regards,
Otto
Re: Create or open a shared memory block
Posted: Mon May 25, 2020 8:57 am
by Antonio Linares
Dear Otto,
I have implemented a first version that is much easier to use (xbase style), you can download it from here:
https://github.com/FiveTechSoft/mod_har ... arbour.dll
https://github.com/FiveTechSoft/mod_har ... harbour.so
The implementation is quite easy:
MWrite( "pwd", "my password" ) // memory write
? MRead( "pwd" ) // memory read
from other browser tab or another user:
? MRead( "pwd" )
There is also a new MErase( "pwd" ) but it is not working fine yet
Re: Create or open a shared memory block
Posted: Mon May 25, 2020 10:21 am
by Otto
Dear Antonio,
Thank you. I will update my mod and test.
This opens so many possibilities.
Best regards,
Otto