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

📄 main.c

📁 一个关于usb编程的源码
💻 C
📖 第 1 页 / 共 2 页
字号:

            SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Toggle 8051 Reset");

            // Get the text in the driver name edit box
            GetDlgItemText (hDlg, IDC_DRIVER_NAME, pcDriverName, MAX_DRIVER_NAME);

            // Open the driver
            if (bOpenDriver (&hDevice, pcDriverName) == TRUE) {
             SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Opened Driver Successfully");
            } else {
             SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Driver");
             hDevice = NULL;
            }/* else */


            myRequest.bRequest = 0xA0;
            myRequest.wValue = 0x7F92;
            myRequest.wIndex = 0x00;
            myRequest.wLength = 0x01;
            myRequest.bData = (LOWORD(wParam) == IDC_RESET_HLD) ? 1 : 0;
            myRequest.direction = 0x00;
          
            if (hDevice != NULL)
            {
             
               // Perform the Get-Descriptor IOCTL
               bResult = DeviceIoControl (hDevice,
                           IOCTL_Ezusb_VENDOR_REQUEST,
                           &myRequest,
                           sizeof(VENDOR_REQUEST_IN),
                           NULL,
                           0,
                           &nBytes,
                           NULL);
             
            }/* if valid driver handle */
          
            if (bResult==TRUE)
            {
                // do nothin
            }
            else
               SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Reset Failed");
          
            // Close the handle
            CloseHandle (hDevice);
         }
       
         break;
       
         case IDC_ANCHOR_DOWNLOAD:
         {
            #define MAX_FILE_SIZE (1024*7)
            char DownloadFilename[256];
            FILE *fp;
            unsigned char buffer[MAX_FILE_SIZE];
            int numread = 0;
            char                   temp[64]  = "";

            hOutputBox = GetDlgItem (hDlg, IDC_OUTPUT_BOX);

            MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);

            // Get the text in the driver name edit box
            GetDlgItemText (hDlg, IDC_DOWNLOAD_FILENAME, DownloadFilename, 256);

            if ((fp = fopen(DownloadFilename,"rb")) != NULL)
            {
               numread = fread(buffer,sizeof(unsigned char),MAX_FILE_SIZE,fp);
               wsprintf (temp, "Anchor Download %d bytes",numread);
               SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
               if (IsDlgButtonChecked(hDlg,IDC_VERBOSE) == BST_CHECKED)
                DumpBuffer(buffer,80,hOutputBox);
               fclose(fp);
            }
          
            // Get the text in the driver name edit box
            GetDlgItemText (hDlg, IDC_DRIVER_NAME, pcDriverName, MAX_DRIVER_NAME);
          
             // Open the driver
             if (bOpenDriver (&hDevice, pcDriverName) == TRUE) {
                SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Opened Driver Successfully");
             } else {
                SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Failed to Open Driver");
                hDevice = NULL;
             }/* else */
          
             if (hDevice != NULL) {
             
                bResult = DeviceIoControl (hDevice,
                   IOCTL_Ezusb_ANCHOR_DOWNLOAD,
                   buffer,
                   numread,
                   NULL,
                   0,
                   &nBytes,
                   NULL);
             
             }/* if valid driver handle */
          
             if (bResult==TRUE)
             {
                // do nothin
             }
             else
                SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)"Anchor Download failed");
          
             // Close the handle
             CloseHandle (hDevice);
          
          }
          break;
       
       
       
       } /*end switch wParam*/
    
       break;
          
          
    } /*end switch message*/
    
    return FALSE;
    
} /*end MainDlgProc*/

#define BYTES_PER_LINE 0x10

