Sign in to follow this  
Followers 0
ZoSTeR

Media Player Classic - Homecinema MPC remote API (via WM_COPYDATA)

11 posts in this topic

#1 ·  Posted (edited)

This only works with the Homecinema branch of Media Player Classic (link).

Refer to the MpcApi.h file in the SVN for details and updates (link log)

The remote control API works by sending WM_COPYDATA messages in both directions. The source should be pretty self explanatory.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <SendMessage.au3>
#Include <WinAPI.au3>

; FROM MpcApi.h :
;
;~ // This file define commands used for "Media Player Classic - Homecinema" API. To send commands
;~ // to mpc-hc, and receive playback notifications, first launch process with the /slave command line
;~ // argument follow by an HWnd handle use to receive notification :
;~ //
;~ // ..\bin\mplayerc /slave 125421
;~ //
;~ // After startup, mpc-hc send a WM_COPYDATA message to host with COPYDATASTRUCT struct filled with :
;~ //       - dwData    : CMD_CONNECT
;~ //       - lpData    : Unicode string containing mpc-hc main window Handle
;~ //
;~ // To pilot mpc-hc, send WM_COPYDATA messages to Hwnd provided on connection. All messages should be
;~ // formatted as Unicode strings. For commands or notifications with multiple parameters, values are
;~ // separated by |
;~ // If a string contains a |, it will be escaped with a \ so a \| is not a separator
;~ //
;~ // Ex : When a file is openned, mpc-hc send to host the "now playing" notification :
;~ //       - dwData    : CMD_NOWPLAYING
;~ //       - lpData    : title|author|description|filename|duration

;~ #pragma once


;~ typedef enum MPC_LOADSTATE
;~ {
;~  MLS_CLOSED, 
;~  MLS_LOADING, 
;~  MLS_LOADED, 
;~  MLS_CLOSING
;~ };


;~ typedef enum MPC_PLAYSTATE
;~ {
;~  PS_PLAY   = 0,
;~  PS_PAUSE  = 1,
;~  PS_STOP   = 2,
;~  PS_UNUSED = 3
;~ };



;==== Commands from MPC to host
    
;   // Send after connection
;   // Par 1 : MPC window handle (command should be send to this HWnd)
Global Const $CMD_CONNECT = 0x50000000

;   // Send when opening or closing file
;   // Par 1 : current state (see MPC_LOADSTATE enum)
Global Const $CMD_STATE = 0x50000001

;   // Send when playing, pausing or closing file
;   // Par 1 : current play mode (see MPC_PLAYSTATE enum)
Global Const $CMD_PLAYMODE = 0x50000002

;   // Send after opening a new file
;   // Par 1 : title
;   // Par 2 : author
;   // Par 3 : description
;   // Par 4 : complete filename (path included)
;   // Par 5 : duration in seconds
Global Const $CMD_NOWPLAYING = 0x50000003

;   // List of subtitle tracks
;   // Par 1 : Subtitle track name 0
;   // Par 2 : Subtitle track name 1
;   // ...
;   // Par n : Active subtitle track, -1 if subtitles disabled
;   //
;   // if no subtitle track present, returns -1
;   // if no file loaded, returns -2
Global Const $CMD_LISTSUBTITLETRACKS = 0x50000004

;   // List of audio tracks
;   // Par 1 : Audio track name 0
;   // Par 2 : Audio track name 1
;   // ...
;   // Par n : Active audio track
;   //
;   // if no audio track present, returns -1
;   // if no file loaded, returns -2
Global Const $CMD_LISTAUDIOTRACKS = 0x50000005

;   // List of files in the playlist
;   // Par 1 : file path 0
;   // Par 2 : file path 1
;   // ...
;   // Par n : active file, -1 if no active file
Global Const $CMD_PLAYLIST = 0x50000006


; ====================================================================
; ==== Commands from host to MPC
    
