navigationGo.pngQuick Navigation
allprojects32.pngAll projects
hardware32.pngHardware
links32.pngLinks

favoriteStar32.pngTop projects
Alan numitron clock
Clapclap 2313/1386
SNES Pi Webserver
USB Volume/USB toys
Smokey amp
Laser cutter
WordClock
ardReveil v3
SNES Arcade cabinet
Game boy projects
cameleon
Home Presence Detector

github32.pngGitHub
AlanFromJapan

navigationMail.pngContact me

alanfjmail.png
3flags.pngWho's Alan?


Akizukidenshi
Elec-lab
Rand Nerd Tut
EEVblog
SpritesMods
AvrFreaks
Gameboy Dev
FLOZz' blog
Switch-science
Sparkfun
Suzusho
Datasheet Lib
Reddit Elec
Ermicro
Carnet du maker (fr)

Remote controlling Windows media player in C

Last update: Thu Jun 5 22:25:41 2025
kalshagar - Remote controlling Windows media player in C

Remote controlling Windows media player in C#

I mean C# but this wiki doesnt accept # sign in the page name... anyway it is also true in C what I will say.
Thing is to use the message WM_APPCOMMAND that is the one used by all those recent keyboards with extra keys for sending mail, controlling volume, rebooting your pc on one click ... etc. Good thing is that even if you have less power than with the usual WM_COMMAND, the following code is the same with Windows media player, Media player classic, ...and should not change soon. (the current ardTouchConnector works with both the before-mentionned softwares)

Code sample

using System.Runtime.InteropServices;
using System;
 
[DllImport("User32.dll")]
public static extern int FindWindow(string strClassName, string strWindowName);
[DllImport("User32.dll")]
public static extern Int32 SendMessage(
    int hWnd,               // handle to destination window
    int Msg,                // message
    int wParam,             // first message parameter
    int lParam);            // second message parameter
 
private const int WM_APPCOMMAND = 0x0319;
private System.Int32 mWindowHandle = 0;
 
//get the win handle
mWindowHandle = FindWindow("WMPlayerApp", "Windows Media Player");
 
//the command, an int32 that has a small value usually 
int pCommand = APPCOMMAND_MEDIA_PLAY_PAUSE;
 
/*
 How to call the WM_APPCOMMAND:
 * 1- Find the window handle and send the message (usual stuff)
 * 2- 1st parameter of WM_COMMAND (aka the 3rd in the call here under) is 0 (zero)
 * 3- The second parameter (4th in the call) is the constant value having been shifted left by 16 bite (or * 65536, that's the same) 
 * 4- Returns 1 when happy, 0 when not happy
 */
int vCallReturn = SendMessage(mWindowHandle, WM_APPCOMMAND, 0x00000000, pCommand << 16);

A painfully long list of constants


public const int APPCOMMAND_BROWSER_BACKWARD    =  1;
public const int APPCOMMAND_BROWSER_FORWARD     =  2;
public const int APPCOMMAND_BROWSER_REFRESH     =  3;
public const int APPCOMMAND_BROWSER_STOP        =  4;
public const int APPCOMMAND_BROWSER_SEARCH      =  5;
public const int APPCOMMAND_BROWSER_FAVORITES   =  6;
public const int APPCOMMAND_BROWSER_HOME        =  7;
public const int APPCOMMAND_VOLUME_MUTE         =  8;
public const int APPCOMMAND_VOLUME_DOWN         =  9;
public const int APPCOMMAND_VOLUME_UP           =  10;
public const int APPCOMMAND_MEDIA_NEXTTRACK     =  11;
public const int APPCOMMAND_MEDIA_PREVIOUSTRACK =  12;
public const int APPCOMMAND_MEDIA_STOP          =  13;
public const int APPCOMMAND_MEDIA_PLAY_PAUSE    =  14;
public const int APPCOMMAND_LAUNCH_MAIL         =  15;
public const int APPCOMMAND_LAUNCH_MEDIA_SELECT =  16;
public const int APPCOMMAND_LAUNCH_APP1         =  17;
public const int APPCOMMAND_LAUNCH_APP2         =  18;
public const int APPCOMMAND_BASS_DOWN           =  19;
public const int APPCOMMAND_BASS_BOOST          =  20;
public const int APPCOMMAND_BASS_UP             =  21;
public const int APPCOMMAND_TREBLE_DOWN         =  22;
public const int APPCOMMAND_TREBLE_UP           =  23;
public const int APPCOMMAND_MICROPHONE_VOLUME_MUTE = 24;
public const int APPCOMMAND_MICROPHONE_VOLUME_DOWN = 25;
public const int APPCOMMAND_MICROPHONE_VOLUME_UP =  26;
public const int APPCOMMAND_HELP                =  27;
public const int APPCOMMAND_FIND                =  28;
public const int APPCOMMAND_NEW                 =  29;
public const int APPCOMMAND_OPEN                =  30;
public const int APPCOMMAND_CLOSE               =  31;
public const int APPCOMMAND_SAVE                =  32;
public const int APPCOMMAND_PRINT               =  33;
public const int APPCOMMAND_UNDO                =  34;
public const int APPCOMMAND_REDO                =  35;
public const int APPCOMMAND_COPY                =  36;
public const int APPCOMMAND_CUT                 =  37;
public const int APPCOMMAND_PASTE               =  38;
public const int APPCOMMAND_REPLY_TO_MAIL       =  39;
public const int APPCOMMAND_FORWARD_MAIL        =  40;
public const int APPCOMMAND_SEND_MAIL           =  41;
public const int APPCOMMAND_SPELL_CHECK         =  42;
public const int APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE  = 43;
public const int APPCOMMAND_MIC_ON_OFF_TOGGLE   =  44;
public const int APPCOMMAND_CORRECTION_LIST     =  45;
public const int APPCOMMAND_MEDIA_PLAY          =  46;
public const int APPCOMMAND_MEDIA_PAUSE         =  47;
public const int APPCOMMAND_MEDIA_RECORD        =  48;
public const int APPCOMMAND_MEDIA_FAST_FORWARD  =  49;
public const int APPCOMMAND_MEDIA_REWIND        =  50;
public const int APPCOMMAND_MEDIA_CHANNEL_UP    =  51;
public const int APPCOMMAND_MEDIA_CHANNEL_DOWN  =  52;


Thanks

This is all inspired from :
http://www.codeproject.com/KB/cs/wmp_pinvoke.aspx
http://www.eventghost.org/forum/viewtopic.php?f=2&t=319
http://msdn.microsoft.com/en-us/library/bb417079.aspx
http://msdn.microsoft.com/en-us/library/ms646275%28VS.85%29.aspx
http://www.codeguru.com/cpp/w-p/system/keyboard/article.php/c5655/
All content on this site is shared under the MIT licence (do what u want, don't sue me, hat tip appreciated)
electrogeek.tokyo ~ Formerly known as Kalshagar.wikispaces.com and electrogeek.cc (AlanFromJapan [2009 - 2025])