Solved

Sending Messages to Windows in another process

Posted on 2003-12-04
2
447 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now