Solved

SendMessage - Clarification on Properly Using the WPARAM and LPARAM Parameters

Posted on 2004-10-20
3
775 Views
Last Modified: 2013-12-03
I am planning on using the wparam and lparam parameters of SendMessage to send small bits
of data between two Windows applications on the same machine.  The types and forms of the
data are as follows:

* int - between four to six digits
* string - between two to three characters

In addition, the applications will be using a Windows message that I will be defining using
RegisterWindowMessage.  My questions are as follows:

* Are there any problems associated with this technique?
* How do I pass these types of data into SendMessage?  Do I pass them in directly or do I
have to cast them?
* How do I retrieve these types of data from the retrieving application?

Finally, I am programming in C using Windows .Net.  Any help is appreciated.  Please
provide code samples with your explanation if applicable.
0
Comment
Question by:jtrades
3 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 12364817
0
 
LVL 8

Expert Comment

by:mxjijo
ID: 12365045

>> Are there any problems associated with this technique?
     No, it should work !

>> How do I pass these types of data into SendMessage?
     You don't it is generic! Its the agreement between the sending function and the receiving function.
On other words, the receiving function knows what type LPARAM and WPARAM are, it it type casts them accordingly.
If you pass more than one params, then make a structure and pass the address of the structure as LPARAM/WPARAM.
Hope you know that LPARAM and WPARAM are kind of same the only diff is WPARAM is long and LPARAM is unsigned long.

>>Do I pass them in directly or do I have to cast them?
    Typecast them.

Example:

typedef struct _SAMPLE
{
     int IntField;
     char szCharField[80];
}SAMPLE, *PSAMPLE;


Sending Functtion:
PSAMPLE pSendSample = new SAMPLE;
SendMessage(hWindow, WM_MY_MSG , (WPARAM)pSendSample, 0);



Receiving function:
WindowProc( HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
{
    case  WM_MY_MSG :
    PSAMPLE pSample = (PSAMPLE)wParam;
    if (pSample)
    {
         // access fields here
         pSample->IntField = 1;
         pSample->szCharField[0]  = 0;
    }  
}


0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 12367255
Pointers cannot be used for inter-process communications. Pointer passed to another process is invalid in destination process address space. Your data may be passed in WPARAM and LPARAM (both are 32-bits integers) directly.

int - between four to six digits. May be casted directly to WPARAM or LPARAM.
string - between two to three characters. May be packed to 3 bytes of WPARAM or LPARAM.

Avoid using SendMessage, use PostMessage whenever it is possible. SendMessage is synchronous and doesn't return until receiver handles this message. If receiver application hangs, sender hangs as well. Use SendMessage only if you need to know return code.

You need to think also about getting list of destination applications to post message to them. The simplest way is using HWND_BROADCAST parameter of PostMessage function.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

932 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

13 Experts available now in Live!

Get 1:1 Help Now