Results 1 to 16 of 16

Thread: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

  1. #1

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

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    Ahhh, yet another application making use of CreateFileMapping.
    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.

  3. #3
    Addicted Member jg.sa's Avatar
    Join Date
    Nov 2017
    Location
    South Australia ( SA )
    Posts
    199

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    G'Day Elroy

    Are you starting to feel a little jaded ?

    Sometimes there are good reasons to alloc. mem., but shared mem. is starting to seem a little sus.

    These is not much u can do if ppl want to go over to the dark side.

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

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    Quote Originally Posted by jg.sa View Post
    G'Day Elroy

    Are you starting to feel a little jaded ?

    Sometimes there are good reasons to alloc. mem., but shared mem. is starting to seem a little sus.

    These is not much u can do if ppl want to go over to the dark side.
    ja.sa: I'm confused by your post, on two points.

    1) I'm not feeling jaded at all. I have tremendous respect for The Trick. But, until he made this CodeBank entry, I had no idea that he had worked with the CreateFileMapping API call. That's the only reason for my initial comment.

    2) Why is using shared memory suspect? Many of us are often looking for ways to have communications between threads and/or processes that don't require writing to disk, and this is a nice third alternative (to either DDE or the SendMessage API). When using shared memory, it's just on us to make sure we've got some kind of marshalling or handshaking protocols setup.
    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.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,687

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    Quote Originally Posted by jg.sa View Post
    G'Day Elroy

    Are you starting to feel a little jaded ?

    Sometimes there are good reasons to alloc. mem., but shared mem. is starting to seem a little sus.

    These is not much u can do if ppl want to go over to the dark side.
    I don't understand what's the problem? This is a custom heap implementation on the shared memory. This is quite strange that you consider this little suspicious.

  6. #6
    Addicted Member jg.sa's Avatar
    Join Date
    Nov 2017
    Location
    South Australia ( SA )
    Posts
    199

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    G'Day Guys

    I didn't mean to upset anyone, I seem to be doing this more and more on here, breaking the rules etc.

    In my defense notice it was a question not a statement !!!

    I never said there was a problem I was wondering 'R U OK' it is my german heritage don't let criminals take you away from cutting gr8 code / systems and being unproductive.

    S O R R Y 4 any offence

  7. #7
    Addicted Member jg.sa's Avatar
    Join Date
    Nov 2017
    Location
    South Australia ( SA )
    Posts
    199

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    BTW - Forgot about the sharing I have been using this tech. for eons -> http://www.thescarms.com/vbasic/PassString.aspx

  8. #8
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    678

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    Quote Originally Posted by The trick View Post
    This class provides dynamic memory allocation in shared memory like in a heap.

    https://github.com/thetrik/CSharedMemory
    Are there any practical examples? thanks

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,687

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    Quote Originally Posted by xxdoc123 View Post
    Are there any practical examples? thanks
    Let's imagine the task where you use threads to work with shared data. Just if you want to use multiprocessing you can use this class. Just pass the offsets instead pointers. The advantage of this class is you have a dynamic memory and you shouldn't calculate the offsets to put your dynamic data.

    For example, you have 2 strings you want to share between processes. If you just use a file mapping you should calculate the size of both strings and copy them to the memory. If you have hundreds of strings and they can be freed the additional efforts are needs to handle all. This class just provides the global heap mechanism for your processes. You can allocate memory and free it without problem (the synchronization is included). You just pass the offsets between your applications.

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

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    Also, someone wanted to know how to break a loop without using DoEvents. If it's another thread (or process) that'll be breaking the loop, this is a perfect way to do it. No need to monitor the message pump ... just monitor this shared memory location in the loop.
    Last edited by Elroy; Apr 25th, 2022 at 03:41 PM.
    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.

  11. #11
    Hyperactive Member
    Join Date
    Mar 2019
    Posts
    426

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    Quote Originally Posted by The trick View Post
    This class provides dynamic memory allocation in shared memory like in a heap.

    https://github.com/thetrik/CSharedMemory
    Looks good. Any chance you have a sample of usage?

  12. #12
    Hyperactive Member
    Join Date
    Feb 2015
    Location
    Colorado USA
    Posts
    269

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    The trick,

    Dumb question. I am working on making your code work in 64-bit VBA. Normally that involves changing handles and pointers to LongPtr and some other minor things. I noticed something in your code that is puzzling.

    In several spots you use a data type called OLE_HANDLE. I looked all over for its definition. It is not in your code nor is it "OLE Automation" (stdole2.tlb) nor is it anywhere else in the other program references. I thought that for sure your code would not compile but it does. So now I am confused. Where is the definition of OLE_HANDLE?

    Also, from everything I can find online, MS took the approach that most of the USER and GDI handles work in 32 and 64-bit code because in 64-bit code, the top 4 bytes of the handle are zero. Based on that it would seem that I can use OLE_HANDLE unmodified in 64-bit code. Do you think that's true?

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,687

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    Where is the definition of OLE_HANDLE?
    stdole2.tlb

    Also, from everything I can find online, MS took the approach that most of the USER and GDI handles work in 32 and 64-bit code because in 64-bit code, the top 4 bytes of the handle are zero. Based on that it would seem that I can use OLE_HANDLE unmodified in 64-bit code. Do you think that's true?
    I faced with errors when used 4-byte pseudo-handles for 64 bits in some situations. So i've been trying to use stronger typing lately.

  14. #14
    Hyperactive Member
    Join Date
    Feb 2015
    Location
    Colorado USA
    Posts
    269

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    The Trick,

    I am trying to expand your code to cover 64-bit VBA and I wanted to verify something with you. You list a heap structure and a block structure near the top of your code. The offsets in these are 4 bytes. Do all of these change to 8 bytes for 64-bit or just some of them? From my review of your code it looks like all of them go to 8 bytes. Is this correct? Thanks.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,687

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    Quote Originally Posted by MountainMan View Post
    The Trick,

    I am trying to expand your code to cover 64-bit VBA and I wanted to verify something with you. You list a heap structure and a block structure near the top of your code. The offsets in these are 4 bytes. Do all of these change to 8 bytes for 64-bit or just some of them? From my review of your code it looks like all of them go to 8 bytes. Is this correct? Thanks.
    For 64 bits (i didn't check that):

    Code:
    ' // Heap structure
    
    ' // 0x00 : Lock
    ' // 0x04 : MaxSize not including header
    ' // 0x0C : Head Offset relative to begin of section
    ' // 0x14 : Blocks
    
    ' // Block structure (describes a free memory block)
    
    ' // 0x00 : Block size not including header
    ' // 0x08 : Next offset relative to begin of section
    ' // 0x10 : Prev offset relative to begin of section
    ' // 0x18 : Data
    
    Private Const OFFSETOF_LOCK           As Long = 0
    Private Const OFFSETOF_MAXSIZE        As Long = 4
    Private Const OFFSETOF_HEAD           As Long = &HC
    Private Const OFFSETOF_FIRSTBLOCK     As Long = &H14
    Private Const HEAP_HDR_SIZE           As Long = &H14
    Private Const OFFSETOF_SIZE           As Long = 0
    Private Const OFFSETOF_PREV           As Long = 8
    Private Const OFFSETOF_NEXT           As Long = &H10
    Private Const OFFSETOF_DATA           As Long = &H18
    Private Const BLOCK_HDR_SIZE          As Long = &H18
    
    Private Type tBlock
        lSize               As LongPtr
        lPrev               As LongPtr
        lNext               As LongPtr
    End Type

  16. #16
    Hyperactive Member
    Join Date
    Feb 2015
    Location
    Colorado USA
    Posts
    269

    Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory

    Since the offsets are to locations in 64-bit space it would seem that they would be LongLong's instead of Longs, especiallly in your procedures you add them to other LongLong's (or 64-bit LongPtr's which as the same). What am I missing?

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