;   // Open new file 
;   // Par 1 : file path
Global Const $CMD_OPENFILE = 2684354560 ; 0xA0000000

;   // Stop playback, but keep file / playlist
Global Const $CMD_STOP = 2684354561 ; 0xA0000001

;   // Stop playback and close file / playlist
Global Const $CMD_CLOSEFILE = 2684354562 ; 0xA0000002

;   // Pause or restart playback
Global Const $CMD_PLAYPAUSE = 0xA0000003

;   // Add a new file to playlist (did not start playing)
;   // Par 1 : file path
Global Const $CMD_ADDTOPLAYLIST = 0xA0001000

;   // Remove all files from playlist
Global Const $CMD_CLEARPLAYLIST =0xA0001001

;   // Start playing playlist
Global Const $CMD_STARTPLAYLIST = 0xA0001002

;   CMD_REMOVEFROMPLAYLIST  = 0xA0001003,   // TODO

;   // Cue current file to specific position
;   // Par 1 : new position in seconds
Global Const $CMD_SETPOSITION = 0xA0002000

;   // Set the audio delay
;   // Par 1 : new audio delay in ms
Global Const $CMD_SETAUDIODELAY = 0xA0002001

;   // Set the subtitle delay
;   // Par 1 : new subtitle delay in ms
Global Const $CMD_SETSUBTITLEDELAY = 0xA0002002

;   // Set the active file in the playlist
;   // Par 1 : index of the active file, -1 for no file selected
;   // DOESN'T WORK
Global Const $CMD_SETINDEXPLAYLIST = 0xA0002003

;   // Set the audio track
;   // Par 1 : index of the audio track
Global Const $CMD_SETAUDIOTRACK = 0xA0002004

;   // Set the subtitle track
;   // Par 1 : index of the subtitle track, -1 for disabling subtitles
Global Const $CMD_SETSUBTITLETRACK = 0xA0002005

;   // Ask for a list of the subtitles tracks of the file
;   // return a CMD_LISTSUBTITLETRACKS
Global Const $CMD_GETSUBTITLETRACKS = 0xA0003000

;   // Ask for a list of the audio tracks of the file
;   // return a CMD_LISTAUDIOTRACKS
Global Const $CMD_GETAUDIOTRACKS = 0xA0003001
    
;   // Ask for the properties of the current loaded file
;   // return a CMD_NOWPLAYING
Global Const $CMD_GETNOWPLAYING = 0xA0003002

;   // Ask for the current playlist
;   // return a CMD_PLAYLIST
Global Const $CMD_GETPLAYLIST = 0xA0003003

;== End MPC Commands



Global $gs_messages
Global $ghnd_MPC_handle

GUIRegisterMsg($WM_COPYDATA, "__Msg_WM_COPYDATA")

; GUI start
$Form1 = GUICreate("MPC-HC remote demo", 608, 408, 180, 120)
$Edit1 = GUICtrlCreateEdit("", 16, 16, 577, 321)
$Button_open = GUICtrlCreateButton("Open File", 24, 368, 140, 28, 0)
$Button_play = GUICtrlCreateButton("Play / Pause", 224, 368, 140, 28, 0)
$Button_stop = GUICtrlCreateButton("Stop", 424, 368, 140, 28, 0)
GUISetState(@SW_SHOW)
; GUI end


; Run MPC-HC with our GUI form handle

$s_formHandle = Dec(StringMid(String($Form1),3))
$pid_MPC = Run("mplayerc.exe /slave " & $s_formHandle)

If $pid_MPC = 0 Then
    MsgBox(0,"Error","Could not run Mplayerc.exe (Path?)")
    Exit
EndIf

;GUI loop
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button_open
            $s_FileToOpen = FileOpenDialog("Open Media File","C:","Media files (*.*)")
            __MPC_send_message($ghnd_MPC_handle, $CMD_OPENFILE, $s_FileToOpen)
        Case $Button_play
            __MPC_send_message($ghnd_MPC_handle, $CMD_PLAYPAUSE, "")
        Case $Button_stop
            __MPC_send_message($ghnd_MPC_handle, $CMD_STOP, "")
    EndSwitch
