From: Guillaume Marcais Date: 2004-12-01T01:52:24+09:00 Subject: Windows SendMessage --=-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--