Solved

Using shared memory in Win32 applications

Posted on 1999-01-06
10
200 Views
Last Modified: 2010-04-02
I want to use simple windows messaging with a buffer of text
the allocation should be global yet there's a problem to use GlobalAlloc since it no longer shares memory between applications.
I also used CreateFileMapping but this solution is too dependent it won't solve my problem. any suggestions?
0
Comment
Question by:arbitrary
10 Comments
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Try the WM_COPYDATA message.  It is the easiest way to pass data between applications.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
However, if you want to go with the file mapping, you should be able to.  I can see no reason why a file mapping won't work.  What do you mean by it is "too dependant"?  
0
 
LVL 1

Author Comment

by:arbitrary
Comment Utility
WM_COPYDATA needs a structure for data - COPYDATASTRUCT
it has 3 var in it - dwData cbData and lpData
I tried to assign the Data pointer (char* ) to the dwData and to
lpData , I know the value of the pointer is value yet there was
a memory violation as if it was null.
i tried to use file mapping but there's synchronization problems with it , which i can't base on synchronizing it since it will make my application less robust.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> I tried to assign the Data pointer (char* ) to the dwData and to
>>  lpData , I know the value of the pointer is value yet there was
You must have done somethig wrong, can you post the code that sent the message and also the code that receives it?

>> which i can't base on synchronizing it
>> since it will make my application less robust.
synchronization will make your application more robust, not less.  What sort of problem were you having or you thought you were having?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
What about a DLL that exports a shared memory section, e.g.

#pragma data_seg( ".shared")
// shared data goes here
char acSharedCharArray [ SHARED_ARRAY_SIZE] = "\0";
// initialization _is_ necessary
#pragma data_seg()

link using

SECTIONS
shared READ WRITE SHARED

in the .def file

Then, export 2 functions from this DLL that allow synchronized access to the data, e.g. 'BOOL SetData( char* pcData, long lnLen)' and 'long GetData( char* pcBuffer, long lnLen)'...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:stefanr
Comment Utility
Another way of sharing data between applications besides WM_COPYDATA (easiest), memory mapped files (most powerful), and a shared DLL data segment is to use the network functions, like a named pipe, NetBios or TPC/IP.
0
 
LVL 1

Author Comment

by:arbitrary
Comment Utility
nietod method  is the most satisfiable,
i reckon you should get the points .

0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
Comment Utility
Then I'm answering again, but did you try it? Is it working?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
So, what is happening, is this working?
0
 
LVL 1

Author Comment

by:arbitrary
Comment Utility
I've tried it and so far it works as good as it can get ,
I think it's good since the i don't need to synchronize between the proccesses .
As I said it's the most satisfying solution,
thanx.
oh sorry it took a long time i just completely forgot 'bout it.
so there you go
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

11 Experts available now in Live!

Get 1:1 Help Now