Solved

Problem in PostMessage Between two Applications.

Posted on 2003-10-28
9
637 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
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

10 Experts available now in Live!

Get 1:1 Help Now