libusb调用库.DLL

vs c++调用libusb.lib生成.dll文件

libusb驱动自带的库文件.dll中包含很多指针,不方便调用,因此要自己建方便调用的.DLL库。
整个工程和libusb库已经上传到我的资源,可以下载使用!
准备:
libusb驱动(用到libusb.lib和lusb0_usb.h 2个文件)
步骤:
1.打开vs 新建项目vsc++ win32控制台应用程序—-应用程序设置 选择DLL,导出符号—完成
2.将libusb.lib和lusb0_usb.h 2个文件放到项目文件夹中。
这里写图片描述
3.项目-USB_Derivers属性-vc++目录-引用目录选择到添加的2个文件目录。
链接器-输入-附加依赖项-输入libusb.lib
这里写图片描述
这里写图片描述
项目属性设置完成开始写程序:
USB_Drivers.cpp

// USB_Drivers.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "USB_Drivers.h"

#include "lusb0_usb.h"
/*USB PID 和 VID */
#define USB_VID    0x1483    
#define USB_PID   0x5751
struct usb_device *USB_Dev[10];
struct usb_dev_handle *USB_Dev_Handle[10];

/*查找到usb返回1,否则返回0*/
int __stdcall Find_USBDev()
{
    int devnums=0;
    usb_init(); 
    usb_find_busses(); 
    usb_find_devices(); 

    struct usb_bus *bus;
    //遍历总线 
    for (bus =usb_busses; bus; bus = bus->next) 
    {
        struct usb_device *dev;
        for (dev = bus->devices; dev; dev = dev->next) 
        {

            if(dev->descriptor.idVendor==USB_VID&& dev->descriptor.idProduct == USB_PID) 
            {
                USB_Dev[devnums]=dev;
                devnums++; 
            }
        }
    }
    return devnums;
}

/*打开usb返回1,否则返回0*/
int __stdcall Open_USBDev(int DevIndex)
{
    int ret;
    if(USB_Dev[DevIndex]==NULL)
        return 0;
     USB_Dev_Handle[DevIndex]=usb_open( USB_Dev[DevIndex]);
        if(USB_Dev_Handle[DevIndex]==NULL)
            return 0;
        else
        {
            ret = usb_claim_interface(USB_Dev_Handle[DevIndex], 0);
            if(ret<0)
                return 0;
            else 
                return 1;
        }
}
/*返回发送出去的数据个数*/
int __stdcall USB_Bulk_Write( int DevIndex,int ep, char *bytes, int size,int timeout)
{
       int ret=0;
        if(USB_Dev_Handle[DevIndex] == NULL)
           return 0;

        ret = usb_bulk_write(USB_Dev_Handle[DevIndex], ep, bytes, size, timeout);
        return ret;
}
/*返回接收的数据个数*/
int __stdcall USB_Bulk_Read(int DevIndex, int ep, char *bytes, int size,int timeout)
{
       int ret=0;
        if(USB_Dev_Handle[DevIndex] == NULL)
           return 0;
        ret = usb_bulk_read(USB_Dev_Handle[DevIndex], ep, bytes, size, timeout);
    return ret;
}

int __stdcall Close_USBDev(int DevIndex)
{
    if(USB_Dev_Handle[DevIndex]==NULL)
        return 1;
    else  
        return usb_close( USB_Dev_Handle[DevIndex]); 
}

USB_Drivers.h

#ifdef USB_DRIVERS_EXPORTS
#define USB_DRIVERS_API __declspec(dllexport)
#else
#define USB_DRIVERS_API __declspec(dllimport)
#endif

extern "C"_declspec(dllexport) int __stdcall Find_USBDev();
extern "C"_declspec(dllexport) int __stdcall Open_USBDev(int DevIndex);

extern "C"_declspec(dllexport) int __stdcall USB_Bulk_Write(int DevIndex, int ep, char *bytes, int size,int timeout);
extern "C"_declspec(dllexport) int __stdcall USB_Bulk_Read(int DevIndex, int ep, char *bytes, int size,int timeout);
extern "C"_declspec(dllexport) int __stdcall Close_USBDev(int DevIndex);

然后 生成-生成USB_Drivers,在Debug文件夹下就生成 USB_Drivers.dll文件。
.dll文件可在其他工程中调用了!

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值