Click here to Skip to main content
13,003,738 members (71,693 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am having a hard time understanding this piece of code.. I am a complete beginner in programming and I am trying to learn new things. Someone kindly explain this for me? Specially the parameters... what purpose each of the parameters will do..

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr,_
ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

I tried looking in the net but I really can't understand .. maybe you guys can give a more easy to understand explanation.
Posted 30-Dec-12 5:47am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is known as Platform Invoke[^], and is a way of using the system API directly from .NET languages. In this specific case it allows you to use the Windows SendMessage function[^], which is a somewhat advanced topic for a beginner. I would suggest that you stick to pure .NET code until you are fully comfortable with it before moving on to using the Win32 functions.
Minazeh 30-Dec-12 12:01pm
i know that this is a bit advance, i think i might need to include this to the program i am currently developing(well, its not a big program though) because in the program i am developing now. i want to add a feature like when a new window pops out like yahoo messenger window for example.. it will automatically focus on that new window appeared then it will automatically send message like "the user of the pc is sleeping..." something like that.. is there a way to achieve this kind of thing with pure .net?
Richard MacCutchan 30-Dec-12 12:10pm
It's not something I have done, but take a look at some of these links.
Minazeh 30-Dec-12 12:19pm
Thanks for the response.. I will review this links for the meantime. maybe I can message you if i have more clarifications to ask?
Richard MacCutchan 30-Dec-12 12:31pm
If you are interested in Windows Hooks then I would suggest starting a new thread, there are probably many people here who can give you help on the subject.
Minazeh 30-Dec-12 12:34pm
maybe i should, but one more question.. related in the code above..the..

ByVal wParam As IntPtr
ByVal lParam As IntPtr

what do they do?...
Zoltán Zörgő 30-Dec-12 12:40pm
Look at the SendMessage API help page Richared gave to you. There you can see, that both parameters are "additional, message specific" information. But SendMessage is not what you want! This is something to send event messages (like click, or keypress) to a specific window, this does not display anything.
Minazeh 30-Dec-12 12:44pm
oh. can you tell me something that is more suited?
Zoltán Zörgő 30-Dec-12 12:50pm
You want to show a popup from a windows forms application?
Minazeh 30-Dec-12 12:55pm
hmm not that i guess, i want to know if there is a way that i can send a message from a newly opened window like a new friend message from yahoo messenger for example, and it will automatically send a message like "he is not here.." things like that.
Zoltán Zörgő 30-Dec-12 13:01pm
Send to who, where?
Newly created widow of what application?

You want to intercept incoming messages of some chat application, and react automatically in name of the user?
Minazeh 30-Dec-12 13:04pm
yes. something like that.
Zoltán Zörgő 30-Dec-12 13:08pm
"Something like that" is no specification. But it looks not easy, since you have to control a third party application. Thus it depend mostly on how that was implemented, if it has any api or interface you can connect to, can you connect directly to the server, or you need to control the client, and so on...
So either you give us really concrete details, or we are unable to help you for sure.
Minazeh 30-Dec-12 13:24pm
How about this.

Let's say i have a click event in this event it will look for a window that contains a name of "Untitled - Notepad" using the FindWindow Function importing from User32.dll.. then bring it to focus using SetForegroundWindow function ... i already manage to get until here.. my problem is.. Automatically sending/passing a string to the notepad after the SetForegroundWindow function executes. How can i pass a string to that notepad. that is my question. I am sorry for not being clear. or maybe it is still not clear.
Zoltán Zörgő 31-Dec-12 3:19am
Any progress?
Minazeh 31-Dec-12 11:19am
hmmm .. i am going to try something like using a dll that autoit has.. maybe there is a solution from that. because i use autoit before and i can set focus and send string to a notepad or any window i want. well.. i am hoping there is.
Richard MacCutchan 30-Dec-12 12:48pm
The ByVal keyword is described here. You should get used to using the MSDN documentation for simple queries, it is an invaluable tool. You should also note Zoltán's comments.
Minazeh 30-Dec-12 12:51pm
The sayings are true. "There are no easy routes in this world." I am grateful for your responses. I will study harder.
Richard MacCutchan 30-Dec-12 12:57pm
That's very true. But keep at it, you obviously have the basic understanding so it's just a matter of determination to learn, and practice, practice, practice.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

After a long lasting clarification, I have some idea what you want.
If you like to send keystrokes to an other window, you can use SendMessage, since that is the very basic tool of Windows messaging. Look here:[^].
But there is a more convenient way than using P/Invoke: SendKeys class[^].
Look here:

But you might find this also interesting: Sending Keystrokes to another Application in C#[^]
Minazeh 31-Dec-12 11:21am
thank you for this. i will review them and tell if they solved my problem.
Minazeh 31-Dec-12 11:32am
This is a a lot more easy to understand because the links provide a more easy to understand examples. i will accept this as a solution thanks.
Zoltán Zörgő 31-Dec-12 13:17pm
You are welcome, and thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170626.1 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100