Click here to Skip to main content
14,699,322 members
Articles » Languages » C# » General
Posted 26 Oct 2003


49 bookmarked

Use WM_COPYDATA to send data to/from C++ and C# Windows processes

Rate me:
Please Sign up or sign in to vote.
4.55/5 (29 votes)
26 Oct 2003
Use WM_COPYDATA to send data to/from C++ and C# Windows processes


This demo demonstrates how to send / receive WM_COPYDATA messages between a collection of C++ and C# programs.


Traditionally WM_COPYDATA was used to send a limited amount of data between processes on the same machine. It is still desirable to interoperate this way even with the rise of .NET. .NET Remoting is out of the scope of this demo. This demo is intended for people like myself that are dealing with legacy environments.

The demo consists of four programs and one DLL. Two programs are written in C++ (MS Version 5.0) and two are written in C# (using .NET 2003). A C++ exe and C# exe will send the structure shown below to a C++ and C# receiving program.

struct sTagData
{//this is the structure that will be sent (copied) by the WM_COPYDATA
    char szTag[ciMaxTag];
    char szData[ciMaxData];

The DLL was written in VC 5.0 and it wraps the WM_COPYDATA message.

Additionally, the DLL provides a uniform way of :-

  1. Protecting the receiving application from third party processes that may be unwise enough to HWND_BROADCAST the WM_COPYMESSAGE.
  2. Allows incoming messages to be limited to specific senders either by handle or sender id.
  3. Optional use a crude bitwise encryption to discourage hackers from spying on messages.

Unzip using the full paths to your C drive. The result should be the shown directory structure.

  • C:\WmCpyDta
  • C:\WMCpyDat\Build - the folder where all build output is placed for all projects.
  • C:\WMCpyDat\From_C - a C++ program that sends the structure
  • C:\WMCpyDat\TO_C - a C++ program that receives the structure
  • C:\WMCpyDat\From_C_Sharp – a C# program that sends the structure
  • C:\WMCpyDat\\TO_C_Sharp - a C# program that receives the structure.

To use the demo, open folder build and click on all 4 executables. Click on the send buttons in the FROM apps and the message will appear in the TO apps. Both FROM programs are document to show how to use the DLL’s interface. Likewise, both TO programs are documented in their WindowProc()’s to show how to receive the data.

Platforms supported

These programs have only been tested on Windows XP.

Other facts

Philip McGahan is the sole creator of this demo. No part of it has been plagiarized from other sources. Any party may use all or part of this demo with out credit or reference to Philip McGahan.

Lastly, it is only fair to tell my reader that I am very new at C#. I did try to use C# to build the DLL. But beyond never getting the data to come across with out junk at the end of the string. I was not comfortable with having to setup unsafe blocks. The whole thing just seemed clumsy. When or if I get better at C#, I will revisit this issue.


  • 10/23/2003 - First published


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
Just a good guy.

Comments and Discussions

QuestionVery Nice Sample: but need wmcpydta_d.lib Pin
Member 1348378724-Oct-17 19:27
MemberMember 1348378724-Oct-17 19:27 
AnswerRe: Very Nice Sample: but need wmcpydta_d.lib Pin
Rick York24-Oct-17 20:17
mveRick York24-Oct-17 20:17 
QuestionCan this be used for Communication Between Windows Service and Console Application Pin
Peter Liu27-Sep-11 17:53
MemberPeter Liu27-Sep-11 17:53 
QuestionNeed More! Pin
rahul_raut5-May-11 0:26
Memberrahul_raut5-May-11 0:26 
GeneralMy vote of 5 Pin
rahul_raut5-May-11 0:18
Memberrahul_raut5-May-11 0:18 
GeneralI have an error Pin
hjhk45646520-Apr-09 22:40
Memberhjhk45646520-Apr-09 22:40 
AnswerRe: I have an error Pin
shanekelly15-Mar-11 11:54
Membershanekelly15-Mar-11 11:54 
GeneralHelp please! Pin
rufj5-May-08 21:04
Memberrufj5-May-08 21:04 
GeneralRe: Help please! Pin
mcgahanfl6-May-08 3:37
Membermcgahanfl6-May-08 3:37 
GeneralRe: Help please! Pin
rufj7-May-08 0:03
Memberrufj7-May-08 0:03 
AnswerRe: Help please! Pin
Alexey Plotnikov21-Nov-08 5:08
MemberAlexey Plotnikov21-Nov-08 5:08 
Generalproblem Pin
modream5-Nov-07 1:03
Membermodream5-Nov-07 1:03 
Generalexcellent article Pin
deepikakethireddy31-Oct-07 1:00
Memberdeepikakethireddy31-Oct-07 1:00 
GeneralVery nice Pin
Shato16-Apr-07 11:40
MemberShato16-Apr-07 11:40 
GeneralGreat job Pin
J.Q.C.13-Aug-06 12:13
MemberJ.Q.C.13-Aug-06 12:13 
GeneralExcelent Pin
ellolazo13-Jun-06 5:43
Memberellolazo13-Jun-06 5:43 
GeneralGood job Pin
sbozcan26-May-06 2:54
Membersbozcan26-May-06 2:54 
GeneralGreat Job! Pin
ciaoroma18-Feb-05 6:33
Memberciaoroma18-Feb-05 6:33 
GeneralAwesome. Just what I needed ! However ... Pin
Dan Clark4-Nov-04 7:21
MemberDan Clark4-Nov-04 7:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.