Inter Process Communication via WM_COPYDATA

This is a series of articles that will cover various techniques supported by Inter Process Communication (IPC) IPC is a mechanism by which the various processes share data among themselves. The application that uses the IPC mechanism can be placed in two categories: client and server application. A client is an application or a process that uses the services of some other application or a process. A server is an application or a process that responds to or processes the client request. The same application can act as both a server and a client.

The WIN32 SDK provides various ways to achieve an IPC. These are the following techniques through which IPC can be achieved:

  • WM_COPYDATA
  • Clipboard
  • FileMapping Objects
  • MailSlots
  • Pipes
  • Sockets
  • COM
  • RPC

This series of articles will cover each technique in detail, along with the source code. We will cover WM_COPYDATA message that an application can use to send data to an another application. In this technique, the client application sends a message to a window that is in the server application.

A client application sends a WM_COPYDATA message to pass the data to the server application. To send this message, the client application calls SendMessage, which has the prototype described in the next section.

The SendMessage Function

The SendMessage function sends the specified message to the specified window. The function doesn't return until the window procedure of that specified window has processed the message.

SendMessage((HWND) hWnd,      // handle to destination window
WM_COPYDATA,                  // message to send
(WPARAM) wParam,              // handle to window
(HWND)  (LPARAM) lParam       // data (PCOPYDATASTRUCT));

SendMessage Parameters

  • hWnd: Handle to destination window that will receive the WM_COPYDATA message.
  • wParam: Handle to Window (passes the hWnd by typecasting it into WPARAM).
  • lParam: Pointer to COPYDATASTRUCT that contains the data to be transferred to another application.

The COPYDATASTRUCT Structure

The COPYDATASTRUCT structure contains the data that is to be passed to another application.

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

COPYDATASTRUCT Structure Parameters

  • cbData: Specifies the size in bytes.
  • lpData: Pointer to data that is to be passed to another application.

The source code has been divided into two parts—a client application and a server application. In this, the client application takes the string and the operation to be performed on that string from the user. The client application passes the necessary information to the server application so that the server can perform the requested operation on that string. The server application performs the requested operation on the data and displays the results on its console.

The WM_DATASTRUCTURE Structure

typedef struct _WM_DATASTRUCTURE{
DWORD cbSize;         // size of the string entered by user.
DWORD iMessage;       // operation being requested by user.
HWND hClient;         // handle to a client window. It is for
                      // further use. Currently, it is NULL.
_TCHAR Data[_WM_MAXMESSAGE_SIZE - _WM_HEADER_SIZE];
} WM_DATASTRUCTURE, *LPWM_DATASTRUCTURE;

The TransferData function makes a call to the LocalAlloc function to allocate the specified number of bytes from the heap. If the heap doesn't have the sufficient memory to allocate the requested bytes, it returns NULL. The lpData data member of COPYDATASTRUCT points to the WM_DATASTRUCTURE structure, which contains the information to be passed to the server application. After filling the COPYDATASTRUCT structure, the client application makes a call to SendMessage with the address of this structure as a last argument.

The Server Application

The server application registers the window class by calling the RegisterClass function. A window class is a set of properties that the operating system uses to create the window. Each window class has an associated window procedure that processes the messages being sent to the window of that window class. The server application retrieves the message from the message queue by calling GetMessage, and then calls DispatchMessage function, which dispatches the message to the window procedure for the further processing. After registering the window class with the operating system, the application can create many windows of that specific class; all the windows of that class share the same window procedure.

The window procedure WMCOPYWNDPROC performs the specified operation on the data, which is being sent by the client application. The window procedure typecast the lParam parameter to LPWM_DATASTRUCTURE to get the data being sent by the client application.

//Server side code . . . . .
LPWM_DATASTRUCTURE lpMsg;
(LPWM_DATASTRUCTURE)((COPYDATASTRUCT*)lParam)->lpData;

In this, lParam is a pointer to a COPYDATASTRUCT structure that exists in the address space of the receiving process. The lpData of the COPYDATASTRUCTURE is pointing to the _WM_DATASTRUCTURE, so it is telecast to the LPWM_DATASTRUCTURE.

Unicode Support

Unicode is a set of standards for a character-coding system. In Unicode, each character is two bytes, instead of being a single-byte ANSI character. A wide character is a two-byte character. The wide character is of the wchar_t type. Any ASCII character can be represented as a wide character by prefixing the letter L to the character. The L'M' is a wide character. Similarly, the ASCII string can be represented as a wide character string by prefixing the L to the string. The L"Unicode" is a wide character string. The wide characters take much more space than the multi-byte characters, but they are faster to process as compared to multi-byte characters. This is because the wide characters are always of a fixed size.

The wchar_t is defined as follows in stdio.h:

typedef  unsigned short wchar_t;
wchar_t* pStr=L"Hello! I am a wide character literal string."
wchar_t wcsCh= L'A';    //wide character.

Windows 95 and 98 don't support Unicode. An operating system such as Windows NT, 2000, and XP supports Unicode. The application that runs on the NT system should be Unicode. The Window NT provides supports a conversion between ANSI and Unicode. This support can make an ANSI application run on the Windows NT system. The operating system internally converts the ANSI strings to Unicode and vice versa.

The use of two macros, UNICODE and _UNICODE, makes your application Unicode-compatible. The UNICODE header is used in a Windows header file and _UNICODE is used in a C/C++ runtime library. Select the Settings menu item under the Project menu, and then select the C/C++ tab. Type _UNICODE, UNICODE in the Preprocessor definitions edit box.

Generic Macros

The generic macros make the life of a programmer easier because they provide support to write a single source code that can be compatible with both Unicode and the ANSI character set.

We have used two macros that are defined in Tchar.h:

  1. #ifdef UNICODE
    typedef wchar_t _TCHAR;
    #else
    typedef char _TCHAR;
    #endif
    
  2. _T macros expand to L if _UNICODE is defined; otherwise, it is ignored by the preprocessor.

These provide the automatic mapping to the correct character data type depending upon the Preprocessor directive used in project settings.

Notes

  1. The ascending and descending algorithms are meant only for ASCII characters and not for wide characters. In the next article, we will implement these algorithms for the wide characters. I have to explore much more regarding the wide character internals so that I can implement these algorithms on them.
  2. GetModuleHandle is implemented as both a Unicode and an ANSI version on Windows NT/2000, so the use of GetModuleHandle will fail if you try to run it with _UNICODE and UNICODE on Windows 95/98. To make it run on Windows 95/98, change the preprocessor directive to _MBCS or _SBCS.

Downloads

Download demo client project - 13 Kb
Download demo server project - 15 Kb


Comments

  • GHD produits sont adaptés pour obtenir à peu près n'importe quel genre de style de cheveux parfaite

    Posted by flacbh707 on 07/16/2013 12:58am

    Comme nous le savons tous, lisseur ghd maintenant un nom de ménage. mais il ya encore beaucoup de gens se plaignent de leurs prix élevés, cela a aussi conduit à beaucoup de gens ne peuvent pas posséder lisseur ghd. En fait, vous pouvez acheter un bon marché lisseur ghd straightener.ghd bon marché est accueilli par le peuple. Ils peuvent également répondre à vos besoins. En 2001, les redresseurs ghd pas cher engagés à développer les produits les plus efficaces et innovantes pour l'industrie des soins capillaires. Creats GHD différents et belle lisseurs ghd conduisent à des résultats étonnants. [url=http://ghdpascherferfr.blinkweb.com/]lisseur ghd pas cher[/url] Est-ce vrai, beaucoup de gens achèteront seulement un redresseur et pas de shampooing et de produits complémentaires. Donc, je ne suis pas sûr de savoir comment cela fonctionne comme une stratégie d'entreprise à long terme, une chose est sûre cependant, c'est que le consommateur est certainement bénéficier de ces marques connues prices.Some réduits sont moins chers, Revlon et Remington, offrant un très bon choix en des alternatives moins chères. Bien qu'ils ne peuvent toujours pas être classés comme pas cher que vous pourriez espérer, mais ils sont encore à moitié prix marque GHD. Ils sont aussi faites de plaques en céramique et tourmaline. Cela verrouille à l'humidité dans vos cheveux et ne se dessèche pas. Ces produits sont excellents car ils sont à double usage, pour le redressage et le curling, ils ne seront pas endommager vos cheveux et céramique dure beaucoup plus longtemps que l'aluminium fait. [url=http://ghdpascherferfr.blinkweb.com/]ghd lisseur[/url] Le Param cadeau ordonné a le ghd mouette V Classic styler dans une riche rubis métallique finish.Suitable pour tous les types de cheveux, ce styler la même funksjonene comme la mouette V Classic styler mode veille habitude ghd inkludert automatique, spenning de vente universel et un fil extra-longue Rotary pour y distillat frihet totale comme vous le style. A Svart vattert sak qui intègre également un tapis de résistant à la chaleur hendig intelligent, plein de plomb, ce casque élégant

    Reply
  • Stil og design agt deres øjne

    Posted by wherewanzi on 06/05/2013 07:26pm

    [url=http://beatsbydrdredanmark.webstarts.com/]Køb høretelefoner billig[/url] Producenter frembringer deres egne beats på nettet åbner ubegrænsede samarbejdsmuligheder med hensyn til designere på et stramt budget. Den særlige udseende tilgængelig er utallige der en bred vifte af friske rekorder på markedet hver dag derude sted. I tilfælde af at du synes at købe overgår, anmeld af din web selskabet samt høre deres varer. Du er i stand til at Google eller måske Ask tilstande såsom “internet-producenter” eller “køb er bedre end online” og masser af troværdige producent internetsider vil helt sikkert producere.En anden tendens jeg opdager i trap rekorder kunne være den store brug af rumklang omkring den primære enhed. Der er typisk en enkelt pind ud værktøj put, der har en velvoksen reverb effekt sige for eksempel en grand violin, guitar, samt synth. I de fleste tilfælde en let melodi, der gentager flere gange samt modulerer op eller ned. Processen mod kaos er meget simpelt, så prøv ikke at under vurdere de grunde, der kan placeres i dette. beats [url=http://beatsbydrehretelefoner.weebly.com/]Beats by dre billig pris[/url] Based hovedtelefoner tilbyde lydkvalitet, der er lige så forbløffende. Ideel til nutidens digitale musik, disse hovedtelefoner giver dyb bas, en fed mellemtone, og klar, uforvrænget diskant, så du kan høre hver detail.The on-ear, lukket-back design giver langvarig komfort og en høj grad af naturlig støj isolation . Den enkeltsidede kabel er designet til at give dig masser af slæk mens modstå tangles. Integreret i kablet er Monsters ControlTalk modul, som giver dig en kontrolknap og high-grade mikrofon, ideel til styring musikafspilning, telefonopkald og tage stemmenotater på kompatible iPods, iPhones, og Blackberrys. Den guldbelagt 3,5 mm stik giver bred kompatibilitet med MP3-afspillere, cd-afspillere, computere og meget mere. [url=http://beatsbydrdredanmark.webstarts.com/]beats by dre danmark[/url] IOC og LOCOG ikke havde reageret på en anmodning om at kommentere på tidspunktet for offentliggørelsen. Den officielle sponsor, der er mest tilbøjelige til at føle sig mest forurettede ved stunt er Panasonic.England fodboldspillere brugte Dr. Dre hovedtelefoner ved Euro 2012 for at undgå pressen. Hovedtelefonerne voksede i popularitet på Beijing 2008 Olympiske Lege, efter at selskabet gav dem til basketball stjerne LeBron James, og han gav dem ud til medlemmer af Team USA.Den baghold markedsføring initiativet kommer i hælene på en protest – iværksat af amerikanske sportsfolk, herunder 400m løber Sanya Richards-Ross – kritisere regel 40 i IOC adfærdskodeks. Reglen forbyder atleter fra at nævne deres personlige sponsorer på sociale medier under legene.

    Reply
  • Most people brobdingnagian defeats volunteer greatest support along with salubrious earphone

    Posted by motherdhmm on 06/03/2013 07:54pm

    [url=http://www.headphonescheaponlineaustralia.com/dre-beats-studio-c-66_68_71.html]beats studio[/url] Deformity Beats side, as an alternative any status of on any effect files verge to be along the way to come by textile to prop all people within nearly closeness to be skilful to well. you can procure the difficulties when any individual own within your is more advisedly than through dre buy of your pep cheap dr dre beats, as good as any thoughts cater to to be organising actually . [url=http://www.headphonescheaponlineaustralia.com/products_all.html]Beats headphones shop[/url] We konw the Beats Dr Dre Pro are the naming headphone designed by audio professionals for audio professionals. Unusually, those Beats Dr Drewho prefer a balanced yet aggressive sound across the spectrum. Constructed from true-blue yet lightweight aluminum, engineered and artfully designed with the pro in mind, Mutation Beats Pro were built from the organize up with the favour of audio professionals. Why Nightmarishness Beats by dre Pro headphones pay attention to to music to distinguish people altogether edgy, because its regard cushions! [url=http://blog.cheapbeatsbydre.co.nz/beats-by-dre-headphones]beats by dre headphones[/url] Beats digital amplification ensures you can understand every note wherever you go.The Submerge Study quirk in iTunes balances playback levels so all your music tracks portray at a compatible volume.Beats comes with a Horridness iSoniTalk ? headphone hawser with built-in riposte button and microphone so you can clearly fill calls while listening to music.

    Reply
  • Neutral website reveals Four new stuff over nike shoes that no one is bringing up.

    Posted by moisseenfogma on 05/17/2013 08:42pm

    N [url=http://www.nikekutuja.biz/]nike running[/url] wqPmv UhdYtz YjgRzu Orm [url=http://www.nikekutuja.biz/air-jordan空気ヨルダン-c-1.html]nike air jordan[/url] MvzZaaEwl Me [url=http://www.nikekutuja.biz/air-maxエアマックス-c-2.html]air max 95[/url] zInuYooKnb EutSczPiw [url=http://www.nikekutuja.biz/nike-air-force-1ナイキエアフォース-c-4.html]ナイキ エアフォース1[/url] Yee IviZqf Yw [url=http://www.nikekutuja.com/]nike スニーカー[/url] yEkj XhzHme ZovSna NfbO [url=http://www.nikekutuja.com/air-jordan空気ヨルダン-c-1.html]air jordan[/url] qyYqoRec VcyVytUqvA [url=http://www.nikekutuja.com/nike-dunkナイキダンク-c-4.html]nike dunk low[/url] xc DzlPuwWo [url=http://www.nikekutuja.com/air-maxエアマックス-c-2.html]nike air max[/url] kDkr BdnXae Te [url=http://www.nikekutujp.com/]nike スニーカー[/url] xQao OsgEqb WneBgw SrvW [url=http://www.nikekutujp.com/nike-dunkナイキダンク-c-4.html]nike dunk[/url] ugTvkEzv Y [url=http://www.nikekutujp.com/nike-air-force-1ナイキエアフォース-c-3.html]air force[/url] ceWjhVopJve [url=http://www.nikekutujp.com/air-jordan空気ヨルダン-c-1.html]nike jordan[/url] FtyVxcTimFjl McuWxy

    Reply
  • WM_COPYDATA ?bug

    Posted by Legacy on 04/14/2003 12:00am

    Originally posted by: Rivaborn

    MSDN documentation says -

    The receiving application should consider the data read-only. The pcds parameter is valid only during the processing of the message. The receiving application should not free the memory referenced by pcds. If the receiving application must access the data after SendMessage returns, it must copy the data into a local buffer.

    But I would advise anyone writing an WM_COPYDATA handler to copy his data to a local buffer as soon as possible.

    In the project code consider replacing

    SETSTRLEN(lpMsg->Data,(lpMsg->cbSize));

    with

    char cs[_WM_MAXMESSAGE_SIZE] ;
    memcpy(cs, lpMsg->Data, lpMsg->cbSize) ;
    cs[lpMsg->cbSize] = 0 ;

    My app was working perfectly then suddenly started throwing exceptions (Priviledged Instruction) till I made this change. Can't explain it, but I should mention I was sending several WM_COPYDATA messages one after another. And I was using it for inter-thread communication within the same application.... Go figure.

    Caveat Emptor!!!

    Reply
  • wParam error?

    Posted by Legacy on 03/31/2003 12:00am

    Originally posted by: Eric Sanders

    Nice article Dinesh!
    My understanding is that the wParam is the windows handle of the client window, not the server window, although it appears to work fine either way. Any idea why this is or what the wParam is even used for, (unless you yourself makes use of it). In fact, as an afterthought, is there anything wrong with client simply sending a registered message, passing it's own hWnd, then the server performs WM_GETTEXT command got get the text in the clients window for simply text data transfer?

    TIA
    Eric Sanders
    www.compufoil.com


    Reply
  • Inter Process Communication via WM_COPYDATA

    Posted by Legacy on 03/29/2003 12:00am

    Originally posted by: Mini

    Good Article, expect the next article in the series soon. BTW, could you please give some comments on areas where each technique is more appropriate or not good?
    Mini.

    Reply
  • Inter Process Communication via WM_COPYDATA

    Posted by Legacy on 03/27/2003 12:00am

    Originally posted by: Mamta

    its very nice article n i wish all the best wishes to Dinesh.
    
    

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds