Solved

SendMessage - Clarification on Properly Using the WPARAM and LPARAM Parameters

Posted on 2004-10-20
3
774 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
Comment Utility
0
 
LVL 8

Expert Comment

by:mxjijo
Comment Utility

>> 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
Comment Utility
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

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.

Join & Write a Comment

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

9 Experts available now in Live!

Get 1:1 Help Now