📄 mount.c
字号:
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <string.h>
#include "ioctl.h"
int AllocDriveLetter(void)
{
// Search for an unused drive letter
DWORD dwDriveBitmaps = GetLogicalDrives();
const int BITS_OF_DWORD = 32;
int iDrive;
DWORD dwMask = 0x8;
for( iDrive = 3; iDrive < BITS_OF_DWORD; iDrive ++ )
{
if( dwMask & dwDriveBitmaps )
{
dwMask <<= 1;
}
else
{
break;
}
}
return iDrive;
}
BOOL
VDisk_AddDrive(PCSTR szIpAddress, PCSTR szFile)
{
St_OpenImageFile stCmd;
DWORD dwBytesReturned;
HANDLE hDevice;
strcpy( stCmd.szIpAddress, szIpAddress );
strcpy( stCmd.szRemotePath, szFile );
hDevice = CreateFile(
"\\\\.\\NetDisk",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
0,
OPEN_EXISTING,
0,
NULL);
if (hDevice != INVALID_HANDLE_VALUE)
{
BOOL isDone;
int iDrive = AllocDriveLetter();
isDone = DeviceIoControl( hDevice,
IOCTL_ADD_DRIVE,
&stCmd, sizeof(stCmd),
NULL, 0,
&dwBytesReturned, NULL);
if( isDone )
{
char aryDriveLetter[4] = "A:";
aryDriveLetter[0] += iDrive;
isDone = DefineDosDevice( DDD_RAW_TARGET_PATH,
aryDriveLetter, "\\Device\\Vdisk0" );
if( isDone )
{
printf( "The newly created drive is %s\n", aryDriveLetter );
}
}
CloseHandle(hDevice);
if( !isDone )
{
printf( "Fail to create a virtual drive!\n" );
}
return isDone;
}
printf( "Fail to open NetDisk device!\n" );
return FALSE;
}
BOOL
VDisk_RemoveDrive(int iDriveNum)
{
char aryDosDevName[MAX_PATH];
HANDLE hDevice;
wsprintf(aryDosDevName, "\\\\.\\%c:", iDriveNum+'A');
hDevice = CreateFile(
aryDosDevName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (hDevice != INVALID_HANDLE_VALUE)
{
DWORD dwBytesReturned;
BOOL isDone;
wsprintf(aryDosDevName, "%c:", iDriveNum+'A');
// Remove the drive letter
isDone = DefineDosDevice( DDD_REMOVE_DEFINITION,
aryDosDevName, NULL );
if( isDone )
{
isDone = DeviceIoControl(
hDevice,
IOCTL_REMOVE_DRIVE,
NULL, 0,
NULL, 0,
&dwBytesReturned,
NULL );
}
CloseHandle(hDevice);
if( isDone )
{
printf( "Done.\n" );
return TRUE;
}
return FALSE;
}
return FALSE;
}
BOOL
VDisk_Insert(int iDriveNum, PCSTR szFile)
{
char aryDosDevName[MAX_PATH];
HANDLE hDevice;
wsprintf(aryDosDevName, "\\\\.\\%c:", iDriveNum+'A');
hDevice = CreateFile(
aryDosDevName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (hDevice != INVALID_HANDLE_VALUE)
{
DWORD dwBytesReturned;
CHAR aryAnsiBuffer[MAX_PATH];
WCHAR aryBuffer[MAX_PATH];
BOOL isDone;
wsprintf( aryAnsiBuffer, "\\??\\%s", szFile );
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED,
aryAnsiBuffer, -1, aryBuffer, MAX_PATH );
isDone = DeviceIoControl(
hDevice,
IOCTL_INSERT,
aryBuffer, MAX_PATH*sizeof(WCHAR),
NULL, 0,
&dwBytesReturned,
NULL );
if( isDone )
{
wsprintf(aryDosDevName, "%c:", iDriveNum+'A');
// Remove the drive letter
if( DefineDosDevice( DDD_REMOVE_DEFINITION,
aryDosDevName, NULL ) )
{
isDone = DefineDosDevice( DDD_RAW_TARGET_PATH,
aryDosDevName, "\\Device\\Vdisk0" );
}
}
CloseHandle(hDevice);
return isDone;
}
return FALSE;
}
BOOL
VDisk_Eject(int iDriveNum)
{
char aryDosDevName[MAX_PATH];
HANDLE hDevice;
wsprintf(aryDosDevName, "\\\\.\\%c:", iDriveNum+'A');
hDevice = CreateFile(
aryDosDevName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (hDevice != INVALID_HANDLE_VALUE)
{
DWORD dwBytesReturned;
BOOL isDone = DeviceIoControl(
hDevice,
IOCTL_EJECT,
NULL, 0,
NULL, 0,
&dwBytesReturned,
NULL );
CloseHandle(hDevice);
return isDone;
}
return FALSE;
}
static
void
ShowUsage(void)
{
printf(
"VMount - The mount utility for the vdisk driver\n"
"Usage:\n"
"vmount add|remove|insert|eject [parameter..]\n"
" add ip_address file_name\n"
" remove drive_letter\n"
" insert drive_letter file_name\n"
" eject drive_letter\n" );
}
int
main( int argc, char * argv[] )
{
if( argc < 2 )
{
ShowUsage();
return -1;
}
if( strcmp( argv[1], "add" ) == 0 )
{
if( argc < 4 )
{
ShowUsage();
return -1;
}
if( VDisk_AddDrive(argv[2], argv[3]) )
{
return 0;
}
return -1;
}
else if( strcmp( argv[1], "remove" ) == 0 )
{
int iDriveNum;
if( argc < 3 )
{
ShowUsage();
return -1;
}
iDriveNum = argv[2][0] - 'A';
if( VDisk_RemoveDrive(iDriveNum) )
{
return 0;
}
printf( "Fail to remove the specified drive!\n" );
return -1;
}
else if( strcmp( argv[1], "insert" ) == 0 )
{
int iDriveNum;
if( argc < 4 )
{
ShowUsage();
return -1;
}
iDriveNum = argv[2][0] - 'A';
if( VDisk_Insert(iDriveNum, argv[3]) )
{
return 0;
}
printf( "Fail to insert an image file!\n" );
return -1;
}
else if( strcmp( argv[1], "eject" ) == 0 )
{
int iDriveNum;
if( argc < 3 )
{
ShowUsage();
return -1;
}
iDriveNum = argv[2][0] - 'A';
if( VDisk_Eject(iDriveNum) )
{
return 0;
}
printf( "Fail to eject a drive!\n" );
return -1;
}
ShowUsage();
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -