Généralement, quand on veut échanger des données entre applications exécutées sur un même poste, on utilise un protocole réseau (UDP ou TCP). Cette solution bien que plus simple a mettre en œuvre est la moins performante et la plus lente.
Sous Windows, dans les InterProcess Communications , il existe une API (le File Mapping) qui simplifie le partage d'un espace mémoire entre process différents (ou identique).
Le Code utilisé n'est pas de moi. Vous trouverez sur le site des exemples, la documentation, ainsi que la DLL Managée et les Sources de celle-ci.
Essayé sous Windows 32 et 64 (tests d'Array) sans aucun souci.
Exemples de base. La DLL est a placer dans un dossier "Plugins".
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SharedMemory;
public class memTestserverIPC : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log("SharedMemory server");
var serverIPC = new SharedArray<byte>("MySharedArray", 100); // création d'un Byte (Par exemple) Array commun de 100 éléments.
serverIPC[0] = 123; // premier indice
serverIPC[serverIPC.Length - 1] = 255; // dernier
Debug.Log("server buffer lenght " + serverIPC.Length);
}
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SharedMemory;
public class memTestClientIPC : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log("SharedMemory Client");
var clientIPC = new SharedArray<byte>("MySharedArray"); // ouverture ....
Debug.Log(clientIPC[0]);
Debug.Log(clientIPC[clientIPC.Length - 1]);
Debug.Log("client buffer length " + clientIPC.Length);
}
}
L'IPC en "bref"...
Me rappelle le bon vieux temps du DDE sous VB (très formateur VB, car il fallait compenser les "faiblesses" de l'outil avec L'API Win32 et Cie ).
Un PDF explicatif, avec exemples en C/C++
Un Serveur et un Client en C, pour ceux qui seraient tentés de construire des DLLs natives. Si si...
Code : Tout sélectionner
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
int main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
/*
* We'll name our shared memory segment
* "5678".
* Be careful about the key. For safety, it is better to use ftok()
*/
key = 5678;
/*
* Create the segment.
*/
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
/*
* Now put some things into the memory for the
* other process to read.
*/
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = NULL;
/*
* Finally, we wait until the other process
* changes the first character of our memory
* to '*', indicating that it has read what
* we put there.
*/
while (*shm != '*')
sleep(1);
if (shmdt(shm)==-1)
perror("shmop: shmdt failed");
exit(0);
}
Code : Tout sélectionner
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm, *s;
/*
* We need to get the segment named
* "5678", created by the server.
*/
key = 5678;
/*
* Locate the segment.
*/
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget");
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
/*
* Now read what the server put in the memory.
*/
for (s = shm; *s != NULL; s++)
putchar(*s);
putchar('\n');
/*
* Finally, change the first character of the
* segment to '*', indicating we have read
* the segment.
*/
*shm = '*';
exit(0);
}
Have Fun.