internal static T[] GetInfoArray <T>(GetInfoDelegate method, IntPtr handle, uint name) where T : struct { var elemSize = Marshal.SizeOf <T>(); IntPtr arraySize; ErrorCode error = method(handle, name, IntPtr.Zero, IntPtr.Zero, out arraySize); if (error != ErrorCode.Success) { throw new OpenClException(error); } int count = (int)arraySize / elemSize; if (count * elemSize < (int)arraySize) { throw new InvalidOperationException($"Array size is incompatible with managed element size: array size = {arraySize}, sizeof({typeof(T)}) = {elemSize})"); } T[] result = new T[count]; GCHandle gch = GCHandle.Alloc(result, GCHandleType.Pinned); try { error = method(handle, name, (IntPtr)(count * elemSize), gch.AddrOfPinnedObject(), out arraySize); if (error != ErrorCode.Success) { throw new OpenClException(error); } } finally { gch.Free(); } return(result); }
internal static string GetInfoString(GetInfoDelegate method, IntPtr handle, uint name) { IntPtr size; ErrorCode error = method(handle, name, IntPtr.Zero, IntPtr.Zero, out size); if (error != ErrorCode.Success) { throw new OpenClException(error); } string result = null; IntPtr buf = Marshal.AllocHGlobal(size); try { error = method(handle, name, size, buf, out size); if (error != ErrorCode.Success) { throw new OpenClException(error); } result = Marshal.PtrToStringAnsi(buf); } finally { Marshal.FreeHGlobal(buf); } return(result); }
/// <summary> /// /// </summary> /// <typeparam name="THandleType"></typeparam> /// <typeparam name="TInfoType"></typeparam> /// <param name="handle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> protected static string GetStringInfo <THandleType, TInfoType> (THandleType handle, TInfoType paramName, GetInfoDelegate <THandleType, TInfoType> getInfoDelegate) { byte[] buffer = GetArrayInfo <THandleType, TInfoType, byte>(handle, paramName, getInfoDelegate); char[] chars = Encoding.ASCII.GetChars(buffer, 0, buffer.Length); return(new string(chars).TrimEnd('\0')); }
/// <summary> /// /// </summary> /// <typeparam name="HandleType"></typeparam> /// <typeparam name="InfoType"></typeparam> /// <param name="handle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> protected bool GetBoolInfo <HandleType, InfoType> (HandleType handle, InfoType paramName, GetInfoDelegate <HandleType, InfoType> getInfoDelegate) { int result = GetInfo <HandleType, InfoType, int>(handle, paramName, getInfoDelegate); return(result == (int)ComputeBoolean.True); }
internal static T[] GetInfoArray <T>(GetInfoDelegate method, IntPtr handle, uint name) where T : struct { IntPtr size; ErrorCode error = method(handle, name, IntPtr.Zero, IntPtr.Zero, out size); if (error != ErrorCode.Success) { throw new OpenClException(error); } int count = (int)size / Marshal.SizeOf <T>(); if (count * Marshal.SizeOf <T>() < (int)size) { count++; } T[] result = new T[count]; GCHandle gch = GCHandle.Alloc(result, GCHandleType.Pinned); try { error = method(handle, name, (IntPtr)(count * Marshal.SizeOf <T>()), gch.AddrOfPinnedObject(), out size); if (error != ErrorCode.Success) { throw new OpenClException(error); } } finally { gch.Free(); } return(result); }
/// <summary> /// /// </summary> /// <typeparam name="HandleType"></typeparam> /// <typeparam name="InfoType"></typeparam> /// <param name="handle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> protected string GetStringInfo <HandleType, InfoType> (HandleType handle, InfoType paramName, GetInfoDelegate <HandleType, InfoType> getInfoDelegate) { byte[] buffer = GetArrayInfo <HandleType, InfoType, byte>(handle, paramName, getInfoDelegate); char[] chars = Encoding.UTF8.GetChars(buffer, 0, buffer.Length); return((new string(chars)).TrimEnd(new char[] { '\0' })); }
private void GetReturn(IAsyncResult asyncResult) { AsyncResult asyncresult = (AsyncResult)asyncResult; GetInfoDelegate del = (GetInfoDelegate)asyncresult.AsyncDelegate; del.EndInvoke(asyncResult); //InText.Alert(result); //ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "<script>alert(" + result + ")</script>", false); //Response.Write(result); }
internal static T GetInfoEnum <T>(GetInfoDelegate method, IntPtr handle, uint name) where T : struct { T val = default(T); ErrorCode error; unsafe { error = method(handle, name, (IntPtr)Marshal.SizeOf(Enum.GetUnderlyingType(typeof(T))), (IntPtr)Unsafe.AsPointer(ref val), out var size); } if (error != ErrorCode.Success) { throw new OpenClException(error); } return(val); }
internal static T GetInfo <T> (GetInfoDelegate method, IntPtr handle, uint name) where T : struct { IntPtr size; object result = default(T); var h = GCHandle.Alloc(result, GCHandleType.Pinned); try { ErrorCode error = method(handle, name, (IntPtr)Marshal.SizeOf <T> (), h.AddrOfPinnedObject(), out size); if (error != ErrorCode.Success) { throw new OpenClException(error); } } finally { h.Free(); } return((T)result); }
/// <summary> /// /// </summary> /// <typeparam name="HandleType"></typeparam> /// <typeparam name="InfoType"></typeparam> /// <typeparam name="QueriedType"></typeparam> /// <param name="handle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> internal QueriedType[] GetArrayInfo <HandleType, InfoType, QueriedType> (HandleType handle, InfoType paramName, GetInfoDelegate <HandleType, InfoType> getInfoDelegate) { QueriedType[] buffer; getInfoDelegate(handle, paramName, IntPtr.Zero, IntPtr.Zero, out IntPtr bufferSizeRet); buffer = new QueriedType[bufferSizeRet.ToInt64() / Marshal.SizeOf(typeof(QueriedType))]; GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { getInfoDelegate(handle, paramName, bufferSizeRet, gcHandle.AddrOfPinnedObject(), out bufferSizeRet); } finally { gcHandle.Free(); } return(buffer); }
internal static T GetInfoEnum <T> (GetInfoDelegate method, IntPtr handle, uint name) where T : struct { var type = Enum.GetUnderlyingType(typeof(T)); var result = Activator.CreateInstance(type); var size = (IntPtr)Marshal.SizeOf(type); var h = GCHandle.Alloc(result, GCHandleType.Pinned); try { ErrorCode error = method(handle, name, size, h.AddrOfPinnedObject(), out size); if (error != ErrorCode.Success) { throw new OpenClException(error); } } finally { h.Free(); } return((T)result); }
/// <summary> /// /// </summary> /// <typeparam name="HandleType"></typeparam> /// <typeparam name="InfoType"></typeparam> /// <typeparam name="QueriedType"></typeparam> /// <param name="handle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> internal QueriedType GetInfo <HandleType, InfoType, QueriedType> (HandleType handle, InfoType paramName, GetInfoDelegate <HandleType, InfoType> getInfoDelegate) where QueriedType : struct { QueriedType result = new QueriedType(); GCHandle gcHandle = GCHandle.Alloc(result, GCHandleType.Pinned); try { getInfoDelegate(handle, paramName, (IntPtr)Marshal.SizeOf(result), gcHandle.AddrOfPinnedObject(), out IntPtr sizeRet); } finally { result = (QueriedType)gcHandle.Target; gcHandle.Free(); } return(result); }
internal static InfoBuffer GetInfo <THandle1Type, THandle2Type, TEnumType>( GetInfoDelegate <THandle1Type, THandle2Type, TEnumType> method, THandle1Type handle1, THandle2Type handle2, TEnumType name, out ErrorCode error) { IntPtr paramSize; error = method(handle1, handle2, name, IntPtr.Zero, InfoBuffer.Empty, out paramSize); // no error checking here because some implementations return InvalidValue even // though the paramSize is correctly returned var buffer = new InfoBuffer(paramSize); error = method(handle1, handle2, name, paramSize, buffer, out paramSize); if (error != ErrorCode.Success) { return(InfoBuffer.Empty); } return(buffer); }
/// <summary> /// /// </summary> /// <typeparam name="THandleType"></typeparam> /// <typeparam name="TInfoType"></typeparam> /// <typeparam name="TQueriedType"></typeparam> /// <param name="handle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> protected static TQueriedType GetInfo <THandleType, TInfoType, TQueriedType> (THandleType handle, TInfoType paramName, GetInfoDelegate <THandleType, TInfoType> getInfoDelegate) where TQueriedType : struct { TQueriedType result = new TQueriedType(); GCHandle gcHandle = GCHandle.Alloc(result, GCHandleType.Pinned); try { ComputeErrorCode error = getInfoDelegate(handle, paramName, (IntPtr)Marshal.SizeOf(result), gcHandle.AddrOfPinnedObject(), out _); ComputeException.ThrowOnError(error); } finally { result = (TQueriedType)gcHandle.Target; gcHandle.Free(); } return(result); }
protected void btnSubmit_Click(object sender, EventArgs e) { try { //查询信息 GetInfoDelegate clDel = new GetInfoDelegate(this.GetInfo); AsyncCallback callBackClose = new AsyncCallback(GetReturn); clDel.BeginInvoke(callBackClose, null);// 异步调用方法,查询信息 } catch (Exception ex) { } finally { InText.AlertAndRedirect("执行成功!", Request.Url.AbsoluteUri); } }
/// <summary> /// /// </summary> /// <typeparam name="HandleType"></typeparam> /// <typeparam name="InfoType"></typeparam> /// <typeparam name="QueriedType"></typeparam> /// <param name="handle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> protected QueriedType GetInfo <HandleType, InfoType, QueriedType> (HandleType handle, InfoType paramName, GetInfoDelegate <HandleType, InfoType> getInfoDelegate) where QueriedType : struct { QueriedType result = new QueriedType(); GCHandle gcHandle = GCHandle.Alloc(result, GCHandleType.Pinned); try { IntPtr sizeRet; ComputeErrorCode error = getInfoDelegate(handle, paramName, (IntPtr)HDSPUtils.SizeOf(result.GetType()), gcHandle.AddrOfPinnedObject(), out sizeRet); ComputeException.ThrowOnError(error); } finally { result = (QueriedType)gcHandle.Target; gcHandle.Free(); } return(result); }
/// <summary> /// /// </summary> /// <typeparam name="THandleType"></typeparam> /// <typeparam name="TInfoType"></typeparam> /// <typeparam name="TQueriedType"></typeparam> /// <param name="handle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> protected static TQueriedType[] GetArrayInfo <THandleType, TInfoType, TQueriedType> (THandleType handle, TInfoType paramName, GetInfoDelegate <THandleType, TInfoType> getInfoDelegate) { var error = getInfoDelegate(handle, paramName, IntPtr.Zero, IntPtr.Zero, out var bufferSizeRet); ComputeException.ThrowOnError(error); var buffer = new TQueriedType[bufferSizeRet.ToInt64() / Marshal.SizeOf(typeof(TQueriedType))]; GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { error = getInfoDelegate(handle, paramName, bufferSizeRet, gcHandle.AddrOfPinnedObject(), out bufferSizeRet); ComputeException.ThrowOnError(error); } finally { gcHandle.Free(); } return(buffer); }
/// <summary> /// Initialise the Yubikey and get its status /// </summary> /// <param name="waitms"></param> public void Init() { lock (_initLock) { if (Info.Status.VersionMajor != 0) { return; } // load library and load info LoadLibrary(); INFO info = new INFO(); GetInfoDelegate f = GetFunction <GetInfoDelegate>("GetInfo"); int ret = f(out info); if (ret > 1) { info.Error = string.Format("Error {0}", ret); } Info = info; } }
internal static InfoBuffer GetInfo <THandle1Type, THandle2Type, TEnumType>( GetInfoDelegate <THandle1Type, THandle2Type, TEnumType> method, THandle1Type handle1, THandle2Type handle2, TEnumType name, out ErrorCode error) { IntPtr paramSize; error = method(handle1, handle2, name, IntPtr.Zero, InfoBuffer.Empty, out paramSize); if (error != ErrorCode.Success) { return(InfoBuffer.Empty); } var buffer = new InfoBuffer(paramSize); error = method(handle1, handle2, name, paramSize, buffer, out paramSize); if (error != ErrorCode.Success) { return(InfoBuffer.Empty); } return(buffer); }
/// <summary> /// /// </summary> /// <typeparam name="HandleType"></typeparam> /// <typeparam name="InfoType"></typeparam> /// <typeparam name="QueriedType"></typeparam> /// <param name="handle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> protected QueriedType[] GetArrayInfo <HandleType, InfoType, QueriedType> (HandleType handle, InfoType paramName, GetInfoDelegate <HandleType, InfoType> getInfoDelegate) { ComputeErrorCode error; QueriedType[] buffer; IntPtr bufferSizeRet; error = getInfoDelegate(handle, paramName, IntPtr.Zero, IntPtr.Zero, out bufferSizeRet); ComputeException.ThrowOnError(error); buffer = new QueriedType[bufferSizeRet.ToInt64() / Marshal.SizeOf(typeof(QueriedType))]; GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { error = getInfoDelegate(handle, paramName, bufferSizeRet, gcHandle.AddrOfPinnedObject(), out bufferSizeRet); ComputeException.ThrowOnError(error); } finally { gcHandle.Free(); } return(buffer); }
internal InfoCollection(SupportsDelegate supports, GetInfoDelegate get, IntPtr handle) { this.supports = supports; this.get = get; this.handle = handle; }