Send message to Skype contact using C#

Let’s see how can we send a message in Skype from a C# application. I chose skype just for the demonstration. You could pick any application and do anything with the following code.

SendMessage – Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.

EnumChildWindows – Enumerates the child windows that belong to the specified parent window by passing the handle to each child window, in turn, to an application-defined callback function. EnumChildWindows continues until the last child window is enumerated or the callback function returns FALSE.

GetClassName – Retrieves the name of the class to which the specified window belongs.

//...
private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);
private const uint WM_SETTEXT = 0x000C;
private const uint WM_KEYDOWN = 0x100;
 
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);
 
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, Int32 wParam, Int32 lParam);
 
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam);
 
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
 
private List<IntPtr> _EnumChildrenHandles(IntPtr handle)
{
    try
    {
	List<IntPtr> _childrenHandlesList = new List<IntPtr>();
	GCHandle _gcchildrenHandlesList = GCHandle.Alloc(_childrenHandlesList);
	IntPtr _pchildrenHandlesList = GCHandle.ToIntPtr(_gcchildrenHandlesList);
	try
	{
		EnumWindowProc _childProc = new EnumWindowProc(_EnumWindow);
		EnumChildWindows(handle, _childProc, _pchildrenHandlesList);
	}
	finally
	{
		_gcchildrenHandlesList.Free();
	}
	return _childrenHandlesList;
    }
    catch { return null; }
}
 
private bool _EnumWindow(IntPtr hWnd, IntPtr lParam)
{
    try
    {
	GCHandle _childrenHandles = GCHandle.FromIntPtr(lParam);
	if (_childrenHandles == null || _childrenHandles.Target == null)
		return false;
	List<IntPtr> _childrenHandlesList = _childrenHandles.Target as List<IntPtr>;
	_childrenHandlesList.Add(hWnd);
	return true;
    }
    catch { return false; }
}
 
private Boolean detective(Process pp, String window_title, String message, String objclassname)
{
    try
    {
	if (pp.MainWindowTitle.Contains(window_title))
	{
	    List<IntPtr> l = _EnumChildrenHandles(pp.MainWindowHandle);
    	    foreach (IntPtr hndl in l)
	    {
		StringBuilder stringBuilder = new StringBuilder(255);
		GetClassName(hndl, stringBuilder, stringBuilder.Capacity);
		if (stringBuilder.ToString() == objclassname)
		{
			SendMessage(hndl, WM_SETTEXT, IntPtr.Zero, message);
			SendMessage(hndl, WM_KEYDOWN, 13, 0);
			return true;
		}
	    }
	}
	return false;
    }
    catch { return false; }
}

Usage:

foreach (Process pp in Process.GetProcesses())
{
    detective(pp, "Skype", "hello there!", "TChatRichEdit");
}
Categories: C# Tags: , , ,
%d bloggers like this: