?
Solved

SendMessage - Clarification on Properly Using the WPARAM and LPARAM Parameters

Posted on 2004-10-20
3
Medium Priority
?
799 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
[X]
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
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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

765 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