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

📄 usbdll.cpp

📁 USB接口的测试程序,主要是测试USB所提供的DLL的接口函数是否正常!
💻 CPP
字号:
//#include "dbt.h"
#include "usbdll.h"


//----------------------------------------------------------------------------------------------

int _stdcall GetDeviceName(GUID* pGuid, char * Name);

HANDLE	InfoOutPipe,InfoInPipe,MainInPipe;
char	Buf[16];

//----------------------------------------------------------------------------------------------

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
//	ParentInstance = NULL;

//	InfoOutPipe = NULL;
//	InfoInPipe = NULL;
//	MainInPipe = NULL;

    return TRUE;
}

//----------------------------------------------------------------------------------------------
//  Initiate USB Pipe Handles
int _stdcall USBDLLInit(HANDLE instance)
{
	int		Error, NameLen;
	char	Name[256];
	GUID	MyGuid;

	MyGuid.Data1 = 0x873fdf;
	MyGuid.Data2 = 0x61A8;
	MyGuid.Data3 = 0x11D1;
	MyGuid.Data4[0] = 0xAA;
	MyGuid.Data4[1] = 0x5E;
	MyGuid.Data4[2] = 0x00;
	MyGuid.Data4[3] = 0xC0;
	MyGuid.Data4[4] = 0x4F;
	MyGuid.Data4[5] = 0xB1;
	MyGuid.Data4[6] = 0x72;
	MyGuid.Data4[7] = 0x8B;

	if (ParentInstance)
	{
		LastError = USBERR_INVALIDUSER;
		return USBERR_INVALIDUSER;
	}

	Error = GetDeviceName(&MyGuid, Name);
	if (Error)
	{
		return Error;
	}

	strcat(Name,"\\Pipe00");
	NameLen = strlen(Name) - 1;

	if (InfoOutPipe = CreateFile(Name,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0))
	{
		Name[NameLen] = '1';
		if(InfoInPipe = CreateFile(Name,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,0))
		{
			Name[NameLen] = '3';
			if(MainInPipe = CreateFile(Name,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,0))
			{
			}else
			{
				CloseHandle(InfoOutPipe);
				CloseHandle(InfoInPipe);
				return USBERR_CANNOTOPENPIPE;
			}
		}else
		{
			CloseHandle(InfoOutPipe);
			return USBERR_CANNOTOPENPIPE;
		}
	}else
	{
		return USBERR_CANNOTOPENPIPE;
	}

	ParentInstance = instance;

	return USBERR_SUCCESS;
}

//----------------------------------------------------------------------------------------------
//  Close USB Pipe Handles
int _stdcall USBDLLDone()
{
	int		Error;
	char	Name[256];
	GUID	MyGuid;

	MyGuid.Data1 = 0x873fdf;
	MyGuid.Data2 = 0x61A8;
	MyGuid.Data3 = 0x11D1;
	MyGuid.Data4[0] = 0xAA;
	MyGuid.Data4[1] = 0x5E;
	MyGuid.Data4[2] = 0x00;
	MyGuid.Data4[3] = 0xC0;
	MyGuid.Data4[4] = 0x4F;
	MyGuid.Data4[5] = 0xB1;
	MyGuid.Data4[6] = 0x72;
	MyGuid.Data4[7] = 0x8B;

	ParentInstance = NULL;

	Error = GetDeviceName(&MyGuid, Name);

	if (Error)
	{
		return 1;
	}

	if (InfoOutPipe)
	{
		CloseHandle(InfoOutPipe);
	}
	if (InfoInPipe)
	{
		CloseHandle(InfoInPipe);
	}
	if (MainInPipe)
	{
		CloseHandle(MainInPipe);
	}

	return USBERR_SUCCESS;
}

//----------------------------------------------------------------------------------------------
//  Get USB Device Name
int _stdcall GetDeviceName(GUID* pGuid, char * Name)
{
	HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
	if(info==INVALID_HANDLE_VALUE)
	{
		return USBERR_INVALIDGUID;
	}

	SP_INTERFACE_DEVICE_DATA ifdata;
	ifdata.cbSize = sizeof(ifdata);
	if(!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, 0 , &ifdata))
	{
		SetupDiDestroyDeviceInfoList(info);
		return USBERR_INVALIDINTERFACEDATA;
	}

	DWORD ReqLen;
	SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
	PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]);
	if( ifDetail==NULL)
	{
		SetupDiDestroyDeviceInfoList(info);
		return USBERR_NOMEMORYFORDEVICEDATA;
	}

	ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
	if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL))
	{
		SetupDiDestroyDeviceInfoList(info);
		delete ifDetail;
		return USBERR_CANNOTGETDEVICEDATA;
	}

	DWORD	i;
	for (i=0;i<ReqLen;i++)
	{
		Name[i] = ifDetail->DevicePath[i];
	}
	Name[ReqLen] = 0;

	delete ifDetail;
	SetupDiDestroyDeviceInfoList(info);
	return USBERR_SUCCESS;
}

//----------------------------------------------------------------------------------------------
//  Read a byte from Status BUS
//		USBBus Command 1
int	_stdcall USB_GetStatus(HANDLE instance,char *Data)
{
	DWORD	Num;

	if (instance != ParentInstance)
	{
		return USBERR_INVALIDUSER;
	}

	Buf[0] = 1;
	WriteFile(InfoOutPipe,Buf,1,&Num,NULL);
	if (Num != 1)
	{
		return USBERR_CANNOTWRITEINFOPIPE;
	}

	ReadFile(InfoInPipe,Buf,1,&Num,NULL);
	if (Num==1)
	{
		*Data = Buf[0];
		return USBERR_SUCCESS;
	}else
	{
		return USBERR_CANNOTREADINFOPIPE;
	}
}