WEnd


Func __MPC_send_message($prm_hnd_MPC, $prm_MPC_Command,$prm_MPC_parameter)
    Local $StructDef_COPYDATA
    Local $st_COPYDATA
    Local $p_COPYDATA
    Local $st_CDString
    Local $p_CDString
    Local $i_StringSize
    
    $StructDef_COPYDATA = "dword dwData;dword cbData;ptr lpData"; 32 bit command; length of string; pointer to string
    $st_COPYDATA = DllStructCreate($StructDef_COPYDATA) ; create the message struct
    
    $i_StringSize = StringLen($prm_MPC_parameter) + 1 ; +1 for null termination
    $st_CDString = DllStructCreate("wchar var1[" & $i_StringSize & "]") ; the array to hold the unicode string we are sending
    DllStructSetData($st_CDString,1,$prm_MPC_parameter) ; set parameter string
    DllStructSetData($st_CDString,1,0,$i_StringSize) ; null termination
    $p_CDString = DllStructGetPtr($st_CDString) ; the pointer to the string
    
    DllStructSetData($st_COPYDATA, "dwData", $prm_MPC_Command) ; dwData 32 bit command
    DllStructSetData($st_COPYDATA, "cbData", $i_StringSize * 2) ; size of string * 2 for unicode
    DllStructSetData($st_COPYDATA, "lpData", $p_CDString) ; lpData pointer to data
    $p_COPYDATA = DllStructGetPtr($st_COPYDATA) ; pointer to COPYDATA struct
    
    _SendMessage($prm_hnd_MPC,$WM_COPYDATA,0,$p_COPYDATA)
   
    $st_COPYDATA = 0 ; free the struct
    $st_CDString = 0 ; free the struct
EndFunc


Func __Msg_WM_COPYDATA($hWnd, $Msg, $wParam, $lParam)
        ; Receives WM_COPYDATA message from MPC
        ; $LParam = pointer to a COPYDATA struct
        Local $StructDef_COPYDATA
        Local $st_COPYDATA
        Local $StructDef_DataString
        Local $st_DataString
        Local $s_DataString
        Local $s_dwData, $s_cbData, $s_lpData
        
        $StructDef_COPYDATA = "dword dwData;dword cbData;ptr lpData"
        $StructDef_DataString = "char DataString"
        $st_COPYDATA = DllStructCreate($StructDef_COPYDATA, $LParam)
        $s_dwData = DllStructGetData($st_COPYDATA,"dwData") ; 32bit MPC command
        $s_cbData = DllStructGetData($st_COPYDATA,"cbData") ; length of DataString
        $s_lpData = DllStructGetData($st_COPYDATA,"lpData") ; pointer to DataString
        
        $StructDef_DataString = "wchar DataString[" & int($s_cbData) & "]" ;unicode string with length cbData
        
        $st_DataString = DllStructCreate($StructDef_DataString,$s_lpData)
        $s_DataString = DllStructGetData($st_DataString,"DataString")
        
        ; If we receive a 0x50000000 CMD_CONNECT we retrieve the MPC handle
        If $s_dwData = 1342177280 Then
            $ghnd_MPC_handle = $s_DataString
        EndIf
        
        $gs_messages = __MPC_Command_to_Text($s_dwData) & @CRLF & "Data: " & $s_DataString & @CRLF & $gs_messages
        GUICtrlSetData($Edit1, $gs_messages)

EndFunc

