Results 1 to 12 of 12

Thread: VB6 - MMapper, Memory Mapped File Demo

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    VB6 - MMapper, Memory Mapped File Demo

    Background

    People are often scrounging for easy to use IPC alternatives for Visual Basic programs. Windows offers a ton of choices, but unless you can state your requirements it is difficult for somebody else to make a recommendation.

    One obvious route is to use a common disk file shared among your program instances (processes). This really isn't as bad as it sounds, but there is a somewhat lighter-weight alternative to a shared temporary file that often isn't considered.

    Files can be mapped into shared memory, but we can go a step further and "back" such shared memory with the system paging file instead. This thread talks about some simple code to do this in a bare bones fashion.


    MMapper

    MMapper is a simple class using the basic set of API calls required to create, open, read, write, and close such a chunk of shared memory.

    As written, it sets up a BLOB of bytes as shared and then treats it as a single place to write or read data that multiple processes can use. Since you do not get fine control over where this memory is mapped into your processes' address spaces it is normally used along with CopyMemory() calls.

    MMapper handles this by accepting pointer and length arguments for its read and write methods. This means you can pass String variables, Byte arrays, or even UDTs that are flat (do not contain dynamic-length fields).


    The Demo

    The attached demo contains the MMapper class and two projects: Writer and Reader.

    Writer is responsible for creating the mapped file and writing data to it. Reader opens this mapped file (which requires that it is first created by Writer) and reads data from it.

    MMapper creates/opens the mapped file with read/write access, so the fixed roles of Reader and Writer are simply to make the operation easier to understand.

    You could easily have every program (and every instance of each program) try to create the mapped file, and they could all both read and write. It's just a matter of figuring out how you want to coordinate things among all of the running processes, and that part is up to you.

    Here I use a small UDT to pass one Long value and one String * 100 value.


    Enhancements

    It is easy enough to extend MMapper in several ways.

    Some of these involve security options to allow sharing among users. You can also treat the mapped space as several separate items instead of one BLOB. And you can map an actual disk file, providing persistence across sessions.

    See your MSDN Library CDs or Windows SDK for more details.

    This even works on Win9x with a few limitations inherent in the platform (such as no Terminal Services or Fast User Switching or other multi-user support).
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - MMapper, Memory Mapped File Demo

    To answer a PM:

    No, the demo works fine on Windows 95. It is only more broad use of memory mapped files in a multi-user scenario that is a little impaired and the "file" name requirements differ slightly as a result too.

    This is covered fairly clearly on the MSDN CDs, the Windows SDKs, and at the MSDN Library Web site.

  3. #3
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: VB6 - MMapper, Memory Mapped File Demo

    Can this idea use for video or voice (VOIP) chatting via internet/LAN?
    In your previous post of camera view, can we share the video/voice through internet as fast as possible using this technique? (some sore of iPhone/iPad facetime)
    Last edited by Jonney; Oct 23rd, 2012 at 08:02 PM.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - MMapper, Memory Mapped File Demo

    No, it can't work at all over the Internet.

    Even using it between machines on a LAN there are limitations. As it says in the documentation:
    With one important exception, file views derived from a single file-mapping object are coherent, or identical, at a given time. If multiple processes have handles of the same file-mapping object, they see a coherent view of the data when they map a view of the file.

    The exception has to do with remote files. Although CreateFileMapping works with remote files, it does not keep them coherent. For example, if two computers both map a file as writable, and both change the same page, each computer will only see its own writes to the page.
    This is an inter-process communication (IPC) technique and not a network protocol.

    It can't work with any random OS either, whether from Apple or anyone else.

  5. #5
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: VB6 - MMapper, Memory Mapped File Demo

    Quote Originally Posted by dilettante View Post
    No, it can't work at all over the Internet.

    Even using it between machines on a LAN there are limitations. As it says in the documentation:


    This is an inter-process communication (IPC) technique and not a network protocol.

    It can't work with any random OS either, whether from Apple or anyone else.
    I see. I remember I ever created a RAM drive for desktop remote control program at 8 or 10 years ago. The program cashed the screenshots in RAM drive instead of hard disk to speed up screen transfer.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - MMapper, Memory Mapped File Demo

    This isn't related to a RAM drive either.

    Memory-mapped files are entirely different. See:

    Memory-mapped file

    File Mapping

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: VB6 - MMapper, Memory Mapped File Demo

    It dawns on me that this is yet another pretty nifty way to have inter-process communications. Also, using this approach, there's no need to have a hWnd. It could all be done with headless processes.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  8. #8
    Addicted Member
    Join Date
    Jun 2022
    Posts
    239

    Re: VB6 - MMapper, Memory Mapped File Demo

    Hi dilettante,

    could objects be shared between processes using this file mapping technique ? Thanks.

  9. #9
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: VB6 - MMapper, Memory Mapped File Demo

    can use rot?lot?
    like getobject("11.xls")

  10. #10
    Addicted Member
    Join Date
    Jun 2022
    Posts
    239

    Re: VB6 - MMapper, Memory Mapped File Demo

    Quote Originally Posted by xiaoyao View Post
    can use rot?lot?
    like getobject("11.xls")
    Yes . I know. The ROT is what I use but I was wondering if the file mapping can somehow make possible sharing objects memories between processes.

    Thanks.

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: VB6 - MMapper, Memory Mapped File Demo

    Quote Originally Posted by AngelV View Post
    I was wondering if the file mapping can somehow make possible sharing objects memories between processes.
    Yes it can. Just use the same string-name for the lpName in your call to OpenFileMapping.

    However, this stuff is really just for getting access to Far Memory (beyond our 2GB). There are easier ways to get strings between processes than this (in memory), especially if they're far less than 2GB.

    You can think of this Far Memory I/O much like a regular file. However, it's gone when nobody is accessing it anymore.
    Last edited by Elroy; Jun 30th, 2022 at 11:37 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  12. #12
    Addicted Member
    Join Date
    Jun 2022
    Posts
    239

    Re: VB6 - MMapper, Memory Mapped File Demo

    Quote Originally Posted by Elroy View Post
    Yes it can
    You can think of this Far Memory I/O much like a regular file. However, it's gone when nobody is accessing it anymore.
    Thanks.

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