CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2010
    Posts
    6

    Shared segment in c#

    Hi,

    Does anyone know how to have a shared segment in a c# dll?? Im trying to share the values of some variables in a dll accross two different exes.
    Theres heaps of stuff on doing it in c++ but can find anything on c#.

    c++ uses:

    pragma# data_seg(".shared")

    Any ideas would be appreciated.

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Shared segment in c#

    Quote Originally Posted by Andre2 View Post
    Hi,

    Does anyone know how to have a shared segment in a c# dll?? Im trying to share the values of some variables in a dll accross two different exes.
    Theres heaps of stuff on doing it in c++ but can find anything on c#.

    c++ uses:

    pragma# data_seg(".shared")

    Any ideas would be appreciated.
    Well...I'm not sure there is a similar concept in .NET. This wouldn't be a limitation of C# but a limitation on the runtime. I think the AppDomain concept might actually physically prevent such a sharing. You could think of alternatives though. When you think about it with C# and .NET we are encouraged to think slightly differently from how we would have in pure native C++ applications. If you approach it from an object-oriented approach what you could have is a class defined in the dll. That class could expose a property that would provide access to the shared data to the executables that use the dll. You would have to synchronise access to the data somehow. If the data is persisted in the database, the database should handle that for you. If the data is volatile then you would need to implement a simple but effective synchronisation mechanism.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Shared segment in c#

    In .net you typically share data via .net remoting, or more recently can use a WCF web service (since a WCF service can be hosted from any exe).

    You can also share data using the traditional interprocess communication techniques such as memory mapped files, named pipes, etc. but you'll need to pinvoke to the appropriate Win32 api's in order to do it this way.

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Shared segment in c#

    Quote Originally Posted by Arjay View Post
    In .net you typically share data via .net remoting, or more recently can use a WCF web service (since a WCF service can be hosted from any exe).

    You can also share data using the traditional interprocess communication techniques such as memory mapped files, named pipes, etc. but you'll need to pinvoke to the appropriate Win32 api's in order to do it this way.
    I would go for WCF hosted in IIS. That way you still just have your DLL with the implementation of the WCF service. Ideally your service should be stateless so you would have to persist any state to something like a database.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Shared segment in c#

    Just wanted to mention that unlike the older web services, WCF doesn't have to be hosted in IIS. WCF can be hosted in IIS, a console app, winforms app, WPF app, etc.

    As such, it can be used as a communication layer between two applications (kind of like pragma# data_seg(".shared") but in an upscale way).

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Shared segment in c#

    Also, you can always use windows API to create shared memory trought P/Invoke.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured