Solved

Problem in PostMessage Between two Applications.

Posted on 2003-10-28
9
658 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 184
sumDigits challenge 9 149
zeroFront challenge 7 99
Problem to open Excel file 15 133
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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