⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ramdisk.cpp

📁 利用系统内存建立虚拟磁盘
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/********************************************************************	created:	2002/9/27  15:53	filename: 	f:\winddk\2600\src\storage\ramdisk\dll\ramdisk.cpp	file path:	f:\winddk\2600\src\storage\ramdisk\dll	file base:	ramdisk	file ext:	cpp	author:		Christiaan Ghijselinck		purpose:	*********************************************************************/
/*  Copyright (C) 2002

    The source code provided here is havenly based on the source code provided by 
    "PHD Computer Consultants Ltd" from their "... Windows Driver Model Article" and 
    on the "Setup\PNPPorts" and "Setup\Enable" sample provided within the
    Microsoft W2K DDK.

    This code should also reveal how primary MFC functions ( CString, CArray, and similar )
    can be used within Driver Property Page providers.

 *************************************************************************/

/***************************************************************************The author of this source including files that are used by this source,rejects all responsability for its proper functioning due to changes madeby other than the author without his knowledge or consultation. The changedsources will be considered as not his property, which implies that the changes made by other than the author, are considered as not existing if theauthor decides to enhance and/or correct his original software.These statements agree with the world wide and local dictated laws aboutauthorship and violence against these laws.****************************************************************************/

//==========================================================================
//                                Include files
//==========================================================================

// C Runtime
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <windowsx.h>

// Application specific
#include "RAMDisk.h"
#include <dbt.h>

//==========================================================================
//                                Globals
//==========================================================================

// some MFC stuff ....
extern "C" void * _afxForceEXCLUDE = NULL ;
extern "C" void * _afxForceUSRDLL  = NULL ;
extern "C" void * _afxForceSTDAFX  = NULL ;
LPCTSTR _afxPchNil = NULL ;


#ifdef _DEBUG
PWCHAR __DIFConverter[] = { 

   L"DIF_null" ,
   L"DIF_SELECTDEVICE" ,
   L"DIF_INSTALLDEVICE" ,
   L"DIF_ASSIGNRESOURCES" ,
   L"DIF_PROPERTIES" ,
   L"DIF_REMOVE" ,
   L"DIF_FIRSTTIMESETUP" ,
   L"DIF_FOUNDDEVICE" ,
   L"DIF_SELECTCLASSDRIVERS" ,
   L"DIF_VALIDATECLASSDRIVERS" ,
   L"DIF_INSTALLCLASSDRIVERS" ,
   L"DIF_CALCDISKSPACE" ,
   L"DIF_DESTROYPRIVATEDATA" ,
   L"DIF_VALIDATEDRIVER" ,
   L"DIF_MOVEDEVICE" ,
   L"DIF_DETECT" ,
   L"DIF_INSTALLWIZARD" ,
   L"DIF_DESTROYWIZARDDATA" ,
   L"DIF_PROPERTYCHANGE" ,
   L"DIF_ENABLECLASS" ,
   L"DIF_DETECTVERIFY" ,
   L"DIF_INSTALLDEVICEFILES" ,
   L"DIF_UNREMOVE" ,
   L"DIF_SELECTBESTCOMPATDRV" ,
   L"DIF_ALLOW_INSTALL" ,
   L"DIF_REGISTERDEVICE" ,
   L"DIF_NEWDEVICEWIZARD_PRESELECT" ,
   L"DIF_NEWDEVICEWIZARD_SELECT" ,
   L"DIF_NEWDEVICEWIZARD_PREANALYZE" ,
   L"DIF_NEWDEVICEWIZARD_POSTANALYZE" ,
   L"DIF_NEWDEVICEWIZARD_FINISHINSTALL" ,
   L"DIF_UNUSED1" ,
   L"DIF_INSTALLINTERFACES" ,
   L"DIF_DETECTCANCEL" ,
   L"DIF_REGISTER_COINSTALLERS" ,
   L"DIF_ADDPROPERTYPAGE_ADVANCED" ,
   L"DIF_ADDPROPERTYPAGE_BASIC" ,
   L"DIF_RESERVED1" ,
   L"DIF_TROUBLESHOOTER" ,
   L"DIF_POWERMESSAGEWAKE" } ;
#endif // _DEBUG

PWCHAR   caDriveLetters[]   =   {   { L"A:" } , { L"B:" } , { L"C:" } , { L"D:" } , { L"E:" } , { L"F:" } ,
                                    { L"G:" } , { L"H:" } , { L"I:" } , { L"J:" } , { L"K:" } , { L"L:" } ,  
                                    { L"M:" } , { L"N:" } , { L"O:" } , { L"P:" } , { L"Q:" } , { L"R:" } ,  
                                    { L"S:" } , { L"T:" } , { L"U:" } , { L"V:" } , { L"W:" } , { L"X:" } , { L"Y:" } , { L"Z:" } } ;
                              

DWORD    dwaDriveSizes[]    =   {   0x10000 * 10   , 0x10000 * 20   , 0x10000 * 50   ,  
                                    0x10000 * 100  , 0x10000 * 200  , 0x10000 * 500  ,
                                    0x10000 * 1000 } ;

HANDLE  g_hInst  = NULL;

WCHAR   szRegServices[]   =   L"System\\CurrentControlSet\\Services\\Ramdisk\\Parameters" ;


//==========================================================================
//                                Dll Entry Point
//==========================================================================
extern "C"
BOOL APIENTRY LibMain( HANDLE hDll, DWORD dwReason, LPVOID lpReserved )
{
    switch( dwReason )
    {
    case DLL_PROCESS_ATTACH:
        g_hInst = hDll;
        DisableThreadLibraryCalls((HMODULE)hDll);

        break;

    case DLL_PROCESS_DETACH:
        break;

    default:
        break;
    }

    return TRUE;
}


//==========================================================================
//                                Functions
//==========================================================================

extern "C"
SHORT BitPosition ( DWORD aDWORD  )
{
/*
    // 16-bit code not using bsf , bsr
    // -------------------------------

    register
    unsigned short  result       ;

    result = 0 ;

    _asm   xor   bx,bx

//_1stword :

    _asm   mov   ax,word ptr aDWORD          
    _asm   xor   cx,cx

_back0 :
    _asm   inc   cx
    _asm   cmp   cx,16
    _asm   jg    _2ndword
    _asm   shr   ax,1
    _asm   jnc   _back0 
    _asm   cmp   bx,0
    _asm   jne   _errorexit
    _asm   mov   bx,cx
    _asm   jmp   _back0   

_2ndword :

    _asm   mov   ax,word ptr aDWORD + 2    
    _asm   mov   cx,16

_back1 :
    _asm   inc   cx
    _asm   cmp   cx,32
    _asm   jg    _exitp
    _asm   shr   ax,1
    _asm   jnc   _back1 
    _asm   cmp   bx,0
    _asm   jne   _errorexit
    _asm   mov   bx,cx
    _asm   jmp   _back1

_errorexit :
    _asm   mov   bx,0FFFFh
_exitp     :
    _asm   mov   word ptr result,bx

    return ( result ) ;

/**/ 
/*
    // 16-bit code using bsf , bsr
    // ---------------------------

    #pragma pack ( 1 )
    struct  {   unsigned short _WORD0 ;
                unsigned short _WORD1 ;
            } _4BYTE ;
    #pragma pack (   )

    register
    unsigned short  result ;

    *(DWORD*)&_4BYTE._WORD0 = aDWORD ;

    result 	=  0                     ;

//  FirstWord :

    _asm     bsf    ax,word ptr _4BYTE
    _asm {
             jz     SecndWord_1
             mov    word ptr result,0FFFFh	 // predefine error 
             bsr    bx,word ptr _4BYTE 
             cmp    ax,bx
             jne    ExitProc
             inc    ax   
             mov    word ptr result,ax    
         }

    SecndWord_1 :

    _asm     bsf    ax,word ptr _4BYTE + 2
    _asm {
             jz     ExitProc 
             cmp    word ptr result,0
             je     SecndWord_2
             mov    word ptr result,0FFFFh
             jmp    ExitProc
         }

    SecndWord_2 :

    _asm     mov    word ptr result,0FFFFh	 // predefine error 
    _asm     bsr    bx,word ptr _4BYTE + 2
    _asm {   cmp    ax,bx
             jne    ExitProc
             inc    ax
             add    ax,16              
             mov    word ptr result,ax    
         }

    ExitProc  :

    return ( result ) ;
 /**/    

    // 32-bit code using bsf , bsr
    // ---------------------------

    register
    unsigned short  result ;

    result 	=  0           ;
    _asm {   bsf    eax,dword ptr aDWORD
             jz     ExitProc 
             mov    word ptr result,0FFFFh	 // predefine error 
             bsr    ebx,dword ptr aDWORD
             cmp    eax,ebx
             jne    ExitProc
             inc    eax   
             mov    word ptr result,ax    
         }
    ExitProc  :
    return ( result ) ;

}

//==========================================================================

extern "C"
BOOL ChangeState ( DWORD NewState, DWORD SelectedItem , HDEVINFO hDevInfo )
{
    SP_PROPCHANGE_PARAMS        PropChangeParams    = {sizeof(SP_CLASSINSTALL_HEADER)};
    SP_DEVINFO_DATA             DeviceInfoData      = {sizeof(SP_DEVINFO_DATA)};
    SP_DEVINSTALL_PARAMS        devParams;

    //
    // Get a handle to the Selected Item.
    //
    if ( !SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData ) )
    {
        #ifdef _DEBUG
        OutputDebugStringW ( TEXT("EnumDeviceInfo") ) ;
        #endif
        return FALSE;
    }

    //
    // Set the PropChangeParams structure. Enable Global profile
    //
    PropChangeParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
    PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
    PropChangeParams.Scope = DICS_FLAG_GLOBAL;
    PropChangeParams.StateChange = NewState; 
    PropChangeParams.HwProfile = 0;

    if (!SetupDiSetClassInstallParams(hDevInfo,&DeviceInfoData,(SP_CLASSINSTALL_HEADER *)&PropChangeParams,sizeof(PropChangeParams)))
    {
        #ifdef _DEBUG
        OutputDebugStringW(TEXT("SetClassInstallParams"));
        #endif
        return FALSE;
    }
    PropChangeParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
    PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
    PropChangeParams.Scope = DICS_FLAG_CONFIGSPECIFIC;
    PropChangeParams.StateChange = NewState; 
    PropChangeParams.HwProfile = 0;

    if (!SetupDiSetClassInstallParams(hDevInfo,&DeviceInfoData,(SP_CLASSINSTALL_HEADER *)&PropChangeParams,sizeof(PropChangeParams))
        || !SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,&DeviceInfoData))
    {
        #ifdef _DEBUG
        OutputDebugStringW(TEXT("SetClassInstallParams"));
        #endif
        return FALSE;
    }
    else
    {
        //
        // see if device needs reboot
        //
        devParams.cbSize = sizeof(devParams);
        if(SetupDiGetDeviceInstallParams(hDevInfo,&DeviceInfoData,&devParams)&& (devParams.Flags & (DI_NEEDRESTART|DI_NEEDREBOOT))) 
        {
            #ifdef _DEBUG
            OutputDebugStringW(TEXT("SetClassInstallParams"));
            #endif
            return FALSE;
        } 
        else 
        {
            return TRUE;
        }
    }
}

//==========================================================================

// Thanks to Nate Bushman <"Nate.Bushman@powerquest.com"> for this code snippet  

extern "C"
DWORD
WINAPI
BroadcastVolumeDeviceChange ( DWORD Notification, WCHAR DriveLetter )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -