Eterlogic Virtual Drive SDK documentation

Python removable RAM disk (COM interface)

This is a python code sample that creates and formats disk 'Z'.

It uses IVirtualDrivesManager COM interface that exposes VDSDK power to any script language. To represent virtual device python script implements IVirtualDriveHandler COM interface. To run the script you should have PythonWin extention installed.



import win32com.client
import pythoncom
import win32com.server.util
import msvcrt
import ctypes
import os

from array import *
from win32com import universal
from win32com.server.util import wrap

# register our interfaces
universal.RegisterInterfaces('{aaadcf2c-39ae-4faa-bfc7-7b83d02ebc4f}', 0, 1, 0, ["IVirtualDriveHandler"])
universal.RegisterInterfaces('{aaadcf2c-39ae-4faa-bfc7-7b83d02ebc4f}', 0, 1, 0, ["IVirtualDriveFormatCallbackHandler"])
universal.RegisterInterfaces('{aaadcf2c-39ae-4faa-bfc7-7b83d02ebc4f}', 0, 1, 0, ["IVDSDKNotificationHandler"])

# constants
VDSDK_OnUnmountRemovableMedia = 0
VDSDK_OnUnmountedDrive = 1

# disk size, MB
g_disk_size = 256;

# virtual drive callbacks                          
class CVirtualDriveHandler:
    _public_methods_ = ['OnReadData','OnWriteData']
    _com_interfaces_ = ['IVirtualDriveHandler']

    def __init__(self):
        self.m_data = ctypes.create_string_buffer(g_disk_size*1024*1024)

    def OnReadData(self, Offset,Size,Buffer):
        Offset = int(Offset)
        print '===> OnReadData',Offset,Size
        Buffer = buffer(self.m_data, Offset, Size)
                return (0,Buffer,Size)

    def OnWriteData(self, Offset,Size,Buffer):
            Offset = int(Offset)
        print '===> OnWriteData',Offset,Size 
            for i in range(0,Size):
            self.m_data[Offset+i] = Buffer[i]
            return Size

# Formatting progress
class CVirtualDriveFormatHandler:
    _public_methods_ = ['OnFormatCallback']
    _com_interfaces_ = ['IVirtualDriveFormatCallbackHandler']

    def OnFormatCallback(self, Progress):
        print 'Formatting progress: ' + str(Progress) + '%'
        return True

# Notifications
class CVDSDKNotificationHandler:
    _public_methods_ = ['OnVDSDK_Notification']
    _com_interfaces_ = ['IVDSDKNotificationHandler']

    def OnVDSDK_Notification(self, DriveHandle, Code, Reserved):
        if Code == VDSDK_OnUnmountRemovableMedia:
            print 'VDSDK_OnUnmountRemovableMedia'
        if Code == VDSDK_OnUnmountedDrive:
            print 'VDSDK_OnUnmountedDrive'  
        return True

#create server and client instance
server = win32com.client.dynamic.Dispatch("VDSDK.VirtualDrivesManager")
client = CVirtualDriveHandler()
clientIUnknown = wrap(client)
notificationHandler = CVDSDKNotificationHandler()
notificationHandlerUnknown = wrap(notificationHandler)

# Initialize SDK
if server.InitializeVDSDK() == False:
    print 'Error: can not initialize VDSDK'

# Activate VDSDK.
# First parameter is activation key shipped with commercial license
# In demo version this method always returns TRUE
if server.ActivateVDSDK('') == False:
    print 'Error: can not activate VDSDK'

#register notification handler

handle = server.CreateVirtualDriveEx(ord('Z'),g_disk_size,clientIUnknown,0,DRIVE_FLAG_REMOVABLE)
if handle != -1:
    print 'VirtualDrive created. Handle =',handle

    # Full NTFS formatting
    server.FormatVirtualDrive(handle,False, 'NTFS','VDSDK Python!',wrap(CVirtualDriveFormatHandler()))
    print 'Formatting completed'

    print 'Press any key to quit'

    print 'Destroying virtual drive...'
    result = server.DestroyVirtualDrive(handle,1)
    if result:
        print 'OK'
        print 'ERROR'
    print 'ERROR: Can not create virtual drive'


(C) 2008-2009 All rights reserved.