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
Solved

Problem in PostMessage Between two Applications.

Posted on 2003-10-28
9
667 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
only14 challenge 19 81
EvenOdd challenge 10 125
Execute multiple curl cmds with sleep and send output to file 10 104
Problem to open Excel file 15 174
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…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

808 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