My Blog

Condividere dati tra applicazioni mediante DLL

by lupok on domenica 14 settembre 2014 09:00

DLL con sezione dati condivisa.

MakeDLL.h

 

#pragma data_seg (".SHARED")
 
int var = 0; // per la condivisione tra processi la variabile delle
             //essere allocata staticamente ed essere inizializzata
#pragma data_seg()
 
extern "C"
{
   bool __declspec(dllexport__cdecl ReadValue(int* value);
   bool __declspec(dllexport__cdecl WriteValue(const int& value);
}
MakeDLL.cpp

 

// MakeDLL.cpp : Defines the exported functions for the DLL application.
//
 
#include "stdafx.h"
#include "MakeDLL.h"
 
#pragma comment(linker, "/section:.SHARED,RWS")
 
bool ReadValue(intvalue)
{
   *value = var;
 
   return true; 
}
 
bool WriteValue(const intvalue)
{
   var = value;
 
   return true;
}
  
A questo punto mediante 2 processi distinti verifico che la sezione dati circondata dalla direttiva data_seg sia realmente condivisa. Per fare cio' creo due applicazioni console di cui una scrive ed una legge il valore della variabile condivisa:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
 
namespace Reader
{
   class Program
   {
      [DllImport("MakeSharedDLL", CallingConvention = CallingConvention.Cdecl)]
      static extern bool ReadValue(ref int value);
 
      static void Main(string[] args)
      {
         Thread thread = new Thread(() =>
         {
            
 
            while (true)
            {
               try
               {
                  int value = 0;
 
                  if (!ReadValue(ref value))
                     Console.WriteLine("error on read");
                  else
                     Console.WriteLine("value: {0}", value);
               }
               finally
               {
                  Thread.Sleep(1000);
               }
            }
 
         }) { IsBackground = true };
 
         thread.Start();
 
 
         Console.WriteLine("Press ENTER to exit");
         Console.ReadLine();
      }
   }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
 
namespace Writer
{
   class Program
   {
      [DllImport("MakeSharedDLL", CallingConvention = CallingConvention.Cdecl)]
      static extern bool WriteValue(ref int value);
 
      static void Main(string[] args)
      {
         Thread thread = new Thread(() =>
         {
            int value = 0;
 
            while (true)
            {
               try
               {
                  if (!WriteValue(ref value))
                     Console.WriteLine("error on write");
                  else
                     Console.WriteLine("value: {0}", value);
               }
               finally
               {
                  value++;
 
                  Thread.Sleep(1000);
               }
            }
 
         }) { IsBackground = true };
 
         thread.Start();
 
 
         Console.WriteLine("Press ENTER to exit");
         Console.ReadLine();
      }
   }
}

 

MakeSharedDLL.zip

 

ShareDLLBetweenProcess.zip

Blogs Parent Separator My Blog
Author
lupok

My Blog

Tags