-
Apr 13th, 2022, 05:31 AM
#1
[VB6] CSharedMemory - class for dynamic memory allocation in shared memory
This class provides dynamic memory allocation in shared memory like in a heap.
https://github.com/thetrik/CSharedMemory
-
Apr 13th, 2022, 03:46 PM
#2
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.
-
Apr 14th, 2022, 07:12 AM
#3
Addicted Member
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.
-
Apr 14th, 2022, 10:57 AM
#4
Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory
Originally Posted by jg.sa
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.
-
Apr 14th, 2022, 11:38 AM
#5
Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory
Originally Posted by jg.sa
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.
-
Apr 15th, 2022, 12:31 AM
#6
Addicted Member
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
-
Apr 15th, 2022, 12:39 AM
#7
Addicted Member
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
-
Apr 21st, 2022, 07:31 PM
#8
Fanatic Member
Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory
Originally Posted by The trick
Are there any practical examples? thanks
-
Apr 22nd, 2022, 01:09 PM
#9
Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory
Originally Posted by xxdoc123
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.
Last edited by The trick; Apr 22nd, 2022 at 01:16 PM.
-
Apr 25th, 2022, 03:36 PM
#10
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.
-
Apr 26th, 2022, 06:38 AM
#11
Hyperactive Member
Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory
Originally Posted by The trick
Looks good. Any chance you have a sample of usage?
-
May 7th, 2022, 05:51 PM
#12
Hyperactive Member
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?
-
May 8th, 2022, 11:53 AM
#13
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.
-
May 17th, 2022, 12:13 PM
#14
Hyperactive Member
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.
-
May 17th, 2022, 12:21 PM
#15
Re: [VB6] CSharedMemory - class for dynamic memory allocation in shared memory
Originally Posted by MountainMan
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
-
May 17th, 2022, 07:37 PM
#16
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|