unit Unit1;
{$mode objfpc}{$H+}
interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Type
{ TMainForm }
TMainForm = Class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure onCreate(Sender: TObject);
Private
//Function SkypeProcessMessages(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall;
//Procedure SendCommand (Cmd : String);
//Procedure RecvCommand (Cmd : String);
End;
var
MainForm: TMainForm;
var
PrevWndProc: WNDPROC;
MsgAttach: LongWord;
MsgDiscover: LongWord;
SkypeWindowHandle: HWND;
implementation
{$R *.lfm}
{ TMainForm }
Procedure RecvCommand(Cmd: string);
Begin
MainForm.Memo1.Lines.Add('-> ' + Cmd);
End;
Procedure SendCommand(Cmd: string);
var
CopyData : CopyDataStruct;
begin
if SkypeWindowHandle = 0 Then Exit;
CopyData.dwData := 0;
CopyData.cbData := Length(Cmd) + 1;
CopyData.lpData := PChar(Cmd);
SendMessage(SkypeWindowHandle, WM_COPYDATA, Application.MainForm.Handle, LPARAM(@CopyData));
End;
function SkypeProcessMessages(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall;
begin
//if uMsg=WM_NCHITTEST then
//begin
// result:=Windows.DefWindowProc(Ahwnd, uMsg, WParam, LParam); //not sure about this one
// if result=windows.HTCAPTION then result:=windows.HTCLIENT;
// exit;
//end;
// MsgAttach sort of message. This we will get as resoponse from Skype
// to MsgDiscover broadcast. WParam will contain Skype API window handle
// (which is not necessarily Skype's UI main window handle). That handle
// we can use to send further messages directly to Skype, instead of
// broadcasting. LParam will contain current handshake status code.
if (uMsg = MsgAttach) Then
Begin
SkypeWindowHandle := wParam;
Case lParam of
0 : Begin
MainForm.Memo1.Lines.Add('** Attach success');
SendCommand('protocol 999');
SendCommand('get skypeversion');
End;
1 : MainForm.Memo1.Lines.Add('** Attach pending');
2 : MainForm.Memo1.Lines.Add('** Attach refused');
3 : MainForm.Memo1.Lines.Add('** Attach unavailable');
End;
Result := 1;
Exit;
End;
// Here we have recieved a notification message from Skype.
if (uMsg = WM_COPYDATA) and (HWND(wParam) = SkypeWindowHandle) Then
Begin
// The LParam contains a pointer to a TCopyDataStruct record.
// lpData field of that record conatins pointer to a null-terminated string.
// Through typecasting, we will pass that string to our RecvCommand procedure,
// where further processing can take place.
RecvCommand(PChar(PCopyDataStruct(LParam)^.lpData));
// Setting the Msg.Result is important here, to notify Skype that the
// message has been processed.
//Msg.Result := 1;
Result := 1;
Exit;
End;
result:=CallWindowProc(PrevWndProc,Ahwnd, uMsg, WParam, LParam);
end;
{ TMainForm }
procedure TMainForm.Edit1KeyPress(Sender: TObject; var Key: char);
begin
if Key = #13 then
Begin
Memo1.Lines.Add('<- ' + Edit1.Text);
SendCommand(Edit1.Text);
Edit1.Clear;
End;
end;
procedure TMainForm.onCreate(Sender: TObject);
begin
SkypeWindowHandle := 0;
// Here we will get back message type IDs
MsgAttach := RegisterWindowMessage('SkypeControlAPIAttach');
MsgDiscover := RegisterWindowMessage('SkypeControlAPIDiscover');
// ProcessMessage will handle incoming messages from Skype client
//Application.HookMainWindow(ProcessMessages);
PrevWndProc:=Windows.WNDPROC(SetWindowLong(Application.MainForm.Handle,GWL_WNDPROC,PtrInt(@SkypeProcessMessages)));
// Broadcasting all over the system that this application wants to
// attach itself to Skype public API. Response from Skype will be
// handled in ProcessMessages.
PostMessage(HWND_BROADCAST, MsgDiscover, Application.MainForm.Handle, 0);
end;
end.