Eterlogic Virtual Drive SDK documentation


C++ Floppy disk

The code below demonstrates simple floppy disk implementation using C++.

Using Native DLL API, it creates disk 'Z' and handles requests.

Cpp.PNG

#include <windows.h>
#include <stdio.h>
#include <vector>
#include <conio.h>

#include "VDSDKDll.h"

#define DISK_SIZE 1024 * 1024 * 2
DRIVE_HANDLE g_hDrive = INVALID_DRIVE_HANDLE;

int MountVirtualDisk(char DriveLetter);
HANDLE g_hFile;

int main(int argc, char* argv[])
{
    char DriveLetter = 'Z';
    
    if(argc < 2)
    {
        printf("Usage: IsoDisk.exe iso_path disk_letter\n");
        return -1;
    }

    g_hFile = CreateFile(argv[1],GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,0,
        OPEN_ALWAYS,FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS,0);

    if(g_hFile == INVALID_HANDLE_VALUE)
    {
        printf("Error: can not open file %s\n", argv[1]);
        return -1;
    }

    ::SetFilePointer(g_hFile, DISK_SIZE, NULL, FILE_BEGIN);
    ::SetEndOfFile(g_hFile);

    if(argc >= 3)
        DriveLetter = argv[2][0];

    if(!InitializeVDSDK())
    {
        printf("Error: Can not initialize VDSDK\n");
        return 1;
    }

    // Activate VDSDK.
    // First parameter is activation key shipped with commercial license
    // In demo version this method always returns TRUE
    if(!ActivateVDSDK(""))
    {
        printf("ERROR: can not activate VDSDK\n");
        return 1;
    }


    MountVirtualDisk(DriveLetter);

    // Shutdown VDSDK
    ShutdownVDSDK(TRUE);

    return 0;
}


// ===========================================
// Virtual disk implementation
// ===========================================

// read callback handler
BOOL _stdcall OnReadCallback
(
 DRIVE_HANDLE h,
 ULONGLONG ReadOffset,
 ULONG ReadSize,
 void* ReadBuffer,
 ULONG *BytesRead
 )
{
    LONG low = (LONG)(ReadOffset & 0xffffffff);
    printf("OnReadCallback: low_offset=0x%08X size=%X\n", low, ReadSize);

    LARGE_INTEGER l;
    DWORD dw;
    l.QuadPart = ReadOffset;
    SetFilePointer(g_hFile,l.LowPart,&l.HighPart,FILE_BEGIN);
    BOOL result = ReadFile(g_hFile,ReadBuffer,ReadSize,&dw,NULL);
    *BytesRead = dw;
    return result;
}


// write callback handler
BOOL _stdcall OnWriteCallback
(
 DRIVE_HANDLE h,
 ULONGLONG WriteOffset,
 ULONG WriteSize,
 const void* WriteBuffer,
 ULONG *BytesWritten
 )
{
    LONG low = (LONG)(WriteOffset & 0xffffffff);
    printf("OnWriteCallback: low_offset=0x%08X size=%X\n", low, WriteSize);

    LARGE_INTEGER l;
    DWORD dw;
    l.QuadPart = WriteOffset;
    SetFilePointer(g_hFile,l.LowPart,&l.HighPart,FILE_BEGIN);
    BOOL result = WriteFile(g_hFile,WriteBuffer,WriteSize,&dw,NULL);
    *BytesWritten = dw;
    return result;
}


int MountVirtualDisk(char DriveLetter)
{
    printf("Creating drive...\n");

    DWORD dwSize = DISK_SIZE / (1024 * 1024);

    // create virtual disk
    g_hDrive = CreateVirtualDriveEx(DriveLetter,dwSize,
        OnReadCallback,
        OnWriteCallback, FALSE, DRIVE_FLAG_FLOPPY
        );

    if(g_hDrive == INVALID_DRIVE_HANDLE)
    {
        printf("ERROR: can not create virtual drive\n");
        return 1;
    }

    printf("VirtualDrive created. Handle = %d\n",g_hDrive);
    printf("Press any key to quit\n");

    // wait until any key pressed
    getch();

    // dismount disk
    if(!DestroyVirtualDrive(g_hDrive,FALSE))
    {
        printf("Dismount failed. Forcing dismount operation...\n");
        // force dismount...
        DestroyVirtualDrive(g_hDrive,TRUE);
    }

    return 0;
}

(C) 2008-2009 Eterlogic.com. All rights reserved.