?
Solved

Problem in PostMessage Between two Applications.

Posted on 2003-10-28
9
Medium Priority
?
691 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 136 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 132 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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit 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 132 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

801 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