void
DumpBuffer(PVOID pvBuffer, ULONG length, HWND hOutputBox)
{
   int                    nItems    = 0;
   char                   temp[64]  = "";
   char                   temp2[64]  = "";
   ULONG	i;
   ULONG   j;
   PUCHAR	ptr;
   
   MAINTAIN_OUTPUT_BOX (hOutputBox, nItems);
   
   ptr = (PUCHAR) pvBuffer;
   
   for (i = 0; i < ((length + BYTES_PER_LINE - 1) / BYTES_PER_LINE); i++)
   {
      wsprintf(temp,"%04X ",(i*BYTES_PER_LINE));
      for (j = 0; j < BYTES_PER_LINE; j++)
      {
         if (((i * BYTES_PER_LINE) + j) < length)
         {
            wsprintf(temp2,"%02X ",*ptr++);
            strcat(temp,temp2);
         }
      }
      SendMessage (hOutputBox, LB_ADDSTRING, 0, (LPARAM)temp);
   }
}

/**************************************************
* bOpenDriver proc                               *
*                                                *
* Purpose:                                       *  
*      Opens the device driver using symbolic    *
*      name provided                             *
*                                                *
* Input:                                         *
*      phDeviceHandle:                           *
*          Pointer to Device Driver handle where *
*          the file handle is placed.            *
*      devname:                                  *
*          Null terminated string containing the *
*          device name                           *
*                                                *
* Return Value:                                  *
*      Boolean that indicates if the driver was  *
*      successfully opened or not.               *
*                                                *
**************************************************/

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*/
   
   
}//OpenDevice




#define ONE_K       0x400
#define ONE_MEG     (ONE_K * ONE_K)

// Assign data only if successfully scanned data.
// Return 0 if no data scanned
int my_sscanf(LPCSTR cBuffer, DWORD *data)
{
   char *ptr;
   char buffer[0x100];
   int value;
   
   strcpy(buffer, cBuffer);
   
   if (ptr = strchr(_strlwr(buffer), 'k'))
   {
      *ptr = 0;
      if (sscanf(buffer, "%d", &value))
      {
         *data = value * ONE_K;
         return 1;
      }
      *ptr = 'K';		// failed, but let's put it back
   }
   
   if (ptr = strchr(_strlwr(buffer), 'm'))
   {
      *ptr = 0;
      if (sscanf(buffer, "%d", &value))
      {
         *data = value * ONE_MEG;
         return 1;
      }
      *ptr = 'M';		// failed, but let's put it back
   }
   
   if (ptr = strpbrk(buffer, "xX"))
   {
      if (sscanf(ptr+1, "%x", &value))
      {
         *data = value;
         return 1;
      }
   }
   else if (sscanf(buffer, "%x", &value))
   {
      *data = value;
      return 1;
   }
   return 0;
}

void FillBuffer(PUCHAR buffer, PUCHAR pattern, int length)
{
   int i;

   for (i = 0; i < length; i++)
   {
      buffer[i] = *pattern;
      // increment the pattern
      (*pattern)++;
   }
}

BOOL BufferHasErrors(PUCHAR buffer, PUCHAR pattern, int length,BOOL inEqualsNotOut)
{
   int i;
   char tempbuff[256];

   for (i = 0; i < length; i++)
   {

      if (inEqualsNotOut)
      {
         if (buffer[i] != ((~(*pattern)) & 0xFF))
         {
            wsprintf(tempbuff, "expected 0x%x got 0x%x", (~(*pattern)) & 0xFF, buffer[i]);
            SendMessage (ghOutputBox, LB_ADDSTRING, 0, (LPARAM)tempbuff);
            DumpBuffer(buffer,length,ghOutputBox);
            return TRUE;
         }
      }
      else
      {
         if (buffer[i] != ((*pattern) & 0xFF))
         {
            wsprintf(tempbuff, "expected 0x%x got 0x%x", (*pattern) & 0xFF, buffer[i]);
            SendMessage (ghOutputBox, LB_ADDSTRING, 0, (LPARAM)tempbuff);
            DumpBuffer(buffer,length,ghOutputBox);
            return TRUE;
         }
      }

      // increment the pattern
      (*pattern)++;
   }
   return FALSE;
}

⌨️ 快捷键说明

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