Windows SendMessage

G

Guillaume Marcais

--=-jv07mFm0G5IkB8dyBrcO
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Has anyone ever tried to send messages between applications using the
Windows messaging system (SendMessage call). Given my little knowledge
of Windows, I started with Ruby and Win32API (hopefully it is achievable
that way).

I am trying to discuss with the pageant program from PuTTy to get the
keys. I recompiled pageant with debugging enable so I can see the
request coming in. It works with PuTTy. But with my Ruby app mimicking
what PuTTy does to get the keys, it seems that pageant doesn't even get
the message.

The code is given in the attachement.The stuff about the file mapping
can be ignored. The SendMessage call returns 0 and pageant doesn't
display any debugging information, as if no message whatsoever was ever
received.

Is there any initialization to be done for Windows to agree to dispatch
my message?

Thanks for your help,
Guillaume.


--=-jv07mFm0G5IkB8dyBrcO
Content-Disposition: attachment; filename=pageant.rb
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1

#! /usr/bin/ruby

require 'Win32API'

module Pageant
# From Putty pageant.c
AGENT_MAX_MSGLEN =3D 8192
AGENT_COPYDATA_ID =3D 0x804e50ba
=20
# From Putty ssh.h
SSH_AGENT_FAILURE =3D 5
SSH_AGENT_SUCCESS =3D 6
SSH2_AGENTC_REQUEST_IDENTITIES =3D 11
SSH2_AGENT_IDENTITIES_ANSWER =3D 12
SSH2_AGENTC_SIGN_REQUEST =3D 13
SSH2_AGENT_SIGN_RESPONSE =3D 14
SSH2_AGENTC_ADD_IDENTITY =3D 17
SSH2_AGENTC_REMOVE_IDENTITY =3D 18
SSH2_AGENTC_REMOVE_ALL_IDENTITIES =3D 19

module Win
# From winbase.h, winnt.h
INVALID_HANDLE_VALUE =3D -1
NULL =3D nil
PAGE_NOACCESS =3D 0x0001
PAGE_READONLY =3D 0x0002
PAGE_READWRITE =3D 0x0004
PAGE_WRITECOPY =3D 0x0008
PAGE_EXECUTE =3D 0x0010
PAGE_EXECUTE_READ =3D 0x0020
PAGE_EXECUTE_READWRITE =3D 0x0040
PAGE_EXECUTE_WRITECOPY =3D 0x0080
PAGE_GUARD =3D 0x0100
PAGE_NOCACHE =3D 0x0200
FILE_MAP_ALL_ACCESS =3D 0xf001f
FILE_MAP_READ =3D 4
FILE_MAP_WRITE =3D 2
FILE_MAP_COPY =3D 1
WM_COPYDATA =3D 74

# CreateWindow =3D Win32API.new('user32', 'CreateWindow',
# ["P", "P", "L", "L", "L", "L", "L",
# "L", "L", "L", "P"], "L")
FindWindow =3D Win32API.new('user32', 'FindWindow', ["P", "P"], "L")
GetCurrentThreadId =3D Win32API.new('kernel32', 'GetCurrentThreadId',=20
[], "L")
CreateFileMapping =3D Win32API.new('kernel32', 'CreateFileMapping',=20
["L", "P", "L", "L", "L", "P"], "L")
MapViewOfFile =3D Win32API.new('kernel32', 'MapViewOfFile',
["L", "L", "L", "L", "L"], "P")
UnmapViewOfFile =3D Win32API.new('kernel32', 'UnmapViewOfFile',
["P"], "V")
CloseHandle =3D Win32API.new('kernel32', 'CloseHandle', ["L"], "V")
SendMessage =3D Win32API.new('user32', 'SendMessage',=20
["L", "L", "P", "P"], "L")
end

class Pageant
def initialize
@win =3D Win::FindWindow.call("Pageant", "Pageant")
raise "Pageant not started" if @win =3D=3D 0
end
=20
def send_query(query)
res =3D nil
mapname =3D "PageantRequest%08x\000" % Win::GetCurrentThreadId.call()
# p mapname
filemap =3D Win::CreateFileMapping.call(Win::INVALID_HANDLE_VALUE,=20
Win::NULL,
Win::pAGE_READWRITE, 0,=20
AGENT_MAX_MSGLEN, mapname)
raise "Creation of file mapping failed" if filemap =3D=3D 0
ptr =3D Win::MapViewOfFile.call(filemap, Win::FILE_MAP_WRITE, 0, 0,=20
AGENT_MAX_MSGLEN)
cds =3D [AGENT_COPYDATA_ID, mapname.size, mapname].pack("NNp")
p [cds].pack("p").unpack("L")[0]
p @win
id =3D Win::SendMessage.call(@win, Win::WM_COPYDATA, Win::NULL, cds)
p id
if id > 0
retlen =3D 4 + ptr.unpack("N")
p retlen
res =3D ptr.unpack("p#{retlen}")
end =20
Win::UnmapViewOfFile.call(ptr)
Win::CloseHandle.call(filemap)
res
end

