отправка сообщений WM_COPYDATA из powershell, проблемы с указателями - PullRequest
0 голосов
/ 20 мая 2018

Я пытаюсь отправить управляющие сообщения из powershell в mpc-hc. API Mpc использует сообщения WM_COPYDATA.Вот что я имею до сих пор, посмотрев здесь , здесь и здесь :

Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class Messages {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, ref IntPtr lParam);
    }
    public struct COPYDATASTRUCT
    {
        public IntPtr dwData;    // Any value the sender chooses.  Perhaps its main window handle?
        public int cbData;       // The count of bytes in the message.
        public IntPtr lpData;    // The address of the message.
    }
"@

$WM_COPYDATA = 0x004A;
$CMD_OSDSHOWMESSAGE = 0xA0005000

$MpcWindow1 = Start-Process -PassThru -FilePath "C:\Program Files (x86)\K-Lite Codec Pack\MPC-HC64\mpc-hc64.exe" -ArgumentList "/new"
$MpcMessage = "hello"

$cds = New-Object COPYDATASTRUCT
$cds.dwData = $CMD_OSDSHOWMESSAGE
$cds.cbData = $MpcMessage.Length
$cds.lpData = $MpcMessage[0]     #without [0] throws an exception

[Messages]::SendMessage($MpcWindow1.MainWindowHandle, $WM_COPYDATA, (Get-Process powershell).MainWindowHandle, [ref]$cds[0])

Выполнение, которое дает мне:

Exception calling "SendMessage" with "4" argument(s): "Cannot convert the "COPYDATASTRUCT" value of type
"COPYDATASTRUCT" to type "System.IntPtr"."
At C:\Users\Petersburg SDA\Videos\dev\wm_copydata.ps1:27 char:1
+ [Messages]::SendMessage($MpcWindow1.MainWindowHandle, $WM_COPYDATA, (Get-Process ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : PSInvalidCastException

Я не уверен, что $ MpcMessage [0] правильный, но использование только переменной дает мне это (в дополнение к вышеупомянутому)

Exception setting "lpData": "Cannot convert the "hello" value of type "System.String" to type "System.IntPtr"."
At C:\Users\Petersburg SDA\Videos\dev\wm_copydata.ps1:25 char:1
+ $cds.lpData = $MpcMessage     #throws an exception
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

Изменение этого значения на [ref]$ MpcMessage получает

Exception setting "lpData": "Cannot convert the "System.Management.Automation.PSReference`1[System.String]" value of
type "System.Management.Automation.PSReference`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]" to type "System.IntPtr"."
At C:\Users\Petersburg SDA\Videos\dev\wm_copydata.ps1:25 char:1
+ $cds.lpData = [ref]$MpcMessage     #throws an exception
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

По общему признанию, я не слишком много работал с PowerShell, но перевод кода оказывается необычайно трудным.

1 Ответ

0 голосов
/ 27 мая 2018

Вам придется использовать класс маршала, чтобы упорядочить структуру до указателя.Пример:

    $StructPointerSize = [System.Runtime.InteropServices.Marshal]::SizeOf($CDS)
    $StructPointer = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($StructPointerSize)
    [System.Runtime.InteropServices.Marshal]::StructureToPtr($CDS,$StructPointer,$true

Это назначит некоторую память, а затем скопирует структуру в этот блок памяти.После этого указатель на вашу структуру будет находиться в переменной $ StructPointer.

Полный пример, который возобновляет приостановленное видео (используя отражение для создания типов):

$Domain = [System.AppDomain]::CurrentDomain
$AssemblyName = [System.Reflection.AssemblyName]::new('Messages')
$Assembly = $Domain.DefineDynamicAssembly($AssemblyName,'Run')
$ModuleBuilder = $Assembly.DefineDynamicModule('Messages')

# Define the struct (source : https://msdn.microsoft.com/en-us/library/windows/desktop/ms649010(v=vs.85).aspx)
$StructAttributes = 'AutoLayout, AnsiClass, Class, Public, SequentialLayout, BeforeFieldInit'
$COPYDATASTRUCT = $ModuleBuilder.DefineType('COPYDATASTRUCT',$StructAttributes)
$COPYDATASTRUCT.DefineField('dwData',[int],'Public')
$COPYDATASTRUCT.DefineField('cbData',[Uint32],'Public')
$COPYDATASTRUCT.DefineField('lpData',[System.IntPtr],'Public')
$COPYDATASTRUCT.CreateType()

# Define the class that will hold the PInvoke method
$MessageClass = $ModuleBuilder.DefineType('Messages','Public')

# Define the PInvoke Method
$SendMessage = $MessageClass.DefinePInvokeMethod(
'SendMessageW',
'User32.dll',
@('Public','Static','PinvokeImpl'),
[System.Reflection.CallingConventions]::Standard,
[System.IntPtr],
@([System.IntPtr],[Uint32],[System.IntPtr],[System.IntPtr]),
[System.Runtime.InteropServices.CallingConvention]::Winapi,
[System.Runtime.InteropServices.CharSet]::Unicode
)

$SendMessage.DefineParameter(1,'In','hWnd')
$SendMessage.DefineParameter(2,'In','Msg')
$SendMessage.DefineParameter(3,'In','wParam')
$SendMessage.DefineParameter(4,'In','lParam')


$SendMessage.SetImplementationFlags($SendMessage.GetMethodImplementationFlags() -bor [System.Reflection.MethodImplAttributes]::PreserveSig)
$MessageClass.CreateType()

$WM_COPYDATA = 0x004A;
$CMD_PLAY = 0xA0000004

$CurrentHandle = [System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle
$Arguments = '/Slave' + " " + $CurrentHandle.ToString()

Start-Process -FilePath 'C:\Program Files\MPC-HC\mpc-hc64.exe' -ArgumentList $Arguments
$CDS = [COPYDATASTRUCT]::new()
$CDS.dwData = $CMD_PLAY
$CDS.lpData = [System.IntPtr]::Zero
$CDS.cbData = 0

$StructPointerSize = [System.Runtime.InteropServices.Marshal]::SizeOf($CDS)
$StructPointer = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($StructPointerSize)
[System.Runtime.InteropServices.Marshal]::StructureToPtr($CDS,$StructPointer,$true)
$p = (Get-Process mpc-hc64).MainWindowHandle
[messages]::SendMessageW($p,$WM_COPYDATA,0,$StructPointer)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...