Solved

Sending Messages to Windows in another process

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Folder Comparison 12 54
not able to insert into temp table 68 169
child constructor and parent constructor, overriding and overloading 6 88
Making an alias 7 101
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli‚Ķ
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

829 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