5,666,132 members and growing! (18,638 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

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

By McGahanFL

Use WM_COPYDATA to send data to/from C++ and C# Windows processes
C#, C++, Windows, .NET, .NET 1.0, .NET 1.1VS.NET2002, VS.NET2003, Visual Studio, Dev

Posted: 26 Oct 2003
Updated: 26 Oct 2003
Views: 42,702
Bookmarked: 17 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 4.73 Rating: 3.85 out of 5
2 votes, 11.8%
1
2 votes, 11.8%
2
0 votes, 0.0%
3
7 votes, 41.2%
4
6 votes, 35.3%
5

Introduction

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

Why use WM_COPYDATA?

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

private:
    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.

History

  • 10/23/2003 - First published

License

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

McGahanFL


Just a good guy.
Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralHelp please!memberrufj21:04 5 May '08  
GeneralRe: Help please!memberMcGahanFL3:37 6 May '08  
GeneralRe: Help please!memberrufj0:03 7 May '08  
AnswerRe: Help please!memberAlexey Plotnikov9hrs 43mins ago 
Generalproblemmembermodream1:03 5 Nov '07  
Generalexcellent articlememberdeepikakethireddy1:00 31 Oct '07  
GeneralVery nicememberShato11:40 16 Apr '07  
GeneralGreat jobmemberJ.Q.C.12:13 13 Aug '06  
GeneralExcelentmemberellolazo5:43 13 Jun '06  
GeneralGood jobmemberselb6132:54 26 May '06  
GeneralGreat Job!memberciaoroma6:33 18 Feb '05  
GeneralAwesome. Just what I needed ! However ...memberDan Clark7:21 4 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Oct 2003
Editor: Nishant Sivakumar
Copyright 2003 by McGahanFL
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project