试图让驾驶员处理GPS卡

时间:2012-11-28 00:43:44

标签: c# pinvoke device-driver createfile

当我调用SetupDiEnumDeviceInterfaces时,我得到成功= false,最后一个错误是ERROR_INVALID_USER_BUFFER 1784.不确定代码是否可以解决。我正在尝试从c和C ++中的供应商处移植代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Windows;
using System.Management;
using Microsoft.Win32.SafeHandles;
using System.IO;

namespace GPSTester.src.IPParallelHQT
{
class HaveQuick
{

    [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SetupDiGetClassDevs(
        ref Guid ClassGuid,
        IntPtr Enumerator,
        IntPtr hwndParent,
        uint Flags
        );

    [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern Boolean SetupDiEnumDeviceInterfaces(
       IntPtr hDevInfo,
       ref DeviceInfoData devInfo,
        //           IntPtr devInfo,
       ref Guid interfaceClassGuid,
       UInt32 memberIndex,
       ref DeviceInterfaceData deviceInterfaceData
    );

    [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern Boolean SetupDiGetDeviceInterfaceDetail(
       IntPtr hDevInfo,
       ref DeviceInterfaceData deviceInterfaceData,
       ref DeviceInterfaceDetailData deviceInterfaceDetailData,
       UInt32 deviceInterfaceDetailDataSize,
       out UInt32 requiredSize,
       ref DeviceInfoData deviceInfoData
    );

    [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern bool SetupDiBuildClassInfoList(
            UInt32 Flags,
            [In, Out] Guid[] ClassGuidList,
            UInt32 ClassGuidListSize,
            out UInt32 RequiredSize);

    [DllImport("setupapi.dll", SetLastError = true)]
    static extern bool SetupDiEnumDeviceInfo(
        IntPtr deviceInfoSet, 
        uint memberIndex, 
        ref DeviceInfoData deviceInfoData);

    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern IntPtr CreateFile(
        string fileName,
        [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
        [MarshalAs(UnmanagedType.U4)] FileShare fileShare,
        int securityAttributes,
        [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
        int flags,
        IntPtr template);

    [StructLayout(LayoutKind.Sequential)]
    public struct DeviceInterfaceData
    {
        public uint cbSize;
        public Guid interfaceClassGuid;
        public uint flags;
        public IntPtr reserved;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct DeviceInfoData
    {
        public uint cbSize;
        public Guid ClassGuid;
        public uint DevInst;
        public IntPtr Reserved;
    }

    // Device interface detail data
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct DeviceInterfaceDetailData
    {
        public UInt32 cbSize;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string DevicePath;
    }

    private const int ERROR_NO_MORE_ITEMS = 259;

    public enum DeviceFlags 
    {
        Default = 0x00000001, // only valid with DeviceInterface
        Present = 0x00000002,
        AllClasses = 0x00000004,
        Profile = 0x00000008,
        DeviceInterface = 0x00000010,
    }

    public const short INVALID_HANDLE_VALUE = -1;

    int lastError;

    public HaveQuick()
    {
        bool success = true;

        Guid GUID_DEVINTERFACE_IPHQT = new Guid(0x13FF045F, 0xe9D2, 0x479C, 0xB3,   0x90, 0x8C, 0xAE, 0xC7, 0xD1, 0x1C, 0xC4);
        IntPtr hDeviceInfo = SetupDiGetClassDevs(ref GUID_DEVINTERFACE_IPHQT, IntPtr.Zero, IntPtr.Zero, (uint)(DeviceFlags.Present | DeviceFlags.DeviceInterface));
        if (hDeviceInfo == IntPtr.Zero)
        {
            lastError = Marshal.GetLastWin32Error();
            Console.WriteLine("**Error: couldn't get class info error code = {0}\n");
            return;
        }


        uint devNum = 0x0000;    //Enter board number(starting from zero)
        // Find the interface for device devNum
        DeviceInterfaceData interfaceData = new DeviceInterfaceData();                  // Interface data for this device
        interfaceData.cbSize = (uint)Marshal.SizeOf(interfaceData);
        success = SetupDiEnumDeviceInterfaces(hDeviceInfo, ref deviceInfoData, ref GUID_DEVINTERFACE_IPHQT, devNum, ref interfaceData);
        if (!success)
        {
            lastError = Marshal.GetLastWin32Error();
            Console.WriteLine("**Error: couldn't enum device error number = {0}\n", lastError);
        }

        // Found our device, get the details data to obtain the symbolic link name

        UInt32 requiredSize;                 // Size of buffer reguired to get the symbolic link name
        DeviceInterfaceDetailData deviceDetailData = new DeviceInterfaceDetailData();
        uint deviceInterfaceDetailDataSize = (uint)Marshal.SizeOf(deviceDetailData);
        success = SetupDiGetDeviceInterfaceDetail(hDeviceInfo, ref interfaceData, ref deviceDetailData, deviceInterfaceDetailDataSize, out requiredSize, ref deviceInfoData);
        if (!success)
        {
            lastError = Marshal.GetLastWin32Error();
            Console.WriteLine("**Error: couldn't get interface detail error number = {0}\n", lastError);
        }

        // Get the detail info
        success = SetupDiGetDeviceInterfaceDetail(hDeviceInfo, ref interfaceData, ref deviceDetailData, deviceInterfaceDetailDataSize, out requiredSize, ref deviceInfoData);
        if (!success)
        {
            lastError = Marshal.GetLastWin32Error();
            Console.WriteLine("**Error: couldn't get interface detail(2) error number = {0}\n", lastError);
        }
        string deviceName = deviceDetailData.DevicePath;
        IntPtr  hIpHqt = CreateFile(deviceName, 
                                FileAccess.Read | FileAccess.Write,
                                FileShare.Read | FileShare.Write,
                                0, FileMode.Open, 0, IntPtr.Zero);

        if (hIpHqt == IntPtr.Zero)
        {
            lastError = Marshal.GetLastWin32Error();
            Console.WriteLine("**Error: couldn't open %s, (%d)\n", deviceName, lastError);
        }
    }

}

}

0 个答案:

没有答案