def request_identities
request =3D [1, SSH2_AGENTC_REQUEST_IDENTITIES].pack("Nc")
# p request
res =3D send_query(request)
res
end
end
end

pageant =3D Pageant::pageant.new
$stdout.puts("Requesting identities")
ids =3D pageant.request_identities
$stdout.puts("Press Enter to end")
$stdin.gets

--=-jv07mFm0G5IkB8dyBrcO--
 
T

Tim Sutherland

Guillaume Marcais said:
Has anyone ever tried to send messages between applications using the
Windows messaging system (SendMessage call).

Yes, I have done this. http://www.windows-spy.com/ is a useful tool for
seeing what messages are passed under the covers when the user does the
"usual thing".
Given my little knowledge
of Windows, I started with Ruby and Win32API (hopefully it is achievable
that way).

DL is nicer. (See below!)
I am trying to discuss with the pageant program from PuTTy to get the
keys. I recompiled pageant with debugging enable so I can see the
request coming in. It works with PuTTy. But with my Ruby app mimicking
what PuTTy does to get the keys, it seems that pageant doesn't even get
the message.

The code is given in the attachement.The stuff about the file mapping
can be ignored. The SendMessage call returns 0 and pageant doesn't
display any debugging information, as if no message whatsoever was ever
received.

Is there any initialization to be done for Windows to agree to dispatch
my message?

No initialisation is needed that is not already done by Ruby.

[...]
require 'Win32API' [...]
cds = [AGENT_COPYDATA_ID, mapname.size, mapname].pack("NNp")

'N' means network byte order. Use 'L' for native byte order. Since you are
using Windows, you are probably using x86 (little-endian) while network byte
order is big-endian.

If you use the 'dl' library you don't need to worry about this sort of thing.

e.g. can just do (untested)


require 'dl/import'
require 'dl/struct'

module Win32
extend DL::Importable

dlload 'user32'

CopyData = struct [
'ULONG *dwData',
'DWORD cbData',
'PVOID lpData'
]

WM_COPYDATA = 1234 # whatever it is

extern 'BOOL SendMessage(HWND, UINT, WPARAM, LPARAM)'
end

cds = CopyData.malloc
cds.dwData = Win32::AGENT_COPYDATA_ID
cds.cbData = mapname.size
cds.lpData = mapname

Win32::sendMessage(@win, Win32::WM_COPYDATA, nil, cds.to_i)

puts("Now isn't that easy :)")
 
T

Tim Sutherland

Guillaume Marcais said:
Given my little knowledge
of Windows, I started with Ruby and Win32API (hopefully it is achievable
that way).

DL is nicer. (See below!) [...]
If you use the 'dl' library you don't need to worry about this sort of thing.
[...]

Guillaume,

You may be interested in Benjamin Peterson's recent translation of the Ruby/DL
tutorial from Japanese into English: http://raa.ruby-lang.org/project/rdl_en/

I also want to take this opportunity to say "thank you!" to
Takaaki Tateishi for creating Ruby/DL. It is such a fantastic library, my
favourite Ruby 1.8 feature.
 
G

Guillaume Marcais

Guillaume Marcais said:
Given my little knowledge
of Windows, I started with Ruby and Win32API (hopefully it is achievable
that way).

DL is nicer. (See below!) [...]
If you use the 'dl' library you don't need to worry about this sort of thing.
[...]

Thanks for the hints, I'll try it ASAP.
Guillaume,

You may be interested in Benjamin Peterson's recent translation of the Ruby/DL
tutorial from Japanese into English: http://raa.ruby-lang.org/project/rdl_en/

I actually encouraged him to go ahead when he asked if anybody was
interested. I am definitly going to read now that it is out.

Guillaume.
 
T

Tim Sutherland

Tim Sutherland wrote: said:
extern 'BOOL SendMessage(HWND, UINT, WPARAM, LPARAM)'

extern 'LONG SendMessage(HWND, UINT, UINT, LONG)'


Sorry for multiple posts. For document, see also dl.txt in the
Ruby distribution. I once did a presentation on using Ruby/DL
to automate Windows GUI programs, see

http://www.sdkacm.com/static/events/12-March-2004/slides.sxi

(OpenOffice.org format.)

The slides are probably not very useful without the demo
programs / code I was also using, halfway through they start
saying things like
[demo foo]

[talk about bar]

but maybe someone will find it useful.

[...]
 
G

Guillaume Marcais

Thanks for your help. It allowed me finish the link to pageant, now
release with Net-SSH :).

Guillaume.

Tim Sutherland wrote: said:
extern 'BOOL SendMessage(HWND, UINT, WPARAM, LPARAM)'

extern 'LONG SendMessage(HWND, UINT, UINT, LONG)'


Sorry for multiple posts. For document, see also dl.txt in the
Ruby distribution. I once did a presentation on using Ruby/DL
to automate Windows GUI programs, see

http://www.sdkacm.com/static/events/12-March-2004/slides.sxi

(OpenOffice.org format.)

The slides are probably not very useful without the demo
programs / code I was also using, halfway through they start
saying things like
[demo foo]

[talk about bar]

but maybe someone will find it useful.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top