标签: safefilehandle

C#WinUSB无法在接口上调用CloseHandle

我试图用CloseHandle释放USB接口的句柄.我得到的例外是:

System.Runtime.InteropServices.SEHException(0x80004005):外部组件引发了异常.位于Usb.Net.Windows.UsbInterface.Dispose()的Device.Net.APICalls.CloseHandle(SafeFileHandle hObject)位于C:\ GitRepos\Device.Net\src\Usb.Net\Windows\UsbInterface.cs:
位于Usb的第23行C:\ GitRepos\Device.Net\src\Usb.Net\Windows\WindowsUsbDevice.cs中的.Net.Windows.WindowsUsbDevice.Dispose():第131行

我试图在我班级的Dispose方法中这样做.

编辑背景:我试图这样做的原因是我的代码在第二次运行时崩溃了.根据这篇文章,我必须在我使用CreateFile创建的设备的句柄上调用CloseHandle.无论如何,这都是在我的代码中完成的,因为设备的句柄因为是SafeFileHandle而被处理掉了.但是,有人告诉我,我需要在接口的句柄上调用CloseHandle.我不知道这是不是真的.我试图这样做,以排除不调用CloseHandle是错误的原因的可能性.基于其他评论和研究,我现在认为这是一个错误,并且调用WinUsb_Free就足够了.它是否正确?

Hans Passant的回答告诉我要删除对CloseHandle的调用,但正如我在评论中指出的那样,原始代码(在master中)从未调用过CloseHandle.当然,删除电话会有效,但这不是问题.下面的问题是:使用WinUSB API释放USB接口的过程什么?.它只是简单地调用WinUsb_Free?这就是我所拥有的所有信息都让我相信.

这是我提出这个问题之前的原始处置方法.它没有调用CloseHandle.

    public void Dispose()
    {
        if (_IsDisposed) return;
        _IsDisposed = true;

        var isSuccess = WinUsbApiCalls.WinUsb_Free(Handle);
        WindowsDeviceBase.HandleError(isSuccess, "Interface could not be disposed");
    }
Run Code Online (Sandbox Code Playgroud)

来自WindowsUsbInterface(https://github.com/MelbourneDeveloper/Device.Net/blob/9ebc122a2755dda2824c6eda961d092f2f6e83b5/src/Usb.Net/Windows/WindowsUsbDevice.cs#L122):

    public override void Dispose()
    {
        if (_IsDisposing) return;
        _IsDisposing = true;

        try
        {
            foreach (var usbInterface in _UsbInterfaces)
            {
                usbInterface.Dispose();
            }

            _UsbInterfaces.Clear();

            _DeviceHandle?.Dispose();
            _DeviceHandle = null;

            base.Dispose();
        }
        catch (Exception ex)
        {
            Logger.Log("Error disposing of device", ex, nameof(WindowsUsbDevice)); …
Run Code Online (Sandbox Code Playgroud)

c# usb winusb safefilehandle

6
推荐指数
2
解决办法
322
查看次数

FileStream.SafeFileHandle*是否真的*将当前流的位置设置为0?

根据MSDN文档FileStream.SafeFileHandle:

SafeFileHandle属性自动刷新流并将当前流位置设置为0.这允许使用此属性返回的SafeFileHandle移动文件或使用另一个流重置流位置.

但是,我的测试似乎表明流位置没有改变.

请考虑以下代码:

using System;
using System.IO;

namespace Demo
{
    internal static class Program
    {
        public static void Main()
        {
            Directory.CreateDirectory("C:\\TEST");
            var buffer = new byte[1024];

            using (var file = new FileStream("C:\\TEST\\TEST.BIN", FileMode.Create))
            {
                file.Write(buffer, 0, buffer.Length);
                Console.WriteLine(file.Position);  // Prints 1024
                var dummy = file.SafeFileHandle;
                // dummy.Dispose();                // Uncommenting this line will make the next line throw.
                Console.WriteLine(file.Position);  // Still prints 1024!
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果SafeFileHandle确实访问确实将当前流位置重置为0,我预计第二个WriteLine()将打印0.

我有其他测试,我实际使用SafeFileHandleWindows API ReadFile()和WriteFile()方法,即使这样,它似乎不会更改文件指针. …

c# filestream safefilehandle

4
推荐指数
1
解决办法
2125
查看次数

C# 中的 SafeFileHandle 是什么?何时应该使用它?

当我还在学习 System.IO 时,在File Streamclass 的构造函数中,我发现有名为 类型的重载构造函数SafeFileHandle,我试图在互联网和 MSDN 文档上搜索,但我什么也看不懂,我发现甚至 更奇怪的词,比如IntPtr,有人能给我解释一下吗?

public FileStream (Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.IO.FileAccess access, int bufferSize, bool isAsync);
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下吗,或者有什么好的网站可以让我学习吗?

c# safefilehandle

2
推荐指数
1
解决办法
7428
查看次数

标签 统计

c# ×3

safefilehandle ×3

filestream ×1

usb ×1

winusb ×1