Solved

Fastest way to move BITMAP data between processes

Posted on 2001-07-05
12
828 Views
Last Modified: 2013-12-03
I am working on a project where I have to move a lot of HBITMAP data between applications. Currently I am using a COPYDATASTRUCT to hold the entire array of data from GetDIBbits then send it using a WM_COPYDATA message. It works well enough but at times I need to move a midsized bitmap several times a second, and the slowdown is noticeable.

Is there some way to write the bitmap data directly to another process or some other "magic" I can use in this situation.
0
Comment
Question by:mite51
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 6257729
The most efficient method for sharing large blocks of data between processes is via shared memory.  See the MFC class CSharedFile.  I've not done this without MFC but I believe the MapViewOfFile() API is used.
0
 
LVL 1

Author Comment

by:mite51
ID: 6257885
I was avoiding that because I thought the shared file was a file on the hard disk, which would be kinda slow. Of course it could ba a memory mapped file.

Still how much more efficient could it be? I fill in a temporary structure with the data and pass it with WM_COPYDATA where the receiver has to copy it out. I guess I can aviod the step of having to copy it out.

Can I create a hbitmap with the pBits pointer in the memory mapped file? that way all drawing would be rendered directly to the shared memory..? mmmm

Jason
0
 
LVL 2

Expert Comment

by:SChertkov
ID: 6258439
You can create COM object what implement IDataObject
in you target application, and register it in
Running Object Table. Source application extract
interface from ROT, query IDataObject and
through it pass hBitmap. IDataObject marshaled by
system and moving bitmap will be transparent.
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6258557
I am not sure but you can try to make DLL (some type of data manager). Load DLL in both processes, register them (with function pointers or message identifiers) and comunicate throw DLL functions.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6258700
>> I was avoiding that because I thought the shared file was a file on the hard disk, which would be kinda slow. Of course it could ba a memory mapped file.

No, memory mapped files are not on hard disk (except if you don't have enough RAM). To your information: WM_COPYDATA uses memory mapped files internally. Windows uses memory mapped files everywhere, it's optimized as hell. Using WM_COPYDATA is not ideal because it creates and destroys a new memory mapped file object everytime you use it. If you do it yourself, you can keep the memory mapped file open, which will speed things up in comparison to WM_COPYDATA.

>> Can I create a hbitmap with the pBits pointer in the memory mapped file? that way all drawing would be rendered directly to the shared memory..? mmmm

Hmmm... Sounds like a good idea. Try it out!

Regards, Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6258703
P.S: Memory mapped files have a fixed size, I hope that is no problem?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:mite51
ID: 6261493
from MSDN..

Please note that CSharedFile does not use memory-mapped files, and the data cannot be directly shared between processes.

What does that mean?

I have found some useful global/process memory functions. Does anyone have experience with VirtualAllocEx or GlobalAlloc. Could I use these? If I allocate memory in the target processes address space can I directly access it from the former process?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6261524
>> Please note that CSharedFile does not use memory-mapped files, and the data cannot be directly shared between processes.
>> What does that mean?

Don't know. Why not using pure APIs? CreateFileMapping + MapViewOfFile.

>> I have found some useful global/process memory functions. Does anyone have experience with VirtualAllocEx
or GlobalAlloc. Could I use these? If I allocate memory in the target processes address space can I directly access it from the former process?

You can use VirtualAllocEx to allocate memory in the destination process, but only in NT based systems, in win9x this function is not available. You can't directly write to this memory from the source process, but you can use WriteProcessMemory to write to the memory.

GlobalAlloc is useless for your purposes. The shared flags don't work in 32bit.

In win9x you could use undocumented functions to allocate memory in the shared area (which is directly accessible from all processes), but why doing that? Memory mapped files do have *EXACTLY* the same effect. In win9x memory mapped files are nothing but allocated shared memory.

Regards, Madshi.
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 6263482
I think there several method can improve the performance.
First, just like MadYugoslav has said, you can use a dll, and use the share-section.
#pragma data_seg("Shared")
//Here is your entire array of data from GetDIBbits
UCHAR  ibuff[MAXBITMAPSIZE];
#pragma data_seg()
#pragma comment(linker, "/section:Shared,rws")
In your dll, you can export two functions, for example, named Get(Set)SharedDiBbits(..).In your two processes, you should use these methods to get or set DIBbits, now you only need send a notify message when a process prepare the DIBbits, and the other process will use the GetSharedDIBbits to get the data. They use the same buffer, there no data transfer.

Another method i can see is, you can use Read/WriteProcessMemory Api, for example, you can only send a message which take the buffer information in one process, in the other process, you can use ReadProcessMemory get the data directly.

And I also think you can use the mapped-file to share the data just as Madshi has said.

Hope this help.
0
 
LVL 1

Author Comment

by:mite51
ID: 6267201
I like the shared data segment in DLLs but is there a way to dynamically allocate the memory? or possibly have a HUGE shared chunk that I can manage. Arrays can only index so much.

Lots of new options tho, thanks
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 6267609
I don't think you can dynamically allocate the memory, if you wanted to use this method you only can use like this:
UCHAR  ibuff[MAXBITMAPSIZE] = {0}; // should be initialized, and the size is fixed!
0
 
LVL 1

Author Comment

by:mite51
ID: 6287897
there was some good input and thanx to all but I think I am going to go with a shared mem file. I guess I'll be giving my points to jhance.

Anyway, here is the code I decide to go with which suits my needs farirly well.. IF I can allocate 20 M chucks with it :)

http://www.microsoft.com/MSJ/1198/wicked/wicked1198top.htm
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now