Func __MPC_Command_to_Text($prm_MPC_Command)
    ; Converts an MPC command code to plain text; accepts the decimal value of the command
    
    Switch $prm_MPC_Command
        Case 1342177280     ;0x50000000
            $s_return_msg = "CMD_CONNECT"
        Case 1342177281     ;0x50000000
            $s_return_msg = "CMD_STATE"
        Case 1342177282     ;0x50000002
            $s_return_msg = "CMD_PLAYMODE"
        Case 1342177283     ;0x50000003
            $s_return_msg = "CMD_NOWPLAYING"
        Case 1342177284     ;0x50000004
            $s_return_msg = "CMD_LISTSUBTITLETRACKS"
        Case 1342177285     ;0x50000005
            $s_return_msg = "CMD_LISTAUDIOTRACKS"
        Case 1342177286     ;0x50000006
            $s_return_msg = "CMD_PLAYLIST"
        Case Else
            $s_return_msg = "CMD_NO_or_UNKNOWN_CMD"
    EndSwitch
    
    Return $s_return_msg
EndFunc
Edited by ZoSTeR

Share this post


Link to post
Share on other sites



excellent ! i use mpc since the first day and i didn't know mpchc.

your script is excellent, hope you can make a child window ?


-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

Share this post


Link to post
Share on other sites

Thanks, ZoSTeR, for this great example. It was very useful. I hope you see this response.

This example and my derivative scripts work fine when compiled as 32bit applications. However, they lock up if compiled as 64bit. I don't profess to be an expert, but a couple of minor changes allowed the 64bit to work.

The problem was in the WM_COPYDATA message data structure. The string length member needs to be a 32bit unsigned integer under 32bit compilation and, correspondingly, 64bit under 64bit. Changing this from dWord to WPARAM in the structure definintion allows for usable compilation as either 32 or 64 bit.

Two lines should be changed in this example. Both lines 226 and 257 currently read as follows:

$StructDef_COPYDATA = "dword dwData;dword cbData;ptr lpData"

Simply change each of these lines to this:

$StructDef_COPYDATA = "dword dwData;WPARAM cbData;ptr lpData"

I hope this is useful to someone.

Share this post


Link to post
Share on other sites

Hi...

Each time i try to run this message appears : could not run Mplayerc.exe (path?).

I dont know how to solve this and would appreciate your help.

Share this post


Link to post
Share on other sites

Did you install it?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Thanks for the reply ,

Yes i installed the program then i pasted the code in the SciTE script editor and saved it as au3 file , when i run this file the message i mentioned eariler appears.

Share this post


Link to post
Share on other sites

Use the full path and file name and see if that works.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Full path and file name of the media player you mean ?

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

yes, the run  command in the script doesn't include the path to the program, just the program name and extension. Which is probably why the message box mentions adding the path in it (cryptically).

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I just tried it and the result is the same , I guess we gotta write our own program in this one , just what  is the  language used ?

could you tell me that if you dont mind.

Share this post


Link to post
Share on other sites

This has a minor modification with what to run depending on the return value of _GetPathOfMPCHC(). The path is read from registry (which exists from running the player at least once). If the registry value is "" then the default is "mpc-hc.exe". The code in the 1st post uses only a default of "mplayerc.exe" which is not valid for the current MPC Home Cinema release.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <SendMessage.au3>
#Include <WinAPI.au3>

; FROM MpcApi.h :
;
;~ // This file define commands used for "Media Player Classic - Homecinema" API. To send commands
;~ // to mpc-hc, and receive playback notifications, first launch process with the /slave command line
;~ // argument follow by an HWnd handle use to receive notification :
;~ //
;~ // ..\bin\mplayerc /slave 125421
;~ //
;~ // After startup, mpc-hc send a WM_COPYDATA message to host with COPYDATASTRUCT struct filled with :
;~ //       - dwData    : CMD_CONNECT
;~ //       - lpData    : Unicode string containing mpc-hc main window Handle
;~ //
;~ // To pilot mpc-hc, send WM_COPYDATA messages to Hwnd provided on connection. All messages should be
;~ // formatted as Unicode strings. For commands or notifications with multiple parameters, values are
;~ // separated by |
;~ // If a string contains a |, it will be escaped with a \ so a \| is not a separator
;~ //
;~ // Ex : When a file is openned, mpc-hc send to host the "now playing" notification :
;~ //       - dwData    : CMD_NOWPLAYING
;~ //       - lpData    : title|author|description|filename|duration

