Solved

SendMessage - Clarification on Properly Using the WPARAM and LPARAM Parameters

Posted on 2004-10-20
3
776 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

11 Experts available now in Live!

Get 1:1 Help Now