Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
Minazeh165
Rate this: bad
good
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.
  Permalink  
Comments
Minazeh at 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 at 30-Dec-12 12:10pm
   
It's not something I have done, but take a look at some of these links.
Minazeh at 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 at 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 at 30-Dec-12 12:34pm
   
maybe i should, but one more question.. related in the code above..the..
 
ByVal wParam As IntPtr
and
ByVal lParam As IntPtr
 
what do they do?...
Zoltán Zörgő at 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 at 30-Dec-12 12:44pm
   
oh. can you tell me something that is more suited?
Zoltán Zörgő at 30-Dec-12 12:50pm
   
You want to show a popup from a windows forms application?
Minazeh at 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ő at 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 at 30-Dec-12 13:04pm
   
yes. something like that.
Zoltán Zörgő at 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 at 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ő at 31-Dec-12 3:19am
   
Any progress?
Minazeh at 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 at 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 at 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 at 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
good
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: http://improve.dk/archive/2007/04/06/sending-keypresses-to-a-window.aspx[^].
But there is a more convenient way than using P/Invoke: SendKeys class[^].
Look here:
- http://www.devx.com/dotnet/Article/7914/0/page/5[^]
- http://www.java2s.com/Tutorial/VB/0260__GUI/SendKeys.htm[^]
- http://www.java2s.com/Code/VB/GUI/UsingSendKeystosimulatekeypress.htm[^]
 
But you might find this also interesting: Sending Keystrokes to another Application in C#[^]
  Permalink  
Comments
Minazeh at 31-Dec-12 11:21am
   
thank you for this. i will review them and tell if they solved my problem.
Minazeh at 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ő at 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
0 Sergey Alexandrovich Kryukov 458
1 OriginalGriff 344
2 George Jonsson 233
3 Animesh Datta 130
4 Shemeemsha RA 128
0 OriginalGriff 6,179
1 Sergey Alexandrovich Kryukov 5,616
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2014
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