We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 22,188 times

Contents

Related Categories

Message Management - WM_COPYDATA

flounder

WM_COPYDATA

There is a possibly useful message, WM_COPYDATA, that will transfer information across the boundary. It does this by passing the data thru the kernel. Space is allocated in the receiving process to hold the information that is copied, by the kernel, from the source process to the target process. Or something that resembles that. The implementation details are actually concealed from you.

The sender passes a pointer to a COPYDATASTRUCT, which is defined as a structure of the following:

typedef struct tagCOPYDATASTRUCT {
      DWORD dwData;
      DWORD cbData;
      PVOID lpData;
} COPYDATASTRUCT, *PCOPYDATASTRUCT;

The dwData member holds an arbitrary 32-bit value that is being passed to the target. You can set this to any value the two processes agree on. The cbData member tells how many bytes are in the value referenced by lpData. When the target process receives the information, it handles it via a method

BOOL CMainFrame::OnCopyData(CWnd* pWnd, 
                            COPYDATASTRUCT* pCopyDataStruct)

The CWnd * is a reference to the sending window, and the COPYDATASTRUCT * references the COPYDATASTRUCT that was passed in. Note that you don't know the actual address in the sender. The data which is passed in must not contain pointers.

There are some potential problems with WM_COPYDATA, in that you need to identify who has sent it in order to understand if it is valid, or you must have some other way to identify it. One way to handle it is to use our old friend the GUID. If you put a GUID in the beginning of the data packet, you can compare it to the expected GUID and if they are equal you know for certain that the packet you received is the packet you want.

You must not store the pCopyDataStruct.lpData pointer, because after you return from the OnCopyData handler the pointer should be assumed to be no longer valid. You must also not attempt to write into the data referenced by the lpData pointer; it must be treated as read-only.

Comments

  • Posted by hadidi on 26 Jan 2004

    Thx a lot for this useful tip and grats for your clear explanation.

    Best Regards,

    Hadi

  • User defined messages

    Posted by velavar on 08 Sep 2003

    Nice working stuff.. very good information to use...