Solved

Copying memory block between Windows..??

Posted on 1998-09-14
4
460 Views
Last Modified: 2013-12-03
hello, experts..
I want to copy memory block between windows..

example is this..

window#1 (Sender Window, mem_block is BYTE *)
     FaceWin = FindWindow("MainWin",NULL);                        
     SendMessage(FaceWin,WM_SENDDATA,
                 (WPARAM)mem_block,(LPARAM)size);

window#2 (Receiver Window,  it's class name is "MainWin")

   case WM_SENDDATA :
        {BYTE *mem;

         mem = (BYTE *)wParam;
         memcpy(block,mem,(int)lParam); //block is BYTE *
        //occured Access Violation !!! at this  
        //point..(in memcpy,,)

why Access Violation is occured ?..
give me your solutions..
0
Comment
Question by:jhjeon
  • 2
4 Comments
 

Author Comment

by:jhjeon
ID: 1414451
each window is different application..
0
 
LVL 7

Accepted Solution

by:
faster earned 100 total points
ID: 1414452
If the memory block is not very huge, you can use WM_COPYDATA message.  It is quite simple so I think the standard sdk document will be enough for you.

Other alternatives include shared memory, pipe, socket, OLE etc, but WM_COPYDATA is the easiest.
0
 

Expert Comment

by:aclynes
ID: 1414453
This example code copies data to a named shared memory block, then sends a message to notify the receiver window.

SENDER WINDOW
=============

//Set up some data to send.
int size = 256;
BYTE* mem = (BYTE*)malloc(size);

//Put some dummy data into the block;
_tcscpy((TCHAR*)mem, "Test data");

HANDLE hMapFile=NULL;
LPVOID lpMapAddress=NULL;

//Create a mapping object named "My mapping object" which we
//can share with the receiver window.
hMapFile = CreateFileMapping(
      (HANDLE)0xFFFFFFFF,    // Use the page file
      NULL, // Default security.
      PAGE_READWRITE, // Read/write permission.
      0, // Data size (high-order).
      size,       // Data (low-order).
      "My mapping object"); // Name of mapping object.
      
if (!hMapFile)
      return;

//Load the mapping object into our address space.
lpMapAddress = MapViewOfFile(
      hMapFile, // Handle to mapping object.
      FILE_MAP_ALL_ACCESS,      // Read/write permission.
      0, 0, 0); // Map entire file.

//Copy the data into the mapping object, and tell the receiver
//window about it.
if (lpMapAddress)
{
      //Copy the data.
      memmove((LPTSTR)lpMapAddress, mem, size);
      //Notify the receiver window with a registered message.
      ::SendMessage(FaceWin,
            RegisterWindowMessage("SEND_DATA_MESSAGE"),
            size, 0
      );

      //Cleanup
      UnmapViewOfFile(lpMapAddress);
}
/Cleanup
CloseHandle(hMapFile);
free(mem);


RECEIVER WINDOW (HANDLES THE REGISTERED MESSAGE)
================================================

HANDLE hMapFile;
LPVOID lpMapAddress;

//Open the mapping object created by the sender window
hMapFile = OpenFileMapping(
      FILE_MAP_ALL_ACCESS, // Read/write permission.
      FALSE,
      "My mapping object"); // of the mapping object.
 
if (!hMapFile)
      return 1;

//Load the mapping object named into our address space.
lpMapAddress = MapViewOfFile(
      hMapFile, // Handle to mapping object.
      FILE_MAP_ALL_ACCESS, // Read/write permission.
      0, 0, 0);
            
if (!lpMapAddress)
{
      CloseHandle(hMapFile);
      return 1;
}

//Do something with the data.
::MessageBox(NULL, (TCHAR*)lpMapAddress, "Received Data", MB_OK|MB_ICONINFORMATION);

//Cleanup
UnmapViewOfFile(lpMapAddress);
CloseHandle(hMapFile);


0
 

Author Comment

by:jhjeon
ID: 1414454
Thanks, faster and aclynes !.

have a nice day !.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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