Solved

Problem in PostMessage Between two Applications.

Posted on 2003-10-28
9
677 Views
Last Modified: 2013-11-20
I am trying to pass a structure
typedef struct
{
int argc;
char argv[80][10];
}ARGS;
from APP1 to APP2 using PostMessage.In APP1,the flow of code is as follows:
#define WM_GETMESG WM_USER+50
ARGS *args;
args=(ARGS *)malloc(sizeof(ARGS));
args->argc=0;
strcpy(args->argv[args->argc++],"snmpget");
strcpy(args->argv[args->argc++],"-v");
strcpy(args->argv[args->argc++],"2c");
strcpy(args->argv[args->argc++],"pslv");
strcpy(args->argv[args->argc++],"public");
strcpy(args->argv[args->argc],"sysDescr.0");
      
HWND hwnd;
hwnd = ::FindWindow("SnmpGet","SNMPGET");
if(hwnd!=NULL)
{
::PostMessage(hwnd,WM_GETMESG,0,(LPARAM)(ARGS *)args);
}

In APP2:
I receieve the message under case WM_GETMESG using the GetMessage Function.But i dont get the values properly.

Kindly Help.
0
Comment
Question by:fundo_techies
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 34 total points
ID: 9639937
It's impossible to pass pointers between applications. Each porocess has it's own address space, and pointer passed to another process is invalid. You need to use inter-process communication (IPC).

See:

http://www.codeproject.com/threads/ipc_tute.asp (beginner tutorial)

http://www.codeproject.com/threads/#Inter%2DProcess+Communication (various ways of IPC).
0
 
LVL 3

Assisted Solution

by:freewell
freewell earned 33 total points
ID: 9640187
Use WM_COPYDATA message.


// In the sending APP1
#define WM_GETMESG WM_USER+50
typedef struct
{
      int argc;
      char argv[80][10];
}ARGS;

ARGS *args;
args=(ARGS *)malloc(sizeof(ARGS));
args->argc=0;
strcpy(args->argv[args->argc++],"snmpget");
strcpy(args->argv[args->argc++],"-v");
strcpy(args->argv[args->argc++],"2c");
strcpy(args->argv[args->argc++],"pslv");
strcpy(args->argv[args->argc++],"public");
strcpy(args->argv[args->argc],"sysDescr.0");
     
HWND hwnd;
hwnd = ::FindWindow("SnmpGet","SNMPGET");
if(hwnd!=NULL)
{
      //::PostMessage(hwnd,WM_GETMESG,0,(LPARAM)(ARGS *)args);
      COPYDATASTRUCT CopyDataStruct;
      CopyDataStruct.dwData = WM_GETMESG;
      CopyDataStruct.cbData = sizeof(ARGS);  // size of data
      CopyDataStruct.lpData = args; // data structure

      SendMessage(hwnd,WM_COPYDATA,(WPARAM)(HWND)hMywnd,(LPARAM)(LPVOID)&CopyDataStruct); // hMywnd = CreateWindow(...)
}




// In the receiving APP2
#define WM_GETMESG WM_USER+50
typedef struct
{
      int argc;
      char argv[80][10];
} ARGS;

ARGS *args;
COPYDATASTRUCT *pCopyDataStruct;
...


// ************ Code fragment ****************
//
case WM_COPYDATA:
      pCopyDataStruct = (COPYDATASTRUCT *) lParam;
      switch(pCopyDataStruct->dwData)
      {
      case WM_GETMESG:
            args = (ARGS *)pCopyDataStruct->lpData;
            args->argc; // You get your data!
      break;
      }
0
 

Author Comment

by:fundo_techies
ID: 9640216
I dont want to use SendMessage as it blocks the 1st application till the message has been processed.I need to use only PostMessage for my applications.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9640246
Pass the info through a memory mapped file that both apps have open.  I believe that is the recommended method for sharing things between apps.
0
 
LVL 3

Expert Comment

by:freewell
ID: 9640588
If you are passing simple data such as DWORD, you are use the PostThreadMessage. The data is copied to local WPARAM and LPARAM variable.

For sharing a data structure,  two threads must be synchronized so that when one thread is writing, the other thread is blocked from accessing.

WM_COPYDATA message makes used of the shared memory for data exchange between threads.

The SendMessage WM_COPYDATA ensures that the receiving thread has finished copy out the data before the sending thread is allowed to amend it.

It is easier and safer way if you don't want to dive into Multithread Synchronization issue. To accelerate the SendMessage call, just do data copying in receiving thread and return immidiately. There is no time penalty for the receiving thread since this is essential thing that it needs to perform.

0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 33 total points
ID: 9640970
If you hold the message text in a CEdit or CStatic window (you may hide it with ShowWindow(SW_HIDE)) then you may pass the HWND handle of that control using LPARAM. At the receiver's side your OnMessage Handler casts LPARAM back to HWND, then
gets a CWnd* using CWnd::FromHandle and finally gets the text by GetWindowText().

sender:

    CWnd* pFrm = FindWindow(NULL, "Test Receiver");
    if (pFrm != NULL)
    {
        CWnd* pStc = GetDlgItem(IDC_TEXT);
        HWND  hStc = pStc->GetSafeHwnd();
        pFrm->PostMessage(WM_USER + 1111, 0, (LPARAM)hStc);
    }

receiver:

mainfrm.cpp

#define IDM_TEXT WM_USER + 1111

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
      //{{AFX_MSG_MAP(CMainFrame)
            // NOTE - the ClassWizard will add and remove mapping macros here.
            //    DO NOT EDIT what you see in these blocks of generated code !
    ON_WM_CREATE()
    ON_MESSAGE(IDM_TEXT , OnMessage1111)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

LRESULT CMainFrame::OnMessage1111(WPARAM w, LPARAM l)
{
    HWND  hStc = (HWND)l;
    CWnd* pStc = CWnd::FromHandle(hStc);

    if (pStc != NULL)
    {
        CString strText;
        pStc->GetWindowText(strText);
        AfxMessageBox( strText );
        // There it is
    }
    return 1;
}

Hope that helps, Alex
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 52
How to know only "File created" with EventLog 3 35
WinWaitActive parameters 12 31
NotAlone Challenge 20 88
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.

749 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