📄 ramdisk.bat
字号:
;@echo off
;goto make
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;
; RamDisk GUI
;
; Written by Four-F (four-f@mail.ru)
;
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.386
.model flat, stdcall
option casemap:none
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; I N C L U D E F I L E S
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\advapi32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\advapi32.lib
include \masm32\include\winioctl.inc
include \masm32\include\dbt.inc
include \masm32\Macros\Strings.mac
include ..\common.inc
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; Fix helper macro
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Fix MACRO txt:=<Fix this later!!!!>
local pos, spos
pos = 0
spos = 0
% FORC chr, @FileCur ;; Don't display full path. Easier to read.
pos = pos + 1
IF "&chr" EQ 5Ch ;; "/"
spos = pos
ENDIF
ENDM
% ECHO @CatStr(<Fix: >, @SubStr(%@FileCur, spos+1,), <(%@Line) - txt>)
ENDM
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; E Q U A T E S
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;DBT_DEVICEARRIVAL equ 8000 ; system detected a new device
;DBT_DEVICEREMOVEPENDING equ 8003 ; about to remove, still avail.
;DBT_DEVICEREMOVECOMPLETE equ 8004 ; device is gone
IDD_MAIN equ 1000
IDC_DRIVE_LETTER equ 1001
IDC_DISK_SIZE equ 1002
IDC_ROOT_DIRECTORY_ENTRIES equ 1003
IDC_SECTORS_PER_CLUSTER equ 1004
IDC_CREATE equ 1005
IDC_REMOVE equ 1006
IDI_ICON equ 2000
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; C O N S T A N T S
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.const
CTA0 "RamDisk.sys", g_szDriverFileName
CTA0 "RamDisk", g_szDriverName
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; U N I N I T I A L I Z E D D A T A
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.data?
g_hInstance HINSTANCE ?
g_hDlg HWND ?
g_hwndDriveLetters HWND ?
g_hwndDiskSize HWND ?
g_hwndRootDerectoryEntries HWND ?
g_hwndSectorsPerCluster HWND ?
g_hwndCreate HWND ?
g_hwndRemove HWND ?
g_hSCManager HANDLE ?
g_hService HANDLE ?
g_hDevice HANDLE ?
g_fRamdiskMounted BOOL ?
g_CreateParams CREATE_PARAMS <>
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; C O D E
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.code
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; MyUnhandledExceptionFilter
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
MyUnhandledExceptionFilter proc lpExceptionInfo:PTR EXCEPTION_POINTERS
; Just cleanup every possible thing
local _ss:SERVICE_STATUS
invoke CloseHandle, g_hDevice
invoke ControlService, g_hService, SERVICE_CONTROL_STOP, addr _ss
invoke DeleteService, g_hService
invoke CloseServiceHandle, g_hService
invoke CloseServiceHandle, g_hSCManager
mov eax, EXCEPTION_EXECUTE_HANDLER
ret
MyUnhandledExceptionFilter endp
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; PutRamDiskParamsIntoRegistry
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PutRamDiskParamsIntoRegistry proc uses esi
local fOk:BOOL
local hKey:HKEY
mov fOk, TRUE
lea esi, g_CreateParams
assume esi:ptr CREATE_PARAMS
invoke RegOpenKeyEx, HKEY_LOCAL_MACHINE, $CTA0("SYSTEM\\CurrentControlSet\\Services\\RamDisk"), \
0, KEY_CREATE_SUB_KEY + KEY_SET_VALUE, addr hKey
.if eax == ERROR_SUCCESS
invoke RegSetValueEx, hKey, $CTA0("DriveLetter"), NULL, REG_DWORD, addr [esi].dwDriveLetter, sizeof DWORD
.if eax != ERROR_SUCCESS
and fOk, FALSE
.endif
invoke RegSetValueEx, hKey, $CTA0("DiskSize"), NULL, REG_DWORD, addr [esi].nDiskSize, sizeof DWORD
.if eax != ERROR_SUCCESS
and fOk, FALSE
.endif
invoke RegSetValueEx, hKey, $CTA0("RootDirectoryEntries"), NULL, REG_DWORD, addr [esi].nRootDirectoryEntries, sizeof DWORD
.if eax != ERROR_SUCCESS
and fOk, FALSE
.endif
invoke RegSetValueEx, hKey, $CTA0("SectorsPerCluster"), NULL, REG_DWORD, addr [esi].nSectorsPerCluster, sizeof DWORD
.if eax != ERROR_SUCCESS
and fOk, FALSE
.endif
invoke RegCloseKey, hKey
.endif
assume esi:nothing
mov eax, fOk
ret
PutRamDiskParamsIntoRegistry endp
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; LoadDriver
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
LoadDriver proc
local hSCManager:HANDLE
local hService:HANDLE
local acModulePath[MAX_PATH]:CHAR
local fOk:BOOL
and fOk, FALSE
invoke OpenSCManager, NULL, NULL, SC_MANAGER_ALL_ACCESS ;SC_MANAGER_CONNECT + SC_MANAGER_CREATE_SERVICE
.if eax != NULL
mov hSCManager, eax
push eax
invoke GetFullPathName, addr g_szDriverFileName, sizeof acModulePath, addr acModulePath, esp
pop eax
invoke CreateService, hSCManager, addr g_szDriverName, $CTA0("Virtual Disk Drive"), \
SERVICE_START + DELETE, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, \
SERVICE_ERROR_IGNORE, addr acModulePath, NULL, NULL, NULL, NULL, NULL
.if eax != NULL
mov hService, eax
invoke PutRamDiskParamsIntoRegistry
.if eax == TRUE
invoke StartService, hService, 0, NULL
.if eax != 0
mov fOk, TRUE
.endif
.endif
.if fOk == FALSE
invoke DeleteService, hService
.endif
invoke CloseServiceHandle, hService
.endif
invoke CloseServiceHandle, hSCManager
.endif
mov eax, fOk
ret
LoadDriver endp
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; UnloadDriver
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
UnloadDriver proc
local fOk:BOOL
local hSCManager:HANDLE
local hService:HANDLE
local _ss:SERVICE_STATUS
and fOk, FALSE ; assume error
invoke CloseHandle, g_hDevice
invoke OpenSCManager, NULL, NULL, SC_MANAGER_ALL_ACCESS
.if eax != NULL
mov hSCManager, eax
invoke OpenService, hSCManager, addr g_szDriverName, SERVICE_STOP + DELETE
.if eax != NULL
mov hService, eax
invoke ControlService, hService, SERVICE_CONTROL_STOP, addr _ss
.if eax != 0
mov fOk, TRUE
.endif
invoke DeleteService, hService
invoke CloseServiceHandle, hService
.endif
invoke CloseServiceHandle, hSCManager
.endif
mov eax, fOk
ret
UnloadDriver endp
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; BroadcastDeviceMessage
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
BroadcastDeviceMessage proc dwDriveLetter:DWORD, uMsg:UINT
local dbv:DEV_BROADCAST_VOLUME
Fix DEV_BROADCAST_VOLUME defined since win2000
Fix dbcv_flags is defined as WORD. Check structure size
mov dbv.dbcv_size, sizeof dbv
mov dbv.dbcv_devicetype, DBT_DEVTYP_VOLUME ; Logical volume
and dbv.dbcv_reserved, 0
mov ecx, dwDriveLetter
sub ecx, 'A'
xor eax, eax
inc eax
shl eax, cl
mov dbv.dbcv_unitmask, eax ; Each bit in the mask corresponds to one logical drive
mov dbv.dbcv_flags, DBTF_MEDIA ; Change affects media in drive
invoke BroadcastSystemMessage, BSF_NOHANG + BSF_POSTMESSAGE, NULL, WM_DEVICECHANGE, uMsg, addr dbv
.if ( eax != 0 ) && ( eax != -1 )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -