Shared memory segment in a C++ Class Library (DLL) using #pragma data seg

Started by pablomanmza, August 23, 2012, 07:22:48 PM

Previous topic - Next topic

pablomanmza

Hi !!, I'm trying to communicate to programs using a DLL. I need to share data between them so I'm using the #pragma data seg() command but It doesn´t work. I call the first function, WinINT1, and I write k with the first program, then I read k with the second program calling the secong function, test. But the value of k is not actualised so they are not using the same variable. this seems very simple but I can´t make it work.

Gracias !, from Argentine. :D


/***************************************************************************************************
#include "dll.h"
#include <windows.h>
#include <malloc.h>
#include <memory.h>
#include <math.h>
#include <float.h>
#include <iostream>
#include <fstream> 
#include <stdio.h>       
         
using namespace std;

#pragma data_seg("SHARED")
double k=0;
#pragma data_seg()
#pragma comment(linker, "/section:SHARED,RWS")


//function 1

extern "C" double WINAPI _export WinINT1(double input)
{
       k=input;   
      return(k);
   
   
}
 
//function 2

extern "C" int WINAPI _export test(double x[99], double y[99])
     {
       
       
     
       y[0] = k;
         
    return (0); //0 means no error, other values may be displayed as warnings
     }

/*********************************************************************************************

CommonTater

That's never going to work in Pelles C ... because it's C++ code. 

Pelles C does not know C++


pablomanmza

sorry but I´m not using pelles, I´m using DEV C++..., but my question is a basic programming doubt in c++

CommonTater

Quote from: pablomanmza on August 23, 2012, 07:37:05 PM
sorry but I´m not using pelles, I´m using DEV C++..., but my question is a basic programming doubt in c++

Well, generally this forum is for Pelles C ... perhaps one of the others will answer.

Stefan Pendl

Quote from: pablomanmza on August 23, 2012, 07:37:05 PM
sorry but I´m not using pelles, I´m using DEV C++..., but my question is a basic programming doubt in c++
So you are really better asking this on a C++ forum for DevC++, since this forum is for a different IDE and language.
---
Stefan

Proud member of the UltraDefrag Development Team

frankie

Most important this forum is for a different compiler!  :P
Whichever behaviour could have PellesC with segment pragma probably will be inconsistent with another compiler (In your case MingW I suppose).
Moreover the #pragma behaviour is completely compiler dependent.

Just as suggestion to use a more portable way of sharing variables you could allocate them in the DLL than share them exporting related symbols.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

pablomanmza

Thanks to you all!!

, I've tested the code using pelles (excellent API) and it does work, but...I´m trying to communicate labview with a propietary program wich code I can´t acces. this program provides dll link block that works with c++ . I realised that I should have control on the apps code to use the pragma section and this is not my case. Eventhough I ask frankie about:

Just as suggestion to use a more portable way of sharing variables you could allocate them in the DLL than share them exporting related symbols.

Could you give me an example of this...I should call the same function in the dll from both programs and work with local variables?

Gracias!

frankie

In the DLL hinstance variables and declare them exportable:

  int    __cdecl(dllexport) a = 1;
  float __cdecl(dllexport) b = 2.0;
  char __cdecl(dllexport) *c = "String";


In the other modules, where you need access, declare the variables as extern:

  extern int    __cdecl(dllimport) a;
  extern float __cdecl(dllimport) b;
  extern char __cdecl(dllimport) *c;

Then use them as you like.
I.e.

  float f = a + b;
  char str[128];
  strcpy(str, c);
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

TimoVJL

QuoteI need to share data between them so I'm using the #pragma data seg() command but It doesn´t work
#ifdef __GNUC__
double k __attribute__((section (".shared"), shared)) = 0;
#endif
#ifdef _MSC_VER
#pragma data_seg(".shared")
double k = 0;
#pragma data_seg()
#pragma comment(linker, "/section:.shared,RWS")
#endif
May the source be with you

pablomanmza

timovjl : you must be inmortal or from another planet cause you gave me the correct answer in a few lines.Thank you so much man !!!, Helping us even if we are at such a long distance (Argentina!!!) and even if we don´t known each other make us so powerful !

Muchas gracias !!   :)