Trying to Play Windows Media Player from C#.NET

A

Anand Ganesh

Hello Everybody,

I am writing a Windows Application to control Windows Media Player which is
required in my project.

I am sending the following code to "Pause" or "Play" the video file in
Windows Media Player. This is equivalent to pressent "Control-P".

For some reason my SendMessage Command is not working. Any suggestions
please?

Thanking you for your time.

Regards
Anand Ganesh

Here is my code

public class Win32

{

[DllImport("User32.dll")]

public static extern Int32 FindWindow(String lpClassName, String
lpWindowName);

[DllImport("User32.dll")]

public static extern UInt32 SendMessage(IntPtr hWnd, UInt32 Msg, UInt32
wParam, UInt32 lParam);

}



private void button1_Click(object sender, EventArgs e)

{

System.Diagnostics.Process[] TheProcessList =
System.Diagnostics.Process.GetProcesses();

foreach (System.Diagnostics.Process EachProcess in TheProcessList)

{

string theProcessName = EachProcess.ProcessName.ToString();

if (theProcessName.ToUpper() == "WMPLAYER")

{

MessageBox.Show("Found Media Player");

uint retval1 = Win32.SendMessage(EachProcess.Handle, 0x100, 0x32, 0x2E631);

uint retval2 = Win32.SendMessage(EachProcess.Handle, 0x101, 0x32, 0x2E631);


}

}

}
 
C

Claes Bergefall

uint retval1 = Win32.SendMessage(EachProcess.Handle, 0x100, 0x32,
0x2E631);

uint retval2 = Win32.SendMessage(EachProcess.Handle, 0x101, 0x32,
0x2E631);

You are using a process handle, SendMessage needs a window handle. Try the
MainWindowHandle property instead.

/claes
 
A

Anand Ganesh

Hi Claes,

Thank you for the response.

I tried this

uint retval1 = Win32.SendMessage(EachProcess.MainWindowHandle, 0x100, 0x32,
0x2E631);
uint retval2 = Win32.SendMessage(EachProcess.MainWindowHandle, 0x101, 0x32,
0x2E631);
but still it is not working.

WM_KEYDOWN is 0x100 then I am passing Control-P - I got those wparam and
lparam from spy++.

Similary for WM_KEYUP

Am I missing something? Still it is not working or in other words I am
expecting it will pause the player but it is not doing it.

Any suggestions please?

Thanks
Anand Ganesh
 
C

Claes Bergefall

I don't think you'll be able to do it that easily. The params for
WM_KEYDOWN/WM_KEYUP should be different to start with. You'll have to check
the docs for it, but at least bit 31 in the lParam paramenter is dependent
on the message.

Check out the Windows Media Player SDK to see if it has something better
that you can use. You'll find it here:
http://msdn2.microsoft.com/en-us/library/aa969732.aspx

/claes
 
A

Anand Ganesh

Thank you for the tips Claes. I tried using SendKeys and sent the shortcut
keys and it worked.

So I am using Win32 API to bring to focus and then using SendKey I am able
to work.

Once again thank you for you valuable time and suggestions.

Regards
Anand
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top