📄 function.cpp
字号:
#include "app_1.h"
#include "stdafx.h"
#include "function.h"
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <winioctl.h>
#include "ezusbsys.h"
#include <windows.h>
//////////////////////////
#define MAX_DRIVER_NAME 64
#define DEVICE_DESCRIPTOR_SIZE 32
#define MAX_ITEMS_IN_LB 256
#define MAX_BUFFER_SIZE 512
#define MAX_FILE_SIZE (1024*32)
//////////////////////////////////////////////
CString SaveFile = "savefile.txt";
CString AllFullPath;
//////////////////////////////////////////////
HANDLE hDevice = NULL;
BOOLEAN bResult = FALSE;
PCHAR pcDriverName = "EZUSB-0";
int nBytes = 0;
/////////////////////////////////////////////
typedef struct __usb_Dev_Descriptor__ {
UCHAR bLength;
UCHAR bDescriptorType;
USHORT bcdUSB;
UCHAR bDeviceClass;
UCHAR bDeviceSubClass;
UCHAR bDeviceProtocol;
UCHAR bMaxPacketSize0;
USHORT idVendor;
USHORT idProduct;
USHORT bcdDevice;
UCHAR iManufacturer;
UCHAR iProduct;
UCHAR iSerialNumber;
UCHAR bNumConfigurations;
} Usb_Device_Descriptor, *pUsb_Device_Descriptor;
typedef struct __usb_Config_Descriptor__ {
UCHAR bLength;
UCHAR bDescriptorType;
USHORT wTotalLength;
UCHAR bNumInterfaces;
UCHAR bConfigurationValue;
UCHAR iConfiguration;
UCHAR bmAttributes;
UCHAR MaxPower;
} Usb_Configuration_Descriptor, *pUsb_Configuration_Descriptor;
typedef struct __usb_String_Descriptor__ { // M added the usb_String_Descriptor
UCHAR bLength;
UCHAR bDescriptorType;
WCHAR bString[1];
} Usb_String_Descriptor, *pUsb_String_Descriptor;
typedef PVOID USBD_PIPE_HANDLE;
typedef PVOID USBD_CONFIGURATION_HANDLE;
typedef PVOID USBD_INTERFACE_HANDLE;
typedef enum _USBD_PIPE_TYPE {
UsbdPipeTypeControl,
UsbdPipeTypeIsochronous,
UsbdPipeTypeBulk,
UsbdPipeTypeInterrupt
} USBD_PIPE_TYPE;
typedef struct _USBD_PIPE_INFORMATION {
//
// OUTPUT
// These fields are filled in by USBD
//
USHORT MaximumPacketSize; // Maximum packet size for this pipe
UCHAR EndpointAddress; // 8 bit USB endpoint address (includes direction)
// taken from endpoint descriptor
UCHAR Interval; // Polling interval in ms if interrupt pipe
USBD_PIPE_TYPE PipeType; // PipeType identifies type of transfer valid for this pipe
USBD_PIPE_HANDLE PipeHandle;
//
// INPUT
// These fields are filled in by the client driver
//
ULONG MaximumTransferSize; // Maximum size for a single request
// in bytes.
ULONG PipeFlags;
} USBD_PIPE_INFORMATION, *PUSBD_PIPE_INFORMATION;
PUSBD_PIPE_INFORMATION pPipe;
typedef struct _USBD_INTERFACE_INFORMATION {
USHORT Length; // Length of this structure, including
// all pipe information structures that
// follow.
//
// INPUT
//
// Interface number and Alternate setting this
// structure is associated with
//
UCHAR InterfaceNumber;
UCHAR AlternateSetting;
//
// OUTPUT
// These fields are filled in by USBD
//
UCHAR Class;
UCHAR SubClass;
UCHAR Protocol;
UCHAR Reserved;
USBD_INTERFACE_HANDLE InterfaceHandle;
ULONG NumberOfPipes;
//
// INPUT/OUPUT
// see PIPE_INFORMATION
// M USBD_PIPE_INFORMATION Pipes[0];
USBD_PIPE_INFORMATION Pipes[1];
} USBD_INTERFACE_INFORMATION, *PUSBD_INTERFACE_INFORMATION;
typedef LONG USBD_STATUS;
typedef struct _USBD_ISO_PACKET_DESCRIPTOR {
ULONG Offset; // INPUT Offset of the packet from the begining of the
// buffer.
ULONG Length; // OUTPUT length of data received (for in).
// OUTPUT 0 for OUT.
USBD_STATUS Status; // status code for this packet.
} USBD_ISO_PACKET_DESCRIPTOR, *PUSBD_ISO_PACKET_DESCRIPTOR;
////////////////////////////////
BOOLEAN
bOpenDriver (HANDLE * phDeviceHandle, PCHAR devname)
{
char completeDeviceName[64] = "";
char pcMsg[64] = "";
strcat (completeDeviceName,
"\\\\.\\"
);
strcat (completeDeviceName,
devname
);
*phDeviceHandle = CreateFile( completeDeviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (*phDeviceHandle == INVALID_HANDLE_VALUE)
{
return (FALSE);
} else
{
return (TRUE);
} /*else*/
}
/*
BOOLEAN
bFileTransfer(CString strFilePath, CString strFileName)
{
CFile OutFile;
int nFileSize;
CHAR *lpBuffer;
lpBuffer = new char[MAX_BUFFER_SIZE];
BULK_TRANSFER_CONTROL bulkControl;
if(!OutFile.Open(strFilePath,CFile::modeRead))
{
AfxMessageBox("打开文件失败!");
return(false);
}
nFileSize= OutFile.GetLength();
OutFile.Read(lpBuffer,nFileSize);
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
AfxMessageBox("Failed to Open Driver!");
hDevice = NULL;
return(FALSE);
}
bulkControl.pipeNum = 0;
if (hDevice != NULL)
{
DWORD ioctl_val = IOCTL_EZUSB_BULK_WRITE;
bResult = DeviceIoControl (hDevice,
ioctl_val,
&bulkControl,
sizeof (BULK_TRANSFER_CONTROL),
lpBuffer,
nFileSize,
(unsigned long *)&nBytes,
NULL);
}
delete []lpBuffer;
OutFile.Close();
CloseHandle(hDevice);
if(bResult==TRUE)
{
return(TRUE);
}
else
{
return(FALSE);
}
}
*/
BOOLEAN
bFileTransfer(CString strFilePath, CString strFileName)
{
CHAR *lpBuffer;
CHAR *fileinfobuffer;
CFile tempfile;
DWORD dwBlockBytes;
__int64 qwFileSize;
CString File_Info = AllFullPath+"fileinfo.txt";
BULK_TRANSFER_CONTROL bulkControl;
bulkControl.pipeNum=0;
fileinfobuffer = new char[100];
// lpBuffer = new char[MAX_BUFFER_SIZE];
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
AfxMessageBox("Failed to Open Driver!");
hDevice = NULL;
return(FALSE);
}
if(tempfile.Open(strFilePath,CFile::modeRead|CFile::typeBinary))
{
qwFileSize=tempfile.GetLength();
}
else
{
AfxMessageBox("Failed to Open File!");
return(FALSE);
}
//////////////////////////////////////////////
/* CFile OutFile;
OutFile.Open("fileinfo.txt",CFile::modeCreate | CFile::modeWrite);
OutFile.Write(strFileName,strFileName.GetLength());
OutFile.Close();)*/
FILE *stream=NULL;
if((stream = fopen( File_Info, "w" ))!= NULL)
{
fputs("NA" , stream );
fputs(strFileName , stream );//写一行
fputs("SI" , stream );
fprintf(stream,"%d",qwFileSize);
fputs("OVER" , stream );
fclose( stream );
}
else
AfxMessageBox("存储信息出错!");
FILE *strea=NULL;
if((strea = fopen( File_Info, "r" ))!= NULL)
{
fscanf(strea,"%s",fileinfobuffer);
fclose( strea );
if (hDevice != NULL)
{
DWORD ioctl_val = IOCTL_EZUSB_BULK_WRITE;
bResult = DeviceIoControl (hDevice,
ioctl_val,
&bulkControl,
sizeof (BULK_TRANSFER_CONTROL),
fileinfobuffer,
strlen(fileinfobuffer),
(unsigned long *)&nBytes,
NULL);
}
if(bResult != TRUE)
{
AfxMessageBox("传输文件信息出错");
return(FALSE);
}
}
else
{
AfxMessageBox("读存储信息出错!");
}
if(qwFileSize < MAX_FILE_SIZE)
dwBlockBytes=(DWORD)qwFileSize;
lpBuffer=new char[dwBlockBytes];
while(qwFileSize>0)
{
tempfile.Read(lpBuffer,dwBlockBytes);
if (hDevice != NULL)
{
DWORD ioctl_val = IOCTL_EZUSB_BULK_WRITE;
bResult = DeviceIoControl (hDevice,
ioctl_val,
&bulkControl,
sizeof (BULK_TRANSFER_CONTROL),
lpBuffer,
strlen(lpBuffer),
(unsigned long *)&nBytes,
NULL);
}
if(bResult != TRUE)
{
AfxMessageBox("传输文件信息出错");
return(FALSE);
}
qwFileSize-=dwBlockBytes;
}
delete []lpBuffer;
tempfile.Close();
return(TRUE);
//////////////////////////////////////////////
}
void GetPipeInfo()
{
UCHAR InterfaceInfo[1024];
PUSBD_INTERFACE_INFORMATION pInterface;
// Open the driver
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
AfxMessageBox("打开设备失败");
}
if (hDevice != NULL)
{// Perform the Get-Descriptor IOCTL
bResult = DeviceIoControl (hDevice,
IOCTL_Ezusb_GET_PIPE_INFO,
NULL,
0,
InterfaceInfo,
sizeof(InterfaceInfo),
(unsigned long *)&nBytes,
NULL);
}// if valid driver handle
if (bResult==TRUE)
{
pInterface = (PUSBD_INTERFACE_INFORMATION) InterfaceInfo;
pPipe = pInterface->Pipes;
}
else
{
AfxMessageBox("获取端点失败");
}
// Close the handle
CloseHandle (hDevice);
}
/*BOOL GetFile(CString Path)
{
CStringList listPaths;
CString strPath;
CString strFolder;
CStringList listFiles;
int iFileCount=0;
int PathLen=Path.GetLength();
CFileFind finder;
BOOL bWorking;
strPath.Format("%s\*.*", Path);
do{
bWorking = finder.FindFile(strPath);
while(bWorking)
{
bWorking = finder.FindNextFile();
//子目录获取
CString strFilePath = finder.GetFilePath();
if(finder.IsDots())
continue;
if(finder.IsDirectory())
{
//文件路径获取这里获取到了
listPaths.AddHead(strFilePath);
continue;
}
else
{
int pos=strFilePath.ReverseFind('\');
SHFILEINFO sfi;
if (::SHGetFileInfo (strFilePath, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(SHFILEINFO),SHGFI_USEFILEATTRIBUTES | SHGFI_DISPLAYNAME | SHGFI_TYPENAME |SHGFI_ICON|SHGFI_SMALLICON ))
{
CString str;
str.Format("%s\%s",strFilePath.Mid(0,pos),sfi.szDisplayName);
listFiles.AddHead(str);
//文件带路径的名字获取这里获取到了str
iFileCount++;
}
}
}
if(listPaths.IsEmpty())
break;
strFolder = listPaths.RemoveHead();
strPath = strFolder + "\*.*";
finder.Close();
}while(TRUE);
return TRUE;
}
*/
void GetFile(CString strDir)
{
CFileFind ff;
CString szDir = strDir;
if(szDir.Right(1) != "\\")
szDir += "\\";
szDir += "*.*";
BOOL res = ff.FindFile(szDir);
while(res)
{
res = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())
{
//如果是一个子目录,用递归继续往深一层找
GetFile(ff.GetFilePath());
}
else if(!ff.IsDirectory() && !ff.IsDots())
{
//显示当前访问的文件
CString str;
str.Format("%s",ff.GetFilePath());
SaveFileName(str);
// Sleep(500);
}
}
ff.Close();//关闭
}
void SaveFileName(CString str)
{
/* CFile OutFile;
int i;
OutFile.Open("savefile.txt",CFile::modeCreate | CFile::modeWrite);
i=str.GetLength();
OutFile.SeekToEnd();
OutFile.Write(str,i);
OutFile.Close();*/
FILE *stream=NULL;
if((stream = fopen( SaveFile, "a+" ))!= NULL)
{
fputs( str+"\n", stream );//写一行
fclose( stream );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -