NO

Author Topic: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg  (Read 17705 times)

pablomanmza

  • Guest
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

Code: [Select]
/***************************************************************************************************
#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
     }

/*********************************************************************************************
« Last Edit: August 23, 2012, 09:22:32 PM by Stefan Pendl »

CommonTater

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

Pelles C does not know C++


pablomanmza

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

CommonTater

  • Guest
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.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
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

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
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.
« Last Edit: August 24, 2012, 11:22:51 AM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

pablomanmza

  • Guest
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!

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
In the DLL hinstance variables and declare them exportable:
Code: [Select]
  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:
Code: [Select]
  extern int    __cdecl(dllimport) a;
  extern float __cdecl(dllimport) b;
  extern char __cdecl(dllimport) *c;
Then use them as you like.
I.e.
Code: [Select]
  float f = a + b;
  char str[128];
  strcpy(str, c);
« Last Edit: August 25, 2012, 01:14:35 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Quote
I need to share data between them so I'm using the #pragma data seg() command but It doesn´t work
Code: [Select]
#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
« Last Edit: August 25, 2012, 05:55:56 PM by timovjl »
May the source be with you

pablomanmza

  • Guest
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 !!   :)