Creating shared memory between applications in C#. *.dll perhaps?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NVergunst
    New Member
    • Apr 2007
    • 18

    Creating shared memory between applications in C#. *.dll perhaps?

    hello. First off, I am very new to dll coding (meaning tonight) and I need to be able to share a List<bool> and a List<double> between my main application and other applications that can interface indirectly with it.

    Basically The mian program will initalize the dll and pass in all the values. Then every about 50ms or so, new values will be given to the dll. I made a basic dll that I can send values to a global list in the dll, but my problem comes from I guess windows creates new memory blocks for each instance of the dll or something, so the my main application communicates with the dll, and the test application communicates with the dll, but neither have the same values, so they cannot speak to eachother.

    My problem comes from me never using or implementing anything having to do with shared memory. I am not even sure if I am on the correct track. Would the keyword "stackalloc " have anything to do with this?

    Justto give some more background, basically I developed some hardware that speaks to my application with a raw byte stream. Then the program interprets that byte stream into doubles and bools. So my application uses them fine. However others would like to interface their program with the hardware, but instead of re-decoding the stream, just grab the values from a dll somewhere.

    I can clarify more if neccessary.
  • NVergunst
    New Member
    • Apr 2007
    • 18

    #2
    anybody have any suggestions?

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      To my mind you have confused a number of concepts. I suggest getting back to basics and learning the fundamentals before starting such a project.

      Comment

      • NVergunst
        New Member
        • Apr 2007
        • 18

        #4
        Originally posted by kenobewan
        To my mind you have confused a number of concepts. I suggest getting back to basics and learning the fundamentals before starting such a project.
        I just dont know what it is called that I am searching for. I can make C# applications no problem by themselves. The problem is now others want to interface with this program through the use of gettings values through a dll.

        I have never written a dll, but I just wrote 1 that is basically useless for this application, but just to learn how they were put together. My understanding is that there needs to be shared memory for 1 dll to interface 2 programs. That is all I am asking.

        Believe me in saying I am very proficient in C# basics and intermediate understandings. The actual "what goes on behind the scenes" I know some of, but I am learning more of. This would fall into the last category because it is not a standard thing to do.

        Through googling, I came accross a few references to stackalloc which I have briefly used when interfacing with a byte stream. I thought it might have some applicability to this scenario.

        And the hardware and my software are both already designed/coded. This dll for 3rd party apps, is what I need some help with.

        So again, does anybody know what keyword to search for when trying to share a variable between 2 applications?

        Comment

        • NVergunst
          New Member
          • Apr 2007
          • 18

          #5
          Anybody? I just need a couple keywords to search google for or just a small code snippet example please.

          Comment

          • kenobewan
            Recognized Expert Specialist
            • Dec 2006
            • 4871

            #6
            I believe our misunderstandin g arose as I use visual studio to publish dlls. Has anyone else written dlls directly?

            Comment

            • NVergunst
              New Member
              • Apr 2007
              • 18

              #7
              if it can be done in visual studio, I am all ears. That would be the new class library for a project correct?

              Does visual studio automatically allow applications to share the same data?

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Have you tried storing the information in a database or something where it can be accessed by the various applications?

                Comment

                • NVergunst
                  New Member
                  • Apr 2007
                  • 18

                  #9
                  Originally posted by Frinavale
                  Have you tried storing the information in a database or something where it can be accessed by the various applications?
                  No I havent. Actually I had never thought of doing this. My experience with databases is limited to MySQL, so if you have a link to an example I would appreciate it.

                  Some quick questions you might be able to answer about databases:

                  1) WIll they update fast enough? I need about 18-50mS update times probably around 30-50 variables of bool/double...

                  2) Will it be useable with code programmed in other languages than C# or .NET as well, or will it be limited?

                  THankyou in advance.

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Originally posted by NVergunst
                    No I havent. Actually I had never thought of doing this. My experience with databases is limited to MySQL, so if you have a link to an example I would appreciate it.

                    Some quick questions you might be able to answer about databases:

                    1) WIll they update fast enough? I need about 18-50mS update times probably around 30-50 variables of bool/double...

                    2) Will it be useable with code programmed in other languages than C# or .NET as well, or will it be limited?

                    THankyou in advance.
                    I also have limited knowledge of using databases but I remember at one time I generated a few hundred threads which added information to the database and after less than a second of running it had added over a thousand entries.

                    I suggest creating a small test program to make sure that your database can handle the work load your program will be placing on it. My feeling is that it should be able to do what you're looking for, but I'm not 100% on that.

                    I don't really have an example for you on how to solve your specific problem but I'd recommend storing the information that needs to be shared between the applications into a table which every program can use.

                    Databases can be used in any programming language that supports them. I'm sure there's even a way to connect to them using C++, and I know that you can in Java...There's even a way to connect to them using Cobol.

                    -Frinny

                    Comment

                    • Motoma
                      Recognized Expert Specialist
                      • Jan 2007
                      • 3236

                      #11
                      You may be able to get away with using a static declaration of your variables in your DLL.

                      If not, you should probably do a Google on C# IPC.

                      Comment

                      • TRScheel
                        Recognized Expert Contributor
                        • Apr 2007
                        • 638

                        #12
                        Originally posted by Motoma
                        You may be able to get away with using a static declaration of your variables in your DLL.

                        If not, you should probably do a Google on C# IPC.
                        No, you cant do that.

                        DLL's are loaded into the program at launch. The program itself persists the static instance in ITS memory, not in the DLL.

                        The above ideas of databases would do the job. You might also look into XML, but bear in mind that using multiple programs is akin to having multiple threads, so be prepared to write code that 'waits its turn'.

                        Comment

                        • LMcKenna
                          New Member
                          • Dec 2013
                          • 1

                          #13
                          When Kenobewan said to learn the fundamentals, he was right, though, he could have added what you (and others here) need to learn.

                          1 - There are different types of memory, CODE, DATA, STACK, DYNAMIC (HEAP).

                          2 - DLL's are used for CODE sharing (so the same code being used by several applications, is not loaded multiple times by the O/S). DLL's are not for sharing DATA across applications (though, within an app, one can share data across threads using STATIC variables within a DLL).

                          3 - Using a database can work, but as someone said, it can grow quickly if you are constantly ADDING data (new rows). If you are just UPDATING a set of rows, this can work nicely - though I suggest simple recovery mode to minimize your log file size.

                          4 - Your best bet (assuming no need to permanently store the data) is to use "memory mapped files" which despite its name, is using shared memory in the O/S (with the ability to have it stored to disk for retrieval later if/when all apps stop and restart - this is optional).

                          This ability (memory mapped files in Windows, originally called global shared memory segments in OS/2) has been around for ages. Lots of examples in books and on the web.

                          NOTE: You will need to worry about things like concurrency! Will two apps ever try to update the data? Are multiple data elements "related" such that they need to be updated "together" (since others may be reading data while another is writing it). These things must be considered when you determine how you're going to control (lock/unlock) access. Again, this is basic fundamentals when using "shared" memory.

                          Good luck.
                          Last edited by LMcKenna; Dec 2 '13, 09:26 PM. Reason: additional information.

                          Comment

                          Working...