//----------------------------------------------------------------------------------------------
//  Read a byte from Data BUS with a given address
//		USBBus Command 2
int	_stdcall USB_Inport(HANDLE instance, char Addr, char *Data)
{
	DWORD	Num;

	if (instance != ParentInstance)
	{
		return USBERR_INVALIDUSER;
	}

	Buf[0] = 2;Buf[1] = Addr;
	WriteFile(InfoOutPipe,Buf,2,&Num,NULL);
	if (Num != 2)
	{
		return USBERR_CANNOTWRITEINFOPIPE;
	}

	ReadFile(InfoInPipe,Buf,1,&Num,NULL);
	if (Num==1)
	{
		*Data = Buf[0];
		return USBERR_SUCCESS;
	}else
	{
		return USBERR_CANNOTREADINFOPIPE;
	}
}

//----------------------------------------------------------------------------------------------
//  Write a byte to Data BUS with a given address
//		USBBus Command 3
int	_stdcall USB_Outport(HANDLE instance, char Addr, char Data)
{
	char	Buf[2];
	DWORD	Num;

	if (instance != ParentInstance)
	{
		return USBERR_INVALIDUSER;
	}

	Buf[0] = 3;Buf[1] = Addr;Buf[2] = Data;
	WriteFile(InfoOutPipe,Buf,3,&Num,NULL);

	if (Num==3)
	{
		return USBERR_SUCCESS;
	}else
	{
		return USBERR_CANNOTWRITEINFOPIPE;
	}
}

//----------------------------------------------------------------------------------------------
//  Read Bulk Data from Data Bus through MainInPipe.
//		USBBus Command 4
int _stdcall USB_BulkInport(HANDLE instance, char Addr, WORD Len, void * Data, int *ReadLen)
{
	DWORD	Num,Total;
	PUCHAR	MyLen = NULL;

	if (instance != ParentInstance)
	{
		return USBERR_INVALIDUSER;
	}

	Buf[0] = 4;Buf[1] = Addr;
	//Buf[2] = 16; Buf[3] = 0;
	MyLen = (PUCHAR)&Len;
	Buf[2] = *MyLen; MyLen++;Buf[3] = *MyLen;
//	MyLen = (WORD *)&Buf[2];
//	*MyLen = Len;
	WriteFile(InfoOutPipe,Buf,4,&Num,NULL);

	if (Num != 4)
	{
		return USBERR_CANNOTWRITEINFOPIPE;
	}

	Total = Len*64;
	ReadFile(MainInPipe,Data,Total,&Num,NULL);
	if (Num==Total)
	{
		*ReadLen =  Num;
		return USBERR_SUCCESS;
	}else
	{
		*ReadLen = (WORD) Num;
		return USBERR_CANNOTREADMAINPIPE;
	}
}

//----------------------------------------------------------------------------------------------
//  Write a byte to Control BUS
//		USBBus Command 5
int _stdcall USB_SetControl(HANDLE instance,char Data)
{
	DWORD	Num;

	if (instance != ParentInstance)
	{
		return USBERR_INVALIDUSER;
	}

	Buf[0] = 5;Buf[1] = Data;
	WriteFile(InfoOutPipe,Buf,2,&Num,NULL);

	if (Num==2)
	{
		return USBERR_SUCCESS;
	}else
	{
		return USBERR_CANNOTWRITEINFOPIPE;
	}
}

//----------------------------------------------------------------------------------------------
//  Set Parameters for Pulse Signal Out.
//		USBBus Command 6
int _stdcall USB_SetPulse(HANDLE instance,char *Data)
{
	DWORD	Num;

	if (instance != ParentInstance)
	{
		return USBERR_INVALIDUSER;
	}

	Buf[0] = 6;
	for (Num = 1; Num<9;Num++)
	{
		Buf[Num] = Data[Num-1];
	}
	WriteFile(InfoOutPipe,Buf,9,&Num,NULL);

	if (Num==9)
	{
		return USBERR_SUCCESS;
	}else
	{
		return USBERR_CANNOTWRITEINFOPIPE;
	}
}

//----------------------------------------------------------------------------------------------
//  Enable Pulse Signal Out.
//		USBBus Command 7
int _stdcall USB_EnablePulse(HANDLE instance)
{
	DWORD	Num;

	if (instance != ParentInstance)
	{
		return USBERR_INVALIDUSER;
	}

	Buf[0] = 7;
	WriteFile(InfoOutPipe,Buf,1,&Num,NULL);

	if (Num==1)
	{
		return USBERR_SUCCESS;
	}else
	{
		return USBERR_CANNOTWRITEINFOPIPE;
	}
}

//----------------------------------------------------------------------------------------------
//  Disable Pulse Signal Out.
//		USBBus Command 8
int _stdcall USB_DisablePulse(HANDLE instance)
{
	DWORD	Num;

	if (instance != ParentInstance)
	{
		return USBERR_INVALIDUSER;
	}

	Buf[0] = 8;
	WriteFile(InfoOutPipe,Buf,1,&Num,NULL);

	if (Num==1)
	{
		return USBERR_SUCCESS;
	}else
	{
		return USBERR_CANNOTWRITEINFOPIPE;
	}
}

//----------------------------------------------------------------------------------------------
//  Write a byte to Control BUS
int	USB_GetHandle(HANDLE *Hand)
{
	if (InfoOutPipe != 0)
	{
		*Hand =  ParentInstance;
		return 1;
	}else
	{
		return 0 ;
	}
}

⌨️ 快捷键说明

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