SUMMA USB Plotter write operation using Kernel32.WriteFile is failing

34 views Asked by At

I am attempting to create handle for SUMMA USB plotter, using its device id ("USB\VID_099F&PID_0100\5&10D09A20&0&1") and writing it using WriteFile, Below is the extracted simplified code for the same.

I get valid handle and error occurs during WriteFile operation as Invalid handle. Any input/ suggestion would be appreciated

Below code is using Vanara library.

var deviceId = "USB\\VID_099F&PID_0100\\5&10D09A20&0&1";//SUMMA plotter deviceID
var flags = SetupAPI.DIGCF.DIGCF_ALLCLASSES | SetupAPI.DIGCF.DIGCF_DEVICEINTERFACE | SetupAPI.DIGCF.DIGCF_PRESENT;

using var devInfoList = SetupAPI.SetupDiGetClassDevs(Guid.Empty, null, IntPtr.Zero, flags);

var devices = SetupAPI.SetupDiEnumDeviceInfo(devInfoList);

foreach (var device in devices)
{
    var interfaces = SetupAPI.SetupDiEnumDeviceInterfaces(devInfoList, SetupAPI.GUID_DEVINTERFACE_USB_DEVICE, device);

    foreach (var item in interfaces)
    {
        var getResult = SetupAPI.SetupDiGetDeviceInterfaceDetail(devInfoList, item, out string path, out var _);
        if (getResult)
        {
//path will be in this format :\\?\usb#vid_099f&pid_0101#5 &10d09a20&0&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
            if (deviceId.Equals(path.ExtractDeviceInstanceId())) /
            {
//Creating Handle
                    var fileHandle = Kernel32.CreateFile(
                            path,
                            Kernel32.FileAccess.GENERIC_WRITE | Kernel32.FileAccess.GENERIC_READ,
                            FileShare.Write | FileShare.Read,
                            null,
                            (FileMode)OPEN_EXISTING,
                            0);

                     using var signal = Kernel32.CreateEvent(null, true, false, null);

                     var overlap = new NativeOverlapped
                     {
                         EventHandle = (IntPtr)signal,
                     };
                    //Writing to this device
                     var writeResult = Kernel32.WriteFile(fileHandle, data, data.Length, out var written, ref overlap);
             }
        }
    }
}

I have tried using USBPrint guid and using other libraries such as LibUdbDotnet but did not result in successful results.

0

There are 0 answers