Solved

Sending Messages to Windows in another process

Posted on 2003-12-04
2
448 Views
Last Modified: 2013-11-20
Hi

I have a .exe which receives data on a serial port, does some checking and formatting and passes the data on.
Its dedicated task is just this, however I need to send the data to another application (.exe) so that it can render
the data for graphical display. Is it possible to send/post to the HWND of the second app. i.e. from process A to
process B. What implications for the data? simple strings!

Another way I thought was to use sockets and pass the data this way, does anyone know of
any better ways to acheive this?

Thanks

LittlePerson
0
Comment
Question by:LittlePerson
2 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 35 total points
ID: 9874286
I hope you have the source code of the second process and can write both sending and receiving functions.
When you send (or better, post) data to another process, you cannot pass pointers since each process has it's own address space and pointer passed to another process is invalid. All data posted using Windows message should be contained in (wParam, lParam).
To pass strings (and any other data) you should use inter-process communications (IPC).

Using memory-mapped files for IPC:
http://www.codeproject.com/threads/ipc_tute.asp

Various ways of APC:
http://www.codeproject.com/threads/#Inter%2DProcess+Communication


0
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 40 total points
ID: 9874288
Use SendMessage with *WM_COPYDATA*, that is the recommended way.

MSDN Sample

// ************ Globals ************

#define MYDISPLAY 1

typedef struct tagMYREC
{
   char  s1[80];
   char  s2[80];
   DWORD n;
} MYREC;

COPYDATASTRUCT MyCDS;
MYREC MyRec;

BOOL CALLBACK InfoDlgProc( HWND, UINT, WPARAM, LPARAM );

// ************ Code fragment ****************

// Get data from user. InfoDlgProc stores the information in MyRec.

   DialogBox( ghInstance, "InfoDlg", hWnd, (DLGPROC) InfoDlgProc );

// Copy data into structure to be passed via WM_COPYDATA

   lstrcpy( MyRec.s1, szFirstName );
   lstrcpy( MyRec.s2, szLastName );
   MyRec.n = nAge;

// Fill the COPYDATA structure

   MyCDS.dwData = MYPRINT;          // function identifier
   MyCDS.cbData = sizeof( MyRec );  // size of data
   MyCDS.lpData = &MyRec;           // data structure

// Call function, passing data in &MyCDS

   hwDispatch = FindWindow( "Disp32Class", "Hidden Window" );
   if( hwDispatch != NULL )
      SendMessage( hwDispatch,
                   WM_COPYDATA,
                   (WPARAM)(HWND) hWnd,
                   (LPARAM) (LPVOID) &MyCDS );
   else
      MessageBox( hWnd, "Can't send WM_COPYDATA", "MyApp", MB_OK );
The receiving application has a hidden window which receives the information from WM_COPYDATA and displays it to the user.

// ************ Globals ************

#define MYDISPLAY 1

typedef struct tagMYREC
{
   char  s1[80];
   char  s2[80];
   DWORD n;
} MYREC;

PCOPYDATASTRUCT pMyCDS;

void WINAPI MyDisplay( LPSTR, LPSTR, DWORD );

// ************ Code fragment ****************

case WM_COPYDATA:
   pMyCDS = (PCOPYDATASTRUCT) lParam;

   switch( pMyCDS->dwData )
   {
      case MYDISPLAY:
         MyDisplay( (LPSTR) ((MYREC *)(pMyCDS->lpData))->s1,
                    (LPSTR) ((MYREC *)(pMyCDS->lpData))->s2,
                    (DWORD) ((MYREC *)(pMyCDS->lpData))->n );
   }
   break;


Rosh :)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 42
Separate files macro - Errors on larger data set 18 69
Process filename extension 3 184
sameEnds challenge 3 158
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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