;~ #pragma once


;~ typedef enum MPC_LOADSTATE
;~ {
;~  MLS_CLOSED, 
;~  MLS_LOADING, 
;~  MLS_LOADED, 
;~  MLS_CLOSING
;~ };


;~ typedef enum MPC_PLAYSTATE
;~ {
;~  PS_PLAY   = 0,
;~  PS_PAUSE  = 1,
;~  PS_STOP   = 2,
;~  PS_UNUSED = 3
;~ };



;==== Commands from MPC to host
    
;   // Send after connection
;   // Par 1 : MPC window handle (command should be send to this HWnd)
Global Const $CMD_CONNECT = 0x50000000

;   // Send when opening or closing file
;   // Par 1 : current state (see MPC_LOADSTATE enum)
Global Const $CMD_STATE = 0x50000001

;   // Send when playing, pausing or closing file
;   // Par 1 : current play mode (see MPC_PLAYSTATE enum)
Global Const $CMD_PLAYMODE = 0x50000002

;   // Send after opening a new file
;   // Par 1 : title
;   // Par 2 : author
;   // Par 3 : description
;   // Par 4 : complete filename (path included)
;   // Par 5 : duration in seconds
Global Const $CMD_NOWPLAYING = 0x50000003

;   // List of subtitle tracks
;   // Par 1 : Subtitle track name 0
;   // Par 2 : Subtitle track name 1
;   // ...
;   // Par n : Active subtitle track, -1 if subtitles disabled
;   //
;   // if no subtitle track present, returns -1
;   // if no file loaded, returns -2
Global Const $CMD_LISTSUBTITLETRACKS = 0x50000004

;   // List of audio tracks
;   // Par 1 : Audio track name 0
;   // Par 2 : Audio track name 1
;   // ...
;   // Par n : Active audio track
;   //
;   // if no audio track present, returns -1
;   // if no file loaded, returns -2
Global Const $CMD_LISTAUDIOTRACKS = 0x50000005

;   // List of files in the playlist
;   // Par 1 : file path 0
;   // Par 2 : file path 1
;   // ...
;   // Par n : active file, -1 if no active file
Global Const $CMD_PLAYLIST = 0x50000006


; ====================================================================
; ==== Commands from host to MPC
    
;   // Open new file 
;   // Par 1 : file path
Global Const $CMD_OPENFILE = 2684354560 ; 0xA0000000

;   // Stop playback, but keep file / playlist
Global Const $CMD_STOP = 2684354561 ; 0xA0000001

;   // Stop playback and close file / playlist
Global Const $CMD_CLOSEFILE = 2684354562 ; 0xA0000002

;   // Pause or restart playback
Global Const $CMD_PLAYPAUSE = 0xA0000003

;   // Add a new file to playlist (did not start playing)
;   // Par 1 : file path
Global Const $CMD_ADDTOPLAYLIST = 0xA0001000

;   // Remove all files from playlist
Global Const $CMD_CLEARPLAYLIST =0xA0001001

;   // Start playing playlist
Global Const $CMD_STARTPLAYLIST = 0xA0001002

;   CMD_REMOVEFROMPLAYLIST  = 0xA0001003,   // TODO

;   // Cue current file to specific position
;   // Par 1 : new position in seconds
Global Const $CMD_SETPOSITION = 0xA0002000

;   // Set the audio delay
;   // Par 1 : new audio delay in ms
Global Const $CMD_SETAUDIODELAY = 0xA0002001

;   // Set the subtitle delay
;   // Par 1 : new subtitle delay in ms
Global Const $CMD_SETSUBTITLEDELAY = 0xA0002002

