protected Int64 SendSausageCommands(ushort[] commandSequence) { if (outputReportSize != 2 || hidUsagePage != 1 || hidUsage != 6) // hid page 1, usage 6 is keyboard { return(1302); } FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttributes = new FileIOApiDeclarations.SECURITY_ATTRIBUTES(); securityAttributes.lpSecurityDescriptor = IntPtr.Zero; securityAttributes.bInheritHandle = System.Convert.ToInt32(true); //patti keep int32 here securityAttributes.nLength = Marshal.SizeOf(securityAttributes); IntPtr hF = FileIOApiDeclarations.CreateFile(path, FileIOApiDeclarations.GENERIC_WRITE, FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE, IntPtr.Zero, FileIOApiDeclarations.OPEN_EXISTING, 0, 0); SafeFileHandle hFile = new SafeFileHandle(hF, true); if (hFile.IsInvalid) { return(1301);; } FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED(); overlapped.hEvent = IntPtr.Zero; overlapped.Offset = 0; // IntPtr.Zero; overlapped.OffsetHigh = 0; // IntPtr.Zero; foreach (ushort command in commandSequence) { uint cmd = ((uint)command) << 16; uint bytesReturned = 0; if (!DeviceManagementApiDeclarations.DeviceIoControl(hFile, (uint)0x000b0008, ref cmd, 4, IntPtr.Zero, 0, ref bytesReturned, ref overlapped)) { int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); return(result); } } hFile.Close(); return(0); }
static public extern bool DeviceIoControl(SafeFileHandle handle, uint dwIoControlCode, ref uint lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer, uint nOutBufferSize, ref uint lpBytesReturned, ref FileIOApiDeclarations.OVERLAPPED lpOverlapped);
protected void ReadThread() { // FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttrUnused = new FileIOApiDeclarations.SECURITY_ATTRIBUTES(); IntPtr overlapEvent = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, ""); FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED(); overlapped.Offset = 0; // IntPtr.Zero; overlapped.OffsetHigh = 0; // IntPtr.Zero; overlapped.hEvent = (IntPtr)overlapEvent; overlapped.Internal = IntPtr.Zero; overlapped.InternalHigh = IntPtr.Zero; if (inputReportSize == 0) { errCodeR = 302; errCodeRE = 302; return; } errCodeR = 0; errCodeRE = 0; byte[] buffer = new byte[inputReportSize]; GCHandle gch = GCHandle.Alloc(buffer, GCHandleType.Pinned); //onur March 2009 - pinning is reuired while (readThreadActive) { int dataRead = 0; //FileIOApiDeclarations. if (readFileHandle.IsInvalid) { errCodeRE = errCodeR = 320; goto EXit; } if (0 == FileIOApiDeclarations.ReadFile(readFileHandle, gch.AddrOfPinnedObject(), inputReportSize, ref dataRead, ref overlapped)) //ref readFileBuffer[0] { int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (result != FileIOApiDeclarations.ERROR_IO_PENDING) //|| result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED) { if (readFileHandle.IsInvalid) { errCodeRE = errCodeR = 321; goto EXit; } errCodeR = result; errCodeRE = 308; goto EXit; } else //if (result != .ERROR_IO_PENDING) { // gch.Free(); //onur while (readThreadActive) { result = FileIOApiDeclarations.WaitForSingleObject(overlapEvent, 50); if (FileIOApiDeclarations.WAIT_OBJECT_0 == result) { if (0 == FileIOApiDeclarations.GetOverlappedResult(readFileHandle, ref overlapped, ref dataRead, 0)) { result = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (result == FileIOApiDeclarations.ERROR_INVALID_HANDLE || result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED) { errCodeR = 309; errCodeRE = 309; goto EXit; } } // buffer[0] = 89; goto ReadCompleted; } } //while } //if (result != .ERROR_IO_PENDING)...else continue; } //buffer[0] = 90; ReadCompleted: if (dataRead != inputReportSize) { errCodeR = 310; errCodeRE = 310; goto EXit; } if (suppressDuplicateReports) { int r = readRing.putIfDiff(buffer); if (r == 0) { FileIOApiDeclarations.SetEvent(readEvent); } } else { readRing.put(buffer); FileIOApiDeclarations.SetEvent(readEvent); } } //while EXit: FileIOApiDeclarations.CancelIo(readFileHandle); readFileHandle = null; gch.Free(); //onur return; }
//------------------------------------------------------------------------------------------- /// <summary> /// Write Thread /// </summary> protected void WriteThread() { // FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttrUnused = new FileIOApiDeclarations.SECURITY_ATTRIBUTES(); IntPtr overlapEvent = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, ""); FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED(); overlapped.Offset = 0; // IntPtr.Zero; overlapped.OffsetHigh = 0; // IntPtr.Zero; overlapped.hEvent = overlapEvent; overlapped.Internal = IntPtr.Zero; overlapped.InternalHigh = IntPtr.Zero; if (outputReportSize == 0) { return; } byte[] buffer = new byte[outputReportSize]; GCHandle wgch = GCHandle.Alloc(buffer, GCHandleType.Pinned); //onur March 2009 - pinning is reuired int byteCount = 0;; // int loopCount = 0; errCodeW = 0; errCodeWE = 0; while (writeThreadActive) { if (writeRing == null) { errCodeW = 407; errCodeWE = 407; goto Error; } while (writeRing.get((byte[])buffer) == 0) { if (0 == FileIOApiDeclarations.WriteFile( writeFileHandle, wgch.AddrOfPinnedObject(), outputReportSize, ref byteCount, ref overlapped)) { int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (result != FileIOApiDeclarations.ERROR_IO_PENDING) //if ((result == FileIOApiDeclarations.ERROR_INVALID_HANDLE) || // (result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED)) { if (result == 87) { errCodeW = 412; errCodeWE = 412; } else { errCodeW = result; errCodeWE = 408; } goto Error; } //if (result == else { // loopCount = 0; // while (writeThreadActive) // { result = FileIOApiDeclarations.WaitForSingleObject(overlapEvent, 1000); if (result == FileIOApiDeclarations.WAIT_OBJECT_0) { // errCodeWE=1400+loopCount; goto WriteCompleted; } // loopCount++; // if (loopCount > 10) // { errCodeW = 411; errCodeWE = 411; goto Error; // } /* if (0 == FileIOApiDeclarations.GetOverlappedResult(readFileHandle, ref overlapped, ref byteCount, 0)) * { * result = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); * // if (result == ERROR_INVALID_HANDLE || result == ERROR_DEVICE_NOT_CONNECTED) * if (result != FileIOApiDeclarations.ERROR_IO_INCOMPLETE) * { * errCodeW = 409; * errCodeWE = 409; * * goto Error; * }//if(result == ERROR_INVALID_HANDLE * }//if(!GetOverlappedResult*/ // }//while // goto Error; } //else if(result==ERROR_IO_PENDING){ // continue; } else { if ((long)byteCount != outputReportSize) { errCodeW = 410; errCodeWE = 410; } //iface->errCodeWE=1399; } WriteCompleted :; } //while(get== FileIOApiDeclarations.WaitForSingleObject(writeEvent, 100); FileIOApiDeclarations.ResetEvent(writeEvent); // System.Threading.Thread.Sleep(100); } Error: wgch.Free(); //onur return; }