sendmessage WM_COPYDATA

Richnl

Well-known member
Joined
Mar 20, 2007
Messages
93
Programming Experience
Beginner
Hi,
I need some help here

I reed about buffers,marshalling, strPtr

I don't know what to fill in for lpdata to send the string to another application. Now it says, it doesn't recognize strPtr
How can I make this right??


VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Structure[/COLOR][/SIZE][SIZE=2] COPYDATASTRUCT[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] dwData [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] cbData [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] lpData [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Structure[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Declare[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] SendMessage [/SIZE][SIZE=2][COLOR=#0000ff]Lib[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]"user32"[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Alias[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]"SendMessageA"[/COLOR][/SIZE][SIZE=2] ( _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] hwnd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] wMsg [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] wParam [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] lParam [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2]) _[/SIZE]
[SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Declare[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] FindWindow [/SIZE][SIZE=2][COLOR=#0000ff]Lib[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]"user32"[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Alias[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]"FindWindowA"[/COLOR][/SIZE][SIZE=2] ( _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] lpClassName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] lpWindowName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) _[/SIZE]
[SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Const[/COLOR][/SIZE][SIZE=2] WM_COPYDATA = &H4A[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Verzenden_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Verzenden.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] hWndREC [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Long[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] MyData [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] COPYDATASTRUCT[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strToSend [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'get handle of receiver [/COLOR][/SIZE]
[SIZE=2]hWndREC = FindWindow(vbNullString, [/SIZE][SIZE=2][COLOR=#a31515]"Receiver"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] hWndREC = 0[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] MsgBox([/SIZE][SIZE=2][COLOR=#a31515]"Start Receiver Program"[/COLOR][/SIZE][SIZE=2], vbOKCancel) = vbCancel [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2]hWndREC = FindWindow(vbNullString, [/SIZE][SIZE=2][COLOR=#a31515]"Receiver"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'string we want to pass [/COLOR][/SIZE]
[SIZE=2]strToSend = [/SIZE][SIZE=2][COLOR=#a31515]"Dutch love ABBA"[/COLOR][/SIZE]
[SIZE=2]MyData.dwData = 100 [/SIZE][SIZE=2][COLOR=#008000]'any number you want [/COLOR][/SIZE]
[SIZE=2]MyData.lpData = strPtr(strToSend)[/SIZE]
[SIZE=2]MyData.cbData = Len(strToSend) * 2 [/SIZE][SIZE=2][COLOR=#008000]'wide data [/COLOR][/SIZE]
[SIZE=2]SendMessage(hWndREC, WM_COPYDATA, [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Handle, MyData)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]

Thanks, Richard
 
There is a "complete" WM_COPYDATA example in post 4 (receive) & post 6 (send) in this thread: http://www.vbdotnetforums.com/showthread.php?t=13943

You'll see the string is converted to bytes with Encoding, memory allocated and the bytes copied there. lpData is set to the pointer of that memory space.
 
I noticed you said that in other thread also. Replied there.
 
If I just wanted to send a string from a windows service to some application wich I can find with FindWindow(...,...)

How would I change the sendmessage structure(see also your example on use of WM_copydata), since I don't have a handle?
What would I do for the wParam argument?

Just sending!

By the way, is there a good book out there for programming such things, sockets, sendmessage, etc , -with visual.basic.net ofcourse.
 
Windows Service don't have windows, no window handle, no gui, no wm_copydata, no message loop. It's kind of a non-environment.
 
but, you can send other types of messages except WM_copydata, am I right?

I want to try sockets now, do you also have a perfect sample for that like WM_copydata? Have not searched the forum yet!

I was just at the bookstore and Pearsons reference guide seem like the only book that elaborated more about ipc. To bad, I came up short to buy the book.
 
Find tutorials on the web. Browse the Net/Sockets forum, keep an eye out for those threads marked with the
paperclip.gif
symbol there, I have posted several simple client-server samples.
 
but, you can send other types of messages except WM_copydata, am I right?

Yes, but what you havent noted so far, i think, is that WM = window message. The app youre targeting has to have a window, for that window to have a message queue, for you to post a message into. Many messages exist, but services dont have a windowing message queue, because they dont have windows.

If youre after a way to do inter-process communication, have you considered remoting?
 
Thanks for your last reply, John I sort of figured out the sockets now

Cjard, with WM, I ment one-way(service to windowApp)
Cjard, I am thinking about remoting just now, because it supposed to be
a little simpler then sockets(no concern about lowlevel io...etc)
-Searching the forum, right now
-Looking for a handy tutorial on how to implement the ipcchannel
-Sofar no luck figuring out how to set it up

It's kind of funny that if you type in remoting, 90% of the threads on how to use ipc end up with "How about remoting?" at the end and then it ends:D

I am reading up on msdn now , but they left out the vb code in the sample
should I work with config files, or not ...and more of these questions

Richard
 
Last edited:
Back
Top