;   // Set the active file in the playlist
;   // Par 1 : index of the active file, -1 for no file selected
;   // DOESN'T WORK
Global Const $CMD_SETINDEXPLAYLIST = 0xA0002003

;   // Set the audio track
;   // Par 1 : index of the audio track
Global Const $CMD_SETAUDIOTRACK = 0xA0002004

;   // Set the subtitle track
;   // Par 1 : index of the subtitle track, -1 for disabling subtitles
Global Const $CMD_SETSUBTITLETRACK = 0xA0002005

;   // Ask for a list of the subtitles tracks of the file
;   // return a CMD_LISTSUBTITLETRACKS
Global Const $CMD_GETSUBTITLETRACKS = 0xA0003000

;   // Ask for a list of the audio tracks of the file
;   // return a CMD_LISTAUDIOTRACKS
Global Const $CMD_GETAUDIOTRACKS = 0xA0003001
    
;   // Ask for the properties of the current loaded file
;   // return a CMD_NOWPLAYING
Global Const $CMD_GETNOWPLAYING = 0xA0003002

;   // Ask for the current playlist
;   // return a CMD_PLAYLIST
Global Const $CMD_GETPLAYLIST = 0xA0003003

;== End MPC Commands



Global $gs_messages
Global $ghnd_MPC_handle

GUIRegisterMsg($WM_COPYDATA, "__Msg_WM_COPYDATA")

; GUI start
$Form1 = GUICreate("MPC-HC remote demo", 608, 408, 180, 120)
$Edit1 = GUICtrlCreateEdit("", 16, 16, 577, 321)
$Button_open = GUICtrlCreateButton("Open File", 24, 368, 140, 28, 0)
$Button_play = GUICtrlCreateButton("Play / Pause", 224, 368, 140, 28, 0)
$Button_stop = GUICtrlCreateButton("Stop", 424, 368, 140, 28, 0)
GUISetState(@SW_SHOW)
; GUI end


; Run MPC-HC with our GUI form handle
$exepath_MPC = _GetPathOfMPCHC()
$s_formHandle = Dec(StringMid(String($Form1),3))
$pid_MPC = Run('"' & $exepath_MPC & '" /slave ' & $s_formHandle)

If $pid_MPC = 0 Then
    MsgBox(0,"Error","Could not run " & $exepath_MPC & " (Path?)")
    Exit
EndIf

;GUI loop
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button_open
            $s_FileToOpen = FileOpenDialog("Open Media File","C:","Media files (*.*)")
            __MPC_send_message($ghnd_MPC_handle, $CMD_OPENFILE, $s_FileToOpen)
        Case $Button_play
            __MPC_send_message($ghnd_MPC_handle, $CMD_PLAYPAUSE, "")
        Case $Button_stop
            __MPC_send_message($ghnd_MPC_handle, $CMD_STOP, "")
    EndSwitch
WEnd


Func __MPC_send_message($prm_hnd_MPC, $prm_MPC_Command,$prm_MPC_parameter)
    Local $StructDef_COPYDATA
    Local $st_COPYDATA
    Local $p_COPYDATA
    Local $st_CDString
    Local $p_CDString
    Local $i_StringSize
    
    $StructDef_COPYDATA = "dword dwData;dword cbData;ptr lpData"; 32 bit command; length of string; pointer to string
    $st_COPYDATA = DllStructCreate($StructDef_COPYDATA) ; create the message struct
    
    $i_StringSize = StringLen($prm_MPC_parameter) + 1 ; +1 for null termination
    $st_CDString = DllStructCreate("wchar var1[" & $i_StringSize & "]") ; the array to hold the unicode string we are sending
    DllStructSetData($st_CDString,1,$prm_MPC_parameter) ; set parameter string
    DllStructSetData($st_CDString,1,0,$i_StringSize) ; null termination
    $p_CDString = DllStructGetPtr($st_CDString) ; the pointer to the string
    
    DllStructSetData($st_COPYDATA, "dwData", $prm_MPC_Command) ; dwData 32 bit command
    DllStructSetData($st_COPYDATA, "cbData", $i_StringSize * 2) ; size of string * 2 for unicode
    DllStructSetData($st_COPYDATA, "lpData", $p_CDString) ; lpData pointer to data
    $p_COPYDATA = DllStructGetPtr($st_COPYDATA) ; pointer to COPYDATA struct
    
    _SendMessage($prm_hnd_MPC,$WM_COPYDATA,0,$p_COPYDATA)
   
    $st_COPYDATA = 0 ; free the struct
    $st_CDString = 0 ; free the struct
