将代码以查找 USB 设备从 C++ 转换为 C#
Converting code to find USB devices from C++ to C#
我在C++中得到了一些代码,可以找到连接的USB设备并为它们生成一些详细的输出,它使用setupapi.h获取信息。
char * myClass::getUSBData(){
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
static GUID GUID_DEVINTERFACE_USB_DEVICE ={ 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return "";
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
string jsonReturn="";
int count = 0;
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
//actions i removed for the post
}
}
我找到了一些在 c# 中做类似事情的代码并对其进行了适当的修改(我相信),代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
enum DigcfConstants : int
{
DIGCF_DEFAULT = 0x00000001,
DIGCF_PRESENT = 0x00000002,
DIGCF_ALLCLASSES = 0x00000004,
DIGCF_PROFILE = 0x00000008,
DIGCF_DEVICEINTERFACE = 0x00000010
}
[StructLayout(LayoutKind.Sequential)]
struct SP_DEVINFO_DATA
{
public int cbSize;
public GUID ClassGuid;
public int DevInst; // DEVINST handle
public UIntPtr Reserved; // this is type'd as a pointer to a ULong in setupapi.h, however, in C/C++ a long is a DWord
}
[StructLayout(LayoutKind.Sequential, Size = 0x10)]
public struct GUID
{
public Int64 Data1;
public Int16 Data2;
public Int16 Data3;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] Data4;
public GUID(Int64 d1, Int16 d2, Int16 d3, byte[] d4)
{
Data1 = d1;
Data2 = d2;
Data3 = d3;
Data4 = new byte[8];
Array.Copy(d4, Data4, d4.Length);
}
}
class NativeMethods
{
[DllImport("Kernel32.dll")]
public static extern int GetLastError();
[DllImport("Setupapi.dll", SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref GUID guid, string enumerator, IntPtr parentHandle, int flags);
[DllImport("Setupapi.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern unsafe bool SetupDiEnumDeviceInfo(IntPtr handle, int index, ref SP_DEVINFO_DATA spDevInfoData);
}
class Program
{
public static unsafe void Main(string[] args)
{
GUID ddClassGuid;
unchecked
{
// GUID_DEVINTERFACE_DISK
ddClassGuid = new GUID(0xA5DCBF10L, (short)0x6530, 0x11D2, new byte[] { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED });
}
IntPtr devList = NativeMethods.SetupDiGetClassDevs(ref ddClassGuid, null, IntPtr.Zero, (int)DigcfConstants.DIGCF_PRESENT | (int)DigcfConstants.DIGCF_DEVICEINTERFACE);
if (devList.ToInt32() == -1)
{
Console.WriteLine("INVALID_HANDLE_VALUE");
return;
}
else
{
Console.WriteLine("FOUND DEVICE");
int index = 0;
SP_DEVINFO_DATA sdd = new SP_DEVINFO_DATA();
Console.WriteLine("cbSize = {0}", sdd.cbSize);
bool test = NativeMethods.SetupDiEnumDeviceInfo(devList, index, ref sdd);
while (NativeMethods.SetupDiEnumDeviceInfo(devList, index, ref sdd))
{
Console.WriteLine("cbSize = {0}", sdd.cbSize);
index++;
}
//NativeMethods.SetupDiDestroyDeviceInfoList(devList);
Console.ReadLine();
}
}
}
}
找到设备后,它会进入 else 语句,但NativeMethods.SetupDiEnumDeviceInfo
总是返回 false。 我认为这意味着这是我发送的参数有问题,但无法弄清楚。
有关如何返回连接的设备的任何帮助将不胜感激。
在这里,这个项目应该可以帮助你
http://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-Drives
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 是否可以从int转换为enum类类型
- 了解 GLM- openGL 中的相机转换
- 将无符号char*转换为std::istream*C++