Archive for November, 2005

11.12.05

Master of Messenger

Posted in Delphi at 10:11 am by kkj

I know. The world is black and white, truth is absolute, and Java developers don’t do Windows programming. So what am I, a J2EE proponent, doing on a rainy Friday messing about with Borland Delphi? And even worse, using the tool for doing Windows programming against another (yrk) Microsoft product, MSN Messenger? Well, there you have it. I confess. My dim past includes exactly that: coding against those Windows extensions exposed by a wide array of applications, in Delphi, and liking it!

The power you can exert with a few lines of code on Windows is tremendous. Word, Excel, Messenger, Explorer: all puppets on strings. So given this, what have I used it for? Well … Ahem … Nothing serious, I am afraid. Before going to Oopsla, I decided to annoy those of my friends who are on my MSN buddy list by writing a scroll text application that would childishly display quotes from various songs about California accompanied by a live countdown ticker in my personal message pane …

California Dreaming … 14569 minutes to go …

Immature? Probably! Fun? Definitely :-)

To control MSN Messenger 7.5 you can either use the COM interface to do things like accessing the buddy list, send messages to people, etc. or you can use the Windows messaging interface to control the personal message. While I started out thinking I would need the COM interface, the final application turned out to use merely message passing as a means to control the Messenger.

For the application to work, you must enable the “What am I listening to” feature, which is intended for displaying songs played by Windows Media Player. Once on, Messenger will faithfully display any text sent via Windows messaging in the right format. The magic lies within the following routine:

procedure SetMsnMessage(category: string; msg: string; enabled: boolean );
var
  handleMSN:THandle;
  structCopy:TCopyDataStruct;
  stringBuffer:array [0..127] of WideChar;
  sEnabled : string;
begin
  FillChar(stringBuffer,SizeOf(stringBuffer),#0);

  if(enabled) then sEnabled := ‘1′ else sEnabled := ‘0′;

  StringToWideChar(’\0′ + category + ‘\0′+ sEnabled +’\0′+’{0}’+'\0′+ msg +’\0\0\0\0′+#0,@stringBuffer[0],128);

  FillChar(structCopy,SizeOf(TCopyDataStruct),#0);
  with structCopy do
  begin
    cbData:=SizeOf(stringBuffer);
    dwData:=$547;
    lpData:=@stringBuffer[0];
  end;

  handleMSN:=FindWindowEx(0,0,’MsnMsgrUIManager’,nil);
  SendMessage(handleMSN, WM_COPYDATA, 0, Integer(@structCopy));
end;

The application comes as a standalone executable and with the full source written using Borland Delphi 2005 for free, without warranties, with no strings attached, free of viruses, and with no support of any kind. Guaranteed.