Click to See Complete Forum and Search --> : Dev-C++ and DLLs question


GordonFreeman
May 16th, 2005, 08:39 AM
When i'm building a DLL with Dev-C++,how can i tell the linker to create a section shared among all processes?

This can be done in Visual C++ as it follows


// example,a shared char

#pragma data_seg("a_shared_section")

char sharedchar = 0;

#pragma data_seg()

#pragma comment(link,"SECTION/a_shared_section,RWS")

but those directives are not valid in Dev-C++,then how can i do?

j0nas
May 16th, 2005, 04:06 PM
If Dev-C++ (http://www.bloodshed.net/devcpp.html) uses GCC (http://gcc.gnu.org) (which I think it does), you can do something like this:
int foo __attribute__((section ("shared"), shared)) = 0;
See the GCC's online docs (http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#index-g_t_0040code_007bshared_007d-variable-attribute-1817) for further info.

mailtokannan
May 19th, 2005, 10:06 PM
#pragma comment(linker, "/SECTION:.shared,RWS")
#pragma data_seg(".shared")

//Whatever data structure you want to keep, you can keep it here and then you must initialize it.

#pragma data_seg()

-------------------------

But i prefer using CreateFileMapping() and MapViewOfFile() which provides me with more options to do things. If you want to use these inside a DLL, you have to do this when the DLL is loaded in memory, preferably in DLL_PROCESS_ATTACH. Only then you will have one instance of this shared memory.

Sample code to use when the DLL is loaded in memory:


BOOL fInit = FALSE, fIgnore = FALSE;
// Create a named file mapping object.
hMapObject = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security attributes
PAGE_READWRITE, // read/write access
0, // size: high 32-bits
SHMEMSIZE, // size: low 32-bits
"Some_Name"); // name of map object

if (hMapObject == NULL)
return FALSE;

// The first process to attach initializes memory.
fInit = (GetLastError() != ERROR_ALREADY_EXISTS);

// Get a pointer to the file-mapped shared memory.
lpvMem = MapViewOfFile(
hMapObject, // object to map view of
FILE_MAP_WRITE, // read/write access
0, // high offset: map from
0, // low offset: beginning
0); // default: map entire file

if (lpvMem == NULL)
return FALSE;

// Initialize memory if this is the first process.
if(fInit)
memset(lpvMem, '\0', SHMEMSIZE);


You can have seperate methods for setting and getting the shared memory, export them and use it whereever you want.

GordonFreeman
May 20th, 2005, 07:52 AM
ok!! ah ,i see what was the error: it's just the linker name , "linker" instead of "link"

the other method is good too

i think there are other manners for sharing data,for instance you can use
mailslots and named pipes,but the simpler one is the first (preprocessor directives)

you only have to directly use the variables instead of creating files or pipes or mailslots and then reading/writing them,wich is very uncomfortable