Solved

string in LPARAM

Posted on 2003-11-26
15
1,822 Views
Last Modified: 2007-12-19
Hello

I Use PostMessage to broadcast a WindowsMessage. I want to post a string, but it seems like I can only post integers.

I try this:

char test[] = "abc";
PostMessage(HWND_BROADCAST, 49888, 2, test);             //Errormessage: cannot convert parameter 4 from 'char [4]' to 'LPARAM'

But it dowsnt work.

How do i send a string in the LPARAM?

Please help
0
Comment
Question by:sk-man
  • 4
  • 3
  • 2
  • +4
15 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 63 total points
ID: 9825361
You need to allocate space on the heap to be able to post the pointer:

char* test = new char[10];
strcpy (test, "abc");
PostMessage (..., (LPARAM) test);

and be sure to delete [] the memory in the handler of the message!

Caveat: this ony works inside a process. HWND_BROADCAST doesn't really make sence in that case. Outside the scope of the process the memory location is invisible.
0
 

Author Comment

by:sk-man
ID: 9825503
Well now i don't get the error, but it still doesn't work.

I do this in a c++ dll:
------------------------
char* test = new char[10];
strcpy (test, "abc");
PostMessage(HWND_BROADCAST, 49888, 2, (LPARAM)test);



In a C# form i have the WinProc function where i catch the message:
-------------------------------------------------------------------------------
string test = m.LParam.ToString();


This gives me "60107080" which looks like a ptr. address to me.
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 9825596
It is a pointer address, and it's only valid within the same process as you post the message (as SteH said).

You can't pass strings over directly. You have to pass some form of handle or pointer, and have the string stored elsewhere.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:sk-man
ID: 9825635
OK, so I'll figure something else out.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9825676
A CMemFile could be a solution. Since it is in memory it is fast and can be used across process boundaries.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 62 total points
ID: 9825771
If you want to send strings, use 'WM_COPYDATA':

COPYDATASTRUCT cs;

ZeroMemory ( &cs, sizeof ( cs));

char test[] = "abc";
cs.cbData = strlen ( test);
cs.lpData = ( LPVOID) test;

SendMessage ( HWND_BROADCAST, WM_COPYDATA, <handle of your window>, (LPARAM) &cs);
0
 
LVL 86

Expert Comment

by:jkr
ID: 9825791
BTW, see also http://codeguru.earthweb.com/win32/ipcdemo.shtml on this issue.
0
 
LVL 11

Expert Comment

by:Agarici
ID: 9825828
you  can also look at this problem from this point of view: why pass the string to the other process? let the other process get the string when it nedds it. so you can simply export a function from your c++ dll that will get the desired string and call that function from c# form. if you want all processes to access the same value of the string, put it in a 'shared' section of your dll

A.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9827067
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10286116
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: SteH {http:#9825676} & jkr {http:#9825791}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
LVL 86

Expert Comment

by:jkr
ID: 10286434
Um, actually "WM_COPYDATA" is the only way to send strings across process boundaries using windows messaging.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10286475
Sorry jkr

Maybe I'm not quite familiar with this topic in particular, but I thought that Steh's comments should also be awarded some points.

However, mine is just a recommendation, post yours and moderators will decide when they come to close it.

Cheers

Tincho
0
 
LVL 86

Expert Comment

by:jkr
ID: 10286498
Hum, then, to be blunt: I *think* I was closest to the requested solution (using messaging)
0
 
LVL 13

Expert Comment

by:SteH
ID: 10288344
@jkr,
you are right. You gave the correct answer to the question. Mine could be at most interpreted as a hint how it can be solved in another way. Sometimes one is looking for a solution the way one is ued to. And one is not aware that there are other possiblities available.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which IDE to use to begin C++ training? 5 63
VS2015 compilation and missing DLLs 9 147
c++ syntax question 9 48
trigs fail! I thought I knew how to do trignometry 3 65
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 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