Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Copying memory block between Windows..??

Posted on 1998-09-14
4
Medium Priority
?
474 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 300 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

824 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