⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hidapi.cpp

📁 USB开发例子可以在其他语言中用API调用,有一个POwerbuilder例子
💻 CPP
字号:
// HIDApi.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "HIDApi.h"
extern "C"
{
#include "C:\WINDDK\2600\inc\ddk\wdm\wxp\hidsdi.h"
}
#include <setupapi.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even abefore any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

#define DLLEXPORT extern "C" __declspec(dllexport)
/////////////////////////////////////////////////////////////////////////////
// CHIDApiApp

BEGIN_MESSAGE_MAP(CHIDApiApp, CWinApp)
	//{{AFX_MSG_MAP(CHIDApiApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHIDApiApp construction

CHIDApiApp::CHIDApiApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CHIDApiApp object


DLLEXPORT int SetFeature(HANDLE hDevice, unsigned char *pData,short nLen)
{
    if(HidD_SetFeature(hDevice, pData, nLen)) return 1;
    return 0;
}

DLLEXPORT int GetFeature(HANDLE hDevice, unsigned char *pData, short nLen)
{
    if(HidD_GetFeature(hDevice, pData, nLen)) return 1;
    return 0;
}

DLLEXPORT void CloseHIDDevice(HANDLE hDevice)
{
    CloseHandle((HANDLE)hDevice);
}

HANDLE _OpenHIDDevice(unsigned long wVID, unsigned long  wPID,BOOL bSync, BOOL bTest);
DLLEXPORT BOOL HIDDeviceExist(unsigned long  wVID, unsigned long  wPID)
{
    HANDLE hDevice=_OpenHIDDevice(wVID,wPID,FALSE,TRUE);
    if(hDevice) return TRUE;
    return FALSE;
}

DLLEXPORT HANDLE OpenHIDDevice2(unsigned long  wVID, unsigned long  wPID,BOOL bSync)
{
    return _OpenHIDDevice(wVID,wPID,bSync,FALSE);
}

DLLEXPORT HANDLE OpenHIDDevice()
{
    return _OpenHIDDevice(0X1241,0Xe001,TRUE,FALSE);
}

HANDLE _OpenHIDDevice(unsigned long wVID, unsigned long wPID,BOOL bSync, BOOL bTest)
{
    HANDLE hWRDevice=NULL;
    ////////////////////////////////////////////////////////////////////////
    //
    //    Get All HID device(interface)
    //
    ////////////////////////////////////////////////////////////////////////
    BOOL  bOK=FALSE;
    GUID  HidGuid;
    DWORD Instance = 0;
    CString str,strMsg;
    
    HidD_GetHidGuid(&HidGuid);
    
    HDEVINFO info = SetupDiGetClassDevs(&HidGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
    if(info==INVALID_HANDLE_VALUE)
    {
        strMsg.Format("No HDEVINFO available for this GUID\r\n");
        goto exit;
    }
    
    while(1)
    {
        SP_INTERFACE_DEVICE_DATA ifdata;
        ifdata.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
        
        if(!SetupDiEnumDeviceInterfaces(info, NULL, &HidGuid, Instance, &ifdata))
        {
            str.Format("No SP_INTERFACE_DEVICE_DATA available for this GUID instance %d \r\n", Instance);
            strMsg+=str;
            goto exit;
        }
        Instance++;
        
        // Get size of symbolic link name
        DWORD ReqLen,preLen;
        SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
        
        PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]);
        if(ifDetail==NULL)
        {
            delete ifDetail;
            strMsg+="\r\n";
            continue;
        }
        
        preLen=ReqLen;
        // Get symbolic link name
        ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
        if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, preLen, &ReqLen, NULL))
        {
            delete ifDetail;
            strMsg+="\r\n";
            continue;
        }
        
        str.Format("Symbolic link is %s\r\n",ifDetail->DevicePath);
        strMsg+=str;
        
        //

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -