Pelles C forum

C language => Expert questions => Topic started by: pablomanmza on August 23, 2012, 07:22:48 PM

Title: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg
Post by: pablomanmza on August 23, 2012, 07:22:48 PM
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
     }

/*********************************************************************************************
Title: Re: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg
Post by: CommonTater on August 23, 2012, 07:25:45 PM
That's never going to work in Pelles C ... because it's C++ code. 

Pelles C does not know C++

Title: Re: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg
Post by: 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++
Title: Re: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg
Post by: CommonTater on August 23, 2012, 08:19:06 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.
Title: Re: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg
Post by: Stefan Pendl on August 23, 2012, 09:25:21 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.
Title: Re: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg
Post by: frankie on August 24, 2012, 11:20:27 AM
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.
Title: Re: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg
Post by: pablomanmza on August 24, 2012, 08:05:27 PM
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!
Title: Re: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg
Post by: frankie on August 25, 2012, 01:12:22 PM
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);
Title: Re: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg
Post by: TimoVJL on August 25, 2012, 03:06:38 PM
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
Title: Re: Shared memory segment in a C++ Class Library (DLL) using #pragma data seg
Post by: pablomanmza on August 27, 2012, 02:54:18 PM
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 !!   :)