The ReadReport method signature:
public delegate void ReadReportCallback(HidReport report);
Isn't a fit for New-ScriptBlockCallback. It works with methods taking an AsyncCallback parameter. IF you know the callback is called on the creating thread during the ReadReport method call you can use:
$device.ReadReport({param($hidReport) $hidReport.ReadStatus })
If it is called back on a different thread, try this modified version of the New-ScriptBlockCallback function:
function New-ScriptBlockCallback {
param(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[scriptblock]$Callback
)
if (-not ("CallbackEventBridge" -as [type])) {
Add-Type @"
using System;
using HidLibrary;
public sealed class CallbackEventBridge
{
public event HidDevice.ReadReportCallback CallbackComplete = delegate { };
private CallbackEventBridge() {}
private void CallbackInternal(HidReport report)
{
CallbackComplete(report);
}
public HidDevice.ReadReportCallback Callback
{
get { return new HidDevice.ReadReportCallback(CallbackInternal); }
}
public static CallbackEventBridge Create()
{
return new CallbackEventBridge();
}
}
"@
}
$bridge = [callbackeventbridge]::create()
Register-ObjectEvent -input $bridge -EventName callbackcomplete -action $callback -messagedata $args > $null
$bridge.callback
}