Eterlogic Virtual Drive SDK documentation


C++ RAM disk (COM interface)

The code below demonstrates simple virtual drive implementation using C++.

Implementing IVirtualDriveHandler3 COM interface, it creates disk 'Z' and handles requests.

Cpp.PNG

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

#include <atlbase.h>

CComModule _Module;

#include <atlcom.h>

#include "VDSDKComServer.h"

const ULONGLONG g_disk_size = 16;



class CVirtualDrive :
    public CComObjectRoot,
    public IDispatchImpl<IVirtualDriveHandler3,&__uuidof(IVirtualDriveHandler3)>
{
public:
    BEGIN_COM_MAP(CVirtualDrive)
        COM_INTERFACE_ENTRY(IVirtualDriveHandler3)
        COM_INTERFACE_ENTRY(IDispatch)
    END_COM_MAP()

    CVirtualDrive() : m_data(NULL)
    {
    }
    ~CVirtualDrive()
    {
        delete[] m_data;
    }
    void Init(ULONG diskSize)
    {
        ULONG sz = diskSize * 1024 * 1024;
        m_data = new unsigned char[sz];
        memset(m_data,0,sz);
    }


    STDMETHOD(OnReadData )(ULONGLONG Offset,ULONG Size,UCHAR *Buffer,ULONG *BytesRead)
    {
        printf("===> OnReadData %d %d\n",(ULONG)Offset,Size);
        memcpy(Buffer,m_data+Offset,Size);
        *BytesRead = Size;
        return S_OK;
    }

    STDMETHOD(OnWriteData )( ULONGLONG Offset,ULONG Size,UCHAR *Buffer,ULONG *BytesWritten)
    {
        printf("===> OnWriteData %d %d\n",(ULONG)Offset,Size);
        memcpy(m_data+Offset,Buffer, Size);
        *BytesWritten = Size;
        return S_OK;
    }
private:
    unsigned char* m_data;
};



bool InstallHandler()
{
    CComPtr<IVirtualDrivesManager> pVDSDK;
    HRESULT hr;
    BOOL result;
    LONG driveHandle;
    hr = pVDSDK.CoCreateInstance ( __uuidof(VirtualDrivesManager),NULL,CLSCTX_INPROC_SERVER);

    if(FAILED(hr))
    {
        printf("ERROR: unknown interface\n");
        return false;
    }

    // initialize VDSDK
    if(FAILED(pVDSDK->InitializeVDSDK(&result)) || result == FALSE)
    {
        printf("ERROR: can not initialize VDSDK\n");
        return false;
    }

    // Activate VDSDK.
    // First parameter is activation key shipped with commercial license
    // In demo version this method always returns TRUE
    if(FAILED(pVDSDK->ActivateVDSDK(L"",&result)) || result == FALSE)
    {
        printf("ERROR: can not activate VDSDK\n");
        return false;
    }

    // create callbacks handlers implementation
    CComObject<CVirtualDrive>* pHandler;
    hr = CComObject<CVirtualDrive>::CreateInstance(&pHandler);
    pHandler->Init(g_disk_size);
    if(FAILED(hr))
    {
        printf("ERROR: can not create CVirtualDrive class instance\n");
        return false;
    }

    // create virtual drive
    hr = pVDSDK->CreateVirtualDrive('Z',g_disk_size,pHandler,&driveHandle);
    if(FAILED(hr) || driveHandle == -1)
    {
        printf("Error: can not create virtual drive\n");
        return false;
    }

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

    // destroy virtual drive
    pVDSDK->DestroyVirtualDrive(driveHandle,TRUE,&result);
    // shutdown SDK
    pVDSDK->ShutdownVDSDK(TRUE);
    return true;
}

int main()
{
    ::CoInitialize(NULL);
    InstallHandler();
    ::CoUninitialize();

    return 0;
}

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