Click here to Skip to main content
15,884,353 members
Articles / Programming Languages / C

How to develop a virtual disk for Windows

,
Rate me:
Please Sign up or sign in to vote.
4.99/5 (98 votes)
15 Feb 2010CPOL10 min read 195.4K   8.2K   335  
This article is oriented to Windows-developers and considers how to create virtual disk in Windows system.
#ifndef KERNEL_RESOURCE_WRAPPER_H_INCLUDED
#define KERNEL_RESOURCE_WRAPPER_H_INCLUDED
extern "C"
{
#include "ntifs.h"
}
#include "sync.h"
#include "new"
#include "exception"

namespace utils
{

template<EVENT_TYPE eventType>
class KernelCustomEvent
{
    KEVENT event_;

    KernelCustomEvent(const KernelCustomEvent&);
    KernelCustomEvent& operator =(const KernelCustomEvent&);
public:
    KernelCustomEvent(BOOLEAN initState = FALSE)
    {
        KeInitializeEvent(&event_, eventType, initState);
    }
    KEVENT* getPointer()
    {
        return &event_;
    }
    void wait()
    {
        NTSTATUS status = KeWaitForSingleObject(&event_, Executive, KernelMode, FALSE, NULL);
        if(!NT_SUCCESS(status))
        {
            KdPrint((__FUNCTION__" KeWaitForMutexObject fail %08X\n", status));
            __asm int 3;
        }
    }
    bool readState()
    {
        return KeReadStateEvent(&event_) != 0;
    }
    void set()
    {
        KeSetEvent(&event_, 0, FALSE);
    }
    void reset()
    {
        KeClearEvent(&event_);
    }
};

typedef KernelCustomEvent<NotificationEvent> KernelNotificationEvent;
typedef KernelCustomEvent<SynchronizationEvent> KernelSynchronizationEvent;

template<class LockType>
class ReverseSemaphore
{
    KernelNotificationEvent readyEvent_;
    LockType lock_;
    int count_;
public:
    ReverseSemaphore():count_(0), readyEvent_(TRUE){}
    KEVENT* getPointer()
    {
        return readyEvent_.getPointer();
    }
    void enter()
    {
        AutoGuard<LockType> guard(lock_);
        ++count_;
        readyEvent_.reset();
    }
    void leave()
    {
    bool ready = false;
    {
            AutoGuard<LockType> guard(lock_);
            --count_;
            ASSERT(count_ >= 0);
            if(count_ == 0)
            ready = true;
    }
    if(ready)
            readyEvent_.set();
    }
};
typedef ReverseSemaphore<Mutex> ReverseMutexSemaphore;
typedef AutoGuard<ReverseMutexSemaphore> AutoReverseMutexSemaphore;
typedef AutoGuardPtr<ReverseMutexSemaphore> AutoReverseMutexSemaphorePtr;


//---------------------------------------------------------------
// auto_ptr_kernel
template<class ResourceType>
class auto_ptr_kernel
{
    ResourceType * m_pResource;
public:
    auto_ptr_kernel()
        : m_pResource(0)
    {
    }
        
    explicit auto_ptr_kernel(ResourceType * pResource)
        : m_pResource( pResource )
    {
    }
    auto_ptr_kernel(auto_ptr_kernel & source_ptr)
        :
        m_pResource( source_ptr.get() )
    {
        source_ptr.m_pResource = 0;
    }

    auto_ptr_kernel & operator = (auto_ptr_kernel & source_ptr)
    {
        m_pResource = source_ptr.get();
        source_ptr.m_pResource = 0;
        return *this;
    }

    ~auto_ptr_kernel()
    {
        if (m_pResource)
            ExFreePool( m_pResource );
    }

    const ResourceType * get() const { return m_pResource; }
    ResourceType * get() { return m_pResource; }

    ResourceType * release()
    {
        ResourceType * pRes = m_pResource;
        m_pResource = 0;
        return pRes;
    }

    ResourceType * operator -> ()   {   return m_pResource;    }
    const ResourceType * operator -> () const {   return m_pResource;    }
    ResourceType & operator * ()   {   return *m_pResource;    }
    const ResourceType & operator * () const {   return *m_pResource;    }
    
    // non-standard
    ResourceType ** getPtr2() 
    { 
        if (m_pResource)
            throw std::exception("Cannot access member");
        return &m_pResource; 
    }
};
//--------------------------------------------------------------
inline void * AllocatePaged(int iAdditionalSize)
{
    void * pData = ExAllocatePool(PagedPool, iAdditionalSize);
    if (!pData)
        throw std::bad_alloc();
    return pData;
}
//--------------------------------------------------------------
template<class Type>
Type * AllocatePaged(int iAdditionalSize=0)
{
    void * pData = ExAllocatePool(PagedPool, sizeof(Type)+iAdditionalSize);
    if (!pData)
        throw std::bad_alloc();
    memset(pData, 0, sizeof(Type));
    return (Type * )pData;
}
//--------------------------------------------------------------
template<class Type>
Type * AllocateNonPaged(int iAdditionalSize=0)
{
    void * pData = ExAllocatePool(NonPagedPool, sizeof(Type)+iAdditionalSize);
    if (!pData)
        throw std::bad_alloc();
    memset(pData, 0, sizeof(Type));
    return (Type * )pData;
}
//--------------------------------------------------------------

}//namespace utils
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Apriorit Inc.
United States United States
ApriorIT is a software research and development company specializing in cybersecurity and data management technology engineering. We work for a broad range of clients from Fortune 500 technology leaders to small innovative startups building unique solutions.

As Apriorit offers integrated research&development services for the software projects in such areas as endpoint security, network security, data security, embedded Systems, and virtualization, we have strong kernel and driver development skills, huge system programming expertise, and are reals fans of research projects.

Our specialty is reverse engineering, we apply it for security testing and security-related projects.

A separate department of Apriorit works on large-scale business SaaS solutions, handling tasks from business analysis, data architecture design, and web development to performance optimization and DevOps.

Official site: https://www.apriorit.com
Clutch profile: https://clutch.co/profile/apriorit
This is a Organisation

33 members

Written By
Team Leader ApriorIT
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions