Solved

Sending Messages to Windows in another process

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 99
method notes when mouse over in eclipse 5 68
conditional code and condition difference 9 65
scoreUp challenge 14 48
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‚Ķ
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

20 Experts available now in Live!

Get 1:1 Help Now