CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ and WinAPI
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ and WinAPI Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old May 16th, 2005, 08:39 AM
    GordonFreeman GordonFreeman is offline
    Member
     
    Join Date: Mar 2005
    Posts: 87
    GordonFreeman is an unknown quantity at this point (<10)
    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")
    but those directives are not valid in Dev-C++,then how can i do?
    Reply With Quote
      #2    
    Old May 16th, 2005, 04:06 PM
    j0nas j0nas is offline
    Senior Member
     
    Join Date: Aug 2001
    Location: Stockholm, Sweden
    Posts: 1,663
    j0nas is a jewel in the rough (200+)j0nas is a jewel in the rough (200+)j0nas is a jewel in the rough (200+)
    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.
    Reply With Quote
      #3    
    Old May 19th, 2005, 10:06 PM
    mailtokannan mailtokannan is offline
    Member
     
    Join Date: Oct 2001
    Location: Bangalore, India
    Posts: 29
    mailtokannan is an unknown quantity at this point (<10)
    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.
    Reply With Quote
      #4    
    Old May 20th, 2005, 07:52 AM
    GordonFreeman GordonFreeman is offline
    Member
     
    Join Date: Mar 2005
    Posts: 87
    GordonFreeman is an unknown quantity at this point (<10)
    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
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ and WinAPI


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 05:00 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009