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

Doing the Demux

That’s completed the task of setting the file we want to process in the Open file dialog. Next we need to set the processing conditions and the location of the output file.

To do this we send a message that clicks the Demux button. This is just more of the same but there is a subtle problem. After the Open dialog is dismissed the application goes off to find if the file actually exists and to read its details. If we click the Demux button before it has finished the file open action is aborted and nothing works.

The application really shouldn’t allow this to happen – it should disable the Demux button until the file open is complete as a user with a lightening speed mouse click could fall into the same trap as our automation program.

The correct solution to this problem is to read the text from the edit control that displays the file details. The easy solution is just to wait a second or so for the file open to complete and then click the button and get the dialog window handle:

Thread.Sleep(1000);
IntPtr DemuxBtnHwnd =
GetDlgItem(Hwnd, 1005);
SendNotifyMessage(
new HandleRef(VobEdit, Hwnd),
WM_COMMAND,
new IntPtr(1005),
DemuxBtnHwnd);
Thread.Sleep(100);
IntPtr DemuxHwnd =
getDialog(Hwnd, "Dialog",5000);

You might need to wait longer than one second if the file open involves a slow DVD drive.

Now we have the Demux dialog open and we need to set the “Demux all Video streams” and “Demux all Audio streams” check box controls – control ID 1032 and 1031.

To set the controls we need to use a new message BM_SETSTATE:

const uint BM_SETSTATE = 0xF1;

This message sets or unsets the control depending on the value of the wParam (=1 sets =0 unsets) and the message has to be sent to each control’s window:

IntPtr AllVidBtn = 
GetDlgItem(DemuxHwnd, 1032);
IntPtr AllAudBtn =
GetDlgItem(DemuxHwnd, 1031);
SendMessage(
new HandleRef(VobEdit, AllVidBtn),
BM_SETSTATE,
new IntPtr(1),
IntPtr.Zero);
SendMessage(
new HandleRef(VobEdit, AllAudBtn),
BM_SETSTATE,
new IntPtr(1),
IntPtr.Zero);

This is all we need to do with this dialog box and we can move on by sending a message to click its OK button, control ID 1:

IntPtr OkBtn = GetDlgItem(DemuxHwnd, 1);
SendMessage(
new HandleRef(VobEdit, DemuxHwnd),
WM_COMMAND,
new IntPtr(1),
OkBtn);

The final stage

Dismissing the Demux dialog generates the Save As dialog and we simply enter the path name and file name into the edit control, control ID 1152 using the same technique developed for the Open dialog:

IntPtr SaveAsHwnd = 
getDialog(Hwnd, "Save As",5000);
StringBuilder outfile =
new StringBuilder(
@"C:\My Videos\VTS_01_1.n.xxx");
IntPtr SaveEditHwnd =
GetDlgItem(SaveAsHwnd, 1152);
set = SendMessage(
new HandleRef(VobEdit, SaveEditHwnd),
WM_SETTEXT,
IntPtr.Zero,
outfile);
IntPtr SaveBtn = GetDlgItem(
SaveAsHwnd, 1);
SendMessage(
new HandleRef(VobEdit, SaveAsHwnd),
WM_COMMAND,
new IntPtr(1),
SaveBtn);

As soon as the Save button is pressed the VOBedit starts to demultiplex the VOB files, joining their content together to form a single MPEG 2 video and AC3 audio file.

Now we can just sit back and wait for the application to finish. Detecting when it has finished is usually not difficult but it varies according to the application. In this case a window appears showing the progress made on converting the file. Simply testing to see when this window disappears is enough to tell when it has finished but you could also retrieve the % complete text in the title bar.

To close the application simply use the Kill method of the process class or send a message to the Close button on the main form – is up to you.

Clearly there are lots of applications for this approach to controlling and extending existing applications. When you also realise that it’s fairly easy to intercept messages intended for other windows as well as send them it gets even more exciting.

If you would like the code for this project then register and click on CodeBin.
<ASIN:1844251187>

<ASIN:1844250318>

<ASIN:159327064X>

 

 



Last Updated ( Sunday, 19 July 2009 )