home | support | contact | forum

Zhorn Software
stickies | bart | zbar | caffeine | cas | birthday reminder | soundboard | vnchelper










    Code ideas



Stickies API

The Stickies API is accessed by sending text strings to the main Stickies window. The Windows message which is used is WM_COPYDATA which is available from Windows 95 onwards, and is a way of passing small amounts of data directly between two windows.

In return, Stickies will reply with an acknowledgement of the command, and information as to whether the command has worked, and also possibly some data in return.

To use the API, your application will find the Stickies main window, which has a title of "ZhornSoftwareStickiesMain", and then use the Windows API call SendMessage to pass a COPYDATASTRUCT which contains the command to be sent. When Stickies replies, your application will receive a WM_COPYDATA message, again with a COPYDATASTRUCT filled in.

This code to send a message to Stickies in C# is:

[DllImport("user32", EntryPoint = "FindWindowExA")]
private static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);

[DllImport("user32", EntryPoint = "SendMessageA")]
private static extern int ZSendMessage(int Hwnd, int wMsg, int wParam, int lParam);

private const short WM_COPYDATA = 0x4A;

public struct COPYDATASTRUCT
  public int dwData;
  public int cbData;
  public int lpData;

int hWnd = FindWindowEx(0, hWnd, null, "ZhornSoftwareStickiesMain");
string str = "api ping";
IntPtr ptr = Marshal.StringToHGlobalAnsi(str);
cs.dwData = 12345;
cs.lpData = (int)ptr;
cs.cbData = str.Length + 1;
int ret = ZSendMessage(hWnd, WM_COPYDATA, (int)Handle, VarPtr(cs));

...and in C++ using MFC is:

CWnd* pStickiesWnd = CWnd::FindWindow(NULL, "ZhornSoftwareStickiesMain");
CString str = "api ping";
LRESULT copyDataResult;
cpd.dwData = 12345;
cpd.cbData = str.GetLength();
cpd.lpData = (void*)str.GetBuffer(cpd.cbData);
copyDataResult = ::SendMessage(pStickiesWnd, WM_COPYDATA, (WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(), (LPARAM)&cpd);

Neither of these fragments handle the receipt of messages in return - for that code, download one of the sample projects for a working application which can communicate with Stickies

The COPYDATASTRUCT struct has a member dwData. This is an application-specific integer, which can be used as the application wants. The Stickies API uses it to identify replies to a command. When your calling application sends a command to Stickies, the COPYDATASTRUCT sent in reply will have its dwData member filled in with the same value, so that your application can be sure that the message received is a reply to the command just sent, and not an event arriving in the middle of a command.

When an event is received by your application, the dwData member is null.

To continue to learn more about the Stickies API, take a look at the links to the left. Browse the sample projects, and take a look at the commands you send, and events you receive.

© Zhorn Software 2019