Part 3: detach a virtual disk using truecrypt Mount
After a virtual disk is loaded, you can create a file protected by the encryption of the virtual disk, and then uninstall the virtual disk. The created file is encrypted and stored in the encrypted volume. This step is relatively simple, how is truecrypt actually implemented?
Another core function of truecrypt is the unmountvolume function for detaching a virtual disk. It accepts three functions: the input window handle, the serial number of the disk partition, And the ID of whether to force detach. Its Prototype in truecrypt is
Bool unmountvolume (hwnd hwnddlg, <br/> int ndosdriveno, <br/> bool forceunmount)
Next, let's take a look at the specific meaning of each parameter.
Parameters |
Type |
Description |
Example |
Hwnddlg |
Hwnd |
Input window handle |
Null |
Driveno |
Int |
Partition Number of the attached Disk |
8 |
Forceunmount |
Bool |
Force uninstall? |
False |
Next let's look at the specific example
Int unmount (INT ndriveno, hwnd hwnddlg) <br/>{< br/> int ndosdriveno; <br/> int unmounted = 0; <br/> bool bforceunmount = false; /* unmount volume even if it cannot be locked */<br/> // ndosdriveno = 8; <br/> ndosdriveno = ndriveno; <br/> bforceunmount = false; <br/> unmounted = unmountvolume (hwnddlg, ndosdriveno, bforceunmount); <br/> If (unmounted = 1) <br/> return 1; <br/> else <br/> return 0; <br/>}
When unmounted is set to 1, the virtual disk is successfully detached. When unmounted is set to 0, the unmount fails. What are the causes of the failure?
1. unmount_failed uninstall Error
2. The unmount_lock_failed virtual disk is opened or the files in the Virtual Disk are still opened.
In the second case, the handling situation of truecrypt is a prompt, and the virtual disk is still on, prompting the user whether to force Uninstall. If it is forcibly uninstalled, the created files will not be saved, the virtual disk is forcibly exited. If the virtual disk is not forcibly detached, the detach operation is performed again.
Bool unmountvolume (hwnd hwnddlg, int ndosdriveno, bool forceunmount) <br/>{< br/> int result; <br/> bool forced = forceunmount; <br/> int dismountmaxretries = unmount_max_auto_retries; <br/> retry: <br/> broadcastdevicechange (dbt_deviceremovepending, ndosdriveno, 0 ); <br/> DO <br/> {<br/> result = driverunmountvolume (hwnddlg, ndosdriveno, forced); <br/> If (result = err_files_open) <br/> sleep (Unmount_auto_retry_delay); <br/> else <br/> break; <br/>}while (-- dismountmaxretries> 0); <br/> If (result! = 0) <br/>{< br/> If (result = err_files_open &&! Silent) <br/>{< br/> If (idyes = askwarnnoyes ("unmount_lock_failed") <br/>{< br/> forced = true; <br/> goto retry; <br/>}< br/> return false; <br/>}< br/> error ("unmount_failed "); <br/> return false; <br/>}</P> <p> broadcastdevicechange (dbt_deviceremovecomplete, ndosdriveno, 0); <br/> return true; <br/>}
However, in some application scenarios, users do not want to force unmount the file, because in that case, the user's operation documents will not be saved, so you need to continue to kindly prompt that the user's virtual disk is still open, therefore, when the result = err_files_open &! is determined &&! When silent is used, return false directly, or comment forced = true. the user is always prompted and can be modified according to the specific application scenario.