Automating applications using messages
Written by Harry Fairhead   
Thursday, 16 July 2009
Article Index
Automating applications using messages
Getting started
Finding a dialog
Opening a file
Doing the Demux

Opening a file

Given the getDialog method we can now move on to make use of any dialogs we open. In this case it’s an Open File dialog box with title “Open”:

IntPtr OpenHwnd = getDialog(

We now need to enter text into the dialog’s edit control which Winspector reveals has a control ID of 1152. Its window handle is:

IntPtr EditHwnd = GetDlgItem(

We can use the WM_SETTEXT message to send a string to any edit control – the code for which is:

const uint WM_SETTEXT = 0xC;

We also need a slightly different definition of SendMessage to cope with a pointer to a string:

CharSet = CharSet.Auto,
SetLastError = false)]
static extern IntPtr SendMessage(
HandleRef hWnd,
uint Msg,
IntPtr wParam,
[Out, In] StringBuilder lParam);

You can enter both definitions and the system will work out which to use in any particular case. We could now just send the text, but even though the dialog and its edit control exist they could be still in the middle of initialising. This would mean that we could write the text successfully only to have it blanked out again as soon as initialisation is completed! The correct way of dealing with this is to enquire what the state of the dialog is or to set the text and then check that it is indeed set (using a GetText message).

The simplest solution is to just yield control to the other threads in the system for a tenth of a second or more:

StringBuilder file =
new StringBuilder(
IntPtr set = SendMessage(
new HandleRef(VobEdit,

The file name entered into the dialog box is the default name for the first VOB making up the main program on a DVD in drive D.

Finally we can click the Open button, control ID 1, by sending it a WM_COMMAND message:

IntPtr DlgOpenBtnHwnd = 
new HandleRef(VobEdit, OpenHwnd),
new IntPtr(1),

This closes the dialog and returns its results to the application.





Last Updated ( Sunday, 19 July 2009 )