Solved

Copying memory block between Windows..??

Posted on 1998-09-14
4
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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