EndFunc


Func __Msg_WM_COPYDATA($hWnd, $Msg, $wParam, $lParam)
        ; Receives WM_COPYDATA message from MPC
        ; $LParam = pointer to a COPYDATA struct
        Local $StructDef_COPYDATA
        Local $st_COPYDATA
        Local $StructDef_DataString
        Local $st_DataString
        Local $s_DataString
        Local $s_dwData, $s_cbData, $s_lpData
        
        $StructDef_COPYDATA = "dword dwData;dword cbData;ptr lpData"
        $StructDef_DataString = "char DataString"
        $st_COPYDATA = DllStructCreate($StructDef_COPYDATA, $LParam)
        $s_dwData = DllStructGetData($st_COPYDATA,"dwData") ; 32bit MPC command
        $s_cbData = DllStructGetData($st_COPYDATA,"cbData") ; length of DataString
        $s_lpData = DllStructGetData($st_COPYDATA,"lpData") ; pointer to DataString
        
        $StructDef_DataString = "wchar DataString[" & int($s_cbData) & "]" ;unicode string with length cbData
        
        $st_DataString = DllStructCreate($StructDef_DataString,$s_lpData)
        $s_DataString = DllStructGetData($st_DataString,"DataString")
        
        ; If we receive a 0x50000000 CMD_CONNECT we retrieve the MPC handle
        If $s_dwData = 1342177280 Then
            $ghnd_MPC_handle = $s_DataString
        EndIf
        
        $gs_messages = __MPC_Command_to_Text($s_dwData) & @CRLF & "Data: " & $s_DataString & @CRLF & $gs_messages
        GUICtrlSetData($Edit1, $gs_messages)

EndFunc

Func __MPC_Command_to_Text($prm_MPC_Command)
    ; Converts an MPC command code to plain text; accepts the decimal value of the command
    
    Switch $prm_MPC_Command
        Case 1342177280     ;0x50000000
            $s_return_msg = "CMD_CONNECT"
        Case 1342177281     ;0x50000000
            $s_return_msg = "CMD_STATE"
        Case 1342177282     ;0x50000002
            $s_return_msg = "CMD_PLAYMODE"
        Case 1342177283     ;0x50000003
            $s_return_msg = "CMD_NOWPLAYING"
        Case 1342177284     ;0x50000004
            $s_return_msg = "CMD_LISTSUBTITLETRACKS"
        Case 1342177285     ;0x50000005
            $s_return_msg = "CMD_LISTAUDIOTRACKS"
        Case 1342177286     ;0x50000006
            $s_return_msg = "CMD_PLAYLIST"
        Case Else
            $s_return_msg = "CMD_NO_or_UNKNOWN_CMD"
    EndSwitch
    
    Return $s_return_msg
EndFunc

Func _GetPathOfMPCHC()
    ; path of media player classic home cinema (MPC-HC version 1.7.5)
    Local $path
    $path = RegRead('HKCU\SOFTWARE\MPC-HC\MPC-HC', 'ExePath')
    If $path == '' Then $path = 'mpc-hc.exe'
    Return $path
EndFunc

Language used is AutoIt :ph34r:

Modification done primarily for ease of use with regards to the interesting code posted by ZoSTeR.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0