首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

控制usb的有关问题

2012-04-12 
控制usb的问题我想使用CreateFile和WriteFile来控制usb口的针式打印机,用SetupDi系列方法来找到打印机端口

控制usb的问题
我想使用CreateFile和WriteFile来控制usb口的针式打印机,用SetupDi系列方法来找到打印机端口,但却只能找到USB键盘及鼠标,U盘或者打印机都找不到,请问这是什么原因?我的代码如下,是C#的代码

C# code
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Threading;using System.Collections;using System.IO;namespace USB{    class Class1    {        //以下是调用windows的API的函数        //获得GUID        [DllImport("hid.dll")]        public static extern void HidD_GetHidGuid(ref Guid HidGuid);        Guid guidHID = Guid.Empty;        //过滤设备,获取需要的设备        [DllImport("setupapi.dll", SetLastError = true)]        public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, uint Enumerator, IntPtr HwndParent, DIGCF Flags);        IntPtr hDevInfo;        //获取设备,true获取到        [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]        public static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);        public struct SP_DEVICE_INTERFACE_DATA        {            public int cbSize;            public Guid interfaceClassGuid;            public int flags;            public int reserved;        }        [StructLayout(LayoutKind.Sequential)]        private struct OVERLAPPED        {            int Internal;            int InternalHigh;            int Offset;            int OffSetHigh;            int hEvent;        }        // 获取接口的详细信息 必须调用两次 第1次返回长度 第2次获取数据        [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]        private static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtrdeviceInterfaceDetailData,            int deviceInterfaceDetailDataSize, ref int requiredSize, SP_DEVINFO_DATA deviceInfoData);        [StructLayout(LayoutKind.Sequential)]        public class SP_DEVINFO_DATA        {            public int cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));            public Guid classGuid = Guid.Empty; // temp            public int devInst = 0; // dumy            public int reserved = 0;        }        [StructLayout(LayoutKind.Sequential, Pack = 2)]        internal struct SP_DEVICE_INTERFACE_DETAIL_DATA        {            internal int cbSize;            internal short devicePath;        }        public enum DIGCF        {            DIGCF_DEFAULT = 0x1,            DIGCF_PRESENT = 0x2,            DIGCF_ALLCLASSES = 0x4,            DIGCF_PROFILE = 0x8,            DIGCF_DEVICEINTERFACE = 0x10        }        //获取设备文件        [DllImport("kernel32.dll", SetLastError = true)]        private static extern int CreateFile(            string lpFileName,                            // file name            uint dwDesiredAccess,                        // access mode            uint dwShareMode,                            // share mode            uint lpSecurityAttributes,                    // SD            uint dwCreationDisposition,                    // how to create            uint dwFlagsAndAttributes,                    // file attributes            uint hTemplateFile                            // handle to template file            );        //读取设备文件        [DllImport("Kernel32.dll", SetLastError = true)]        private static extern bool ReadFile            (                IntPtr hFile,                byte[] lpBuffer,                uint nNumberOfBytesToRead,                ref uint lpNumberOfBytesRead,                IntPtr lpOverlapped            );        [DllImport("kernel32.dll")]        private static extern bool WriteFile            (                int hFile,                 byte[] lpBuffer,                 int nNumberOfBytesToWriter,                 out int lpNumberOfBytesWriten,                 out OVERLAPPED lpOverLapped            );        //释放设备        [DllImport("hid.dll")]        static public extern bool HidD_FreePreparsedData(ref IntPtr PreparsedData);        //关闭访问设备句柄,结束进程的时候把这个加上保险点        [DllImport("kernel32.dll")]        static public extern int CloseHandle(int hObject);        int HidHandle = -1;        public const uint GENERIC_READ = 0x80000000;        public const uint GENERIC_WRITE = 0x40000000;        public const uint FILE_SHARE_READ = 0x00000001;        public const uint FILE_SHARE_WRITE = 0x00000002;        public const int OPEN_EXISTING = 3;        public void UsBMethod(int index)        {            HidD_GetHidGuid(ref guidHID); //此处获得的guidHID就是所有usb的HID吗???            hDevInfo = SetupDiGetClassDevs(ref guidHID, 0, IntPtr.Zero, DIGCF.DIGCF_PRESENT | DIGCF.DIGCF_DEVICEINTERFACE);            int bufferSize = 0;            ArrayList HIDUSBAddress = new ArrayList();            string devicePathName = "";            while (true)            {                //获取设备,true获取到                SP_DEVICE_INTERFACE_DATA DeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();                DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData);                bool result = false;                //for (int i = 0; i < 3; i++)                //{                    result = SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guidHID, (UInt32)index, ref DeviceInterfaceData);                    int b = Marshal.GetLastWin32Error();                //}                //第一次调用出错,但可以返回正确的Size                SP_DEVINFO_DATA strtInterfaceData = new SP_DEVINFO_DATA();                result = SetupDiGetDeviceInterfaceDetail(hDevInfo, ref DeviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, strtInterfaceData);                //第二次调用传递返回值,调用即可成功                IntPtr detailDataBuffer = Marshal.AllocHGlobal(bufferSize);                SP_DEVICE_INTERFACE_DETAIL_DATA detailData = new SP_DEVICE_INTERFACE_DETAIL_DATA();                detailData.cbSize = Marshal.SizeOf(typeof(SP_DEVICE_INTERFACE_DETAIL_DATA));                Marshal.StructureToPtr(detailData, detailDataBuffer, false);                result = SetupDiGetDeviceInterfaceDetail(hDevInfo, ref DeviceInterfaceData, detailDataBuffer, bufferSize, ref bufferSize, strtInterfaceData);                if (result == false)                {                    break;                }                //获取设备路径访                IntPtr pdevicePathName = (IntPtr)((int)detailDataBuffer + 4);                devicePathName = Marshal.PtrToStringAuto(pdevicePathName);                HIDUSBAddress.Add(devicePathName);                index++;            }            //连接设备文件            int aa = CT_CreateFile(devicePathName);        }        //建立和设备的连接        public int CT_CreateFile(string DeviceName)        {            HidHandle = CreateFile(                DeviceName,                GENERIC_READ | GENERIC_WRITE,//读写,或者一起                0,// | FILE_SHARE_WRITE,//共享读写,或者一起                0,                OPEN_EXISTING,                0,                0);            int a = Marshal.GetLastWin32Error();            if (HidHandle == -1)            {                return 0;            }            else            {                return 1;            }        }    }} 



[解决办法]
楼主,为什么C#的代码发到这个论坛来了。。。
[解决办法]
探讨

楼主,为什么C#的代码发到这个论坛来了。。。

热点排行