| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ and WinAPI Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Dev-C++ and DLLs question
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 Code:
// example,a shared char
#pragma data_seg("a_shared_section")
char sharedchar = 0;
#pragma data_seg()
#pragma comment(link,"SECTION/a_shared_section,RWS")
|
|
#2
|
|||
|
|||
|
Re: Dev-C++ and DLLs question
If Dev-C++ uses GCC (which I think it does), you can do something like this:
int foo __attribute__((section ("shared"), shared)) = 0;See the GCC's online docs for further info. |
|
#3
|
|||
|
|||
|
Re: Dev-C++ and DLLs question
#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. |
|
#4
|
|||
|
|||
|
Re: Dev-C++ and DLLs question
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 |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|