Eterlogic Virtual Drive SDK documentation


Python 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.

Python.PNG

#python

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"])

# 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


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

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

# 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'
    os._exit(1)


handle = server.CreateVirtualDrive(ord('Z'),g_disk_size,clientIUnknown)
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'
    msvcrt.getch()

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

server.ShutdownVDSDK(True)
                          

This is a python code sample that creates 26 ram disks.

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.

#python

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"])

# 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


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

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

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

handles = []

# Try to create 26 virtual drives
for idx in range(0, 25):
    handle = server.CreateVirtualDrive(ord('A') + idx,g_disk_size,clientIUnknown)
    if handle != -1:
        print 'VirtualDrive created. Handle =',handle
        handles.append(handle);
    else:
        print 'ERROR: Can not create virtual drive'


print 'Press any key to quit'
msvcrt.getch()

print 'Destroying virtual drives...'
for handle in handles:
    result = server.DestroyVirtualDrive(handle,1)
    if result:
        print 'OK'
    else:
        print 'ERROR'

server.ShutdownVDSDK